Skip to content

Commit b54876b

Browse files
authored
feat(toggle): add ref prop (#2252)
Closes #2218
1 parent 82ef787 commit b54876b

File tree

6 files changed

+45
-13
lines changed

6 files changed

+45
-13
lines changed

COMPONENT_INDEX.md

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4390,17 +4390,18 @@ export type CarbonTheme = "white" | "g10" | "g80" | "g90" | "g100";
43904390

43914391
### Props
43924392

4393-
| Prop name | Required | Kind | Reactive | Type | Default value | Description |
4394-
| :-------- | :------- | :--------------- | :------- | ---------------------------------- | ------------------------------------------------ | ----------------------------------------------- |
4395-
| toggled | No | <code>let</code> | Yes | <code>boolean</code> | <code>false</code> | Set to `true` to toggle the checkbox input |
4396-
| size | No | <code>let</code> | No | <code>"default" &#124; "sm"</code> | <code>"default"</code> | Specify the toggle size |
4397-
| disabled | No | <code>let</code> | No | <code>boolean</code> | <code>false</code> | Set to `true` to disable checkbox input |
4398-
| labelA | No | <code>let</code> | No | <code>string</code> | <code>"Off"</code> | Specify the label for the untoggled state |
4399-
| labelB | No | <code>let</code> | No | <code>string</code> | <code>"On"</code> | Specify the label for the toggled state |
4400-
| labelText | No | <code>let</code> | No | <code>string</code> | <code>""</code> | Specify the label text |
4401-
| hideLabel | No | <code>let</code> | No | <code>boolean</code> | <code>false</code> | Set to `true` to visually hide the label text |
4402-
| id | No | <code>let</code> | No | <code>string</code> | <code>"ccs-" + Math.random().toString(36)</code> | Set an id for the input element |
4403-
| name | No | <code>let</code> | No | <code>string</code> | <code>undefined</code> | Specify a name attribute for the checkbox input |
4393+
| Prop name | Required | Kind | Reactive | Type | Default value | Description |
4394+
| :-------- | :------- | :--------------- | :------- | ----------------------------------------- | ------------------------------------------------ | ----------------------------------------------- |
4395+
| ref | No | <code>let</code> | Yes | <code>null &#124; HTMLInputElement</code> | <code>null</code> | Obtain a reference to the input HTML element |
4396+
| toggled | No | <code>let</code> | Yes | <code>boolean</code> | <code>false</code> | Set to `true` to toggle the checkbox input |
4397+
| size | No | <code>let</code> | No | <code>"default" &#124; "sm"</code> | <code>"default"</code> | Specify the toggle size |
4398+
| disabled | No | <code>let</code> | No | <code>boolean</code> | <code>false</code> | Set to `true` to disable checkbox input |
4399+
| labelA | No | <code>let</code> | No | <code>string</code> | <code>"Off"</code> | Specify the label for the untoggled state |
4400+
| labelB | No | <code>let</code> | No | <code>string</code> | <code>"On"</code> | Specify the label for the toggled state |
4401+
| labelText | No | <code>let</code> | No | <code>string</code> | <code>""</code> | Specify the label text |
4402+
| hideLabel | No | <code>let</code> | No | <code>boolean</code> | <code>false</code> | Set to `true` to visually hide the label text |
4403+
| id | No | <code>let</code> | No | <code>string</code> | <code>"ccs-" + Math.random().toString(36)</code> | Set an id for the input element |
4404+
| name | No | <code>let</code> | No | <code>string</code> | <code>undefined</code> | Specify a name attribute for the checkbox input |
44044405

44054406
### Slots
44064407

docs/src/COMPONENT_API.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16858,6 +16858,18 @@
1685816858
"isRequired": false,
1685916859
"constant": false,
1686016860
"reactive": false
16861+
},
16862+
{
16863+
"name": "ref",
16864+
"kind": "let",
16865+
"description": "Obtain a reference to the input HTML element",
16866+
"type": "null | HTMLInputElement",
16867+
"value": "null",
16868+
"isFunction": false,
16869+
"isFunctionDeclaration": false,
16870+
"isRequired": false,
16871+
"constant": false,
16872+
"reactive": true
1686116873
}
1686216874
],
1686316875
"moduleExports": [],

src/Toggle/Toggle.svelte

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,12 @@
3636
*/
3737
export let name = undefined;
3838
39+
/** Obtain a reference to the input HTML element */
40+
export let ref = null;
41+
3942
import { createEventDispatcher } from "svelte";
4043
4144
const dispatch = createEventDispatcher();
42-
43-
let ref = null;
4445
</script>
4546

4647
<!-- svelte-ignore a11y-mouse-events-have-key-events -->

tests/Toggle/Toggle.test.svelte

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
1+
<svelte:options accessors />
2+
13
<script lang="ts">
24
import { Toggle } from "carbon-components-svelte";
35
46
let toggled = false;
57
let initialToggled = true;
8+
export let ref: HTMLInputElement | null = null;
69
</script>
710

811
<Toggle
912
bind:toggled
13+
bind:ref
1014
labelText="Default toggle"
1115
on:toggle={(e) => {
1216
console.log("toggled:", e.detail.toggled);

tests/Toggle/Toggle.test.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,4 +269,12 @@ describe("Toggle", () => {
269269

270270
expect(screen.getByText("On slot")).toBeInTheDocument();
271271
});
272+
273+
it("should bind ref to input element", () => {
274+
const { component } = render(Toggle);
275+
276+
expect(component.ref).toBeInstanceOf(HTMLInputElement);
277+
assert(component.ref);
278+
expect(component.ref.type).toBe("checkbox");
279+
});
272280
});

types/Toggle/Toggle.svelte.d.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,12 @@ type $Props = {
5858
*/
5959
name?: string;
6060

61+
/**
62+
* Obtain a reference to the input HTML element
63+
* @default null
64+
*/
65+
ref?: null | HTMLInputElement;
66+
6167
[key: `data-${string}`]: any;
6268
};
6369

0 commit comments

Comments
 (0)