Skip to content

Commit

Permalink
fix: adds required attribute on toggle
Browse files Browse the repository at this point in the history
  • Loading branch information
KovaCro committed Dec 21, 2023
1 parent 9f8f252 commit 5666b7a
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
5 changes: 4 additions & 1 deletion docs/docs/Form fields/toggle.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
| size | | `'large'`|`'small'` | size of the toggle button |
| checked | | `boolean`| is on by default |
| disabled | | `boolean`| determines if toggle is disabled |
| required | | `boolean`| if true toggle needs to be check to pass validity |
| requiredValidationMessage | | `string` | validation message for when component does not satisfy required |


### Slots
Expand All @@ -21,7 +23,8 @@ This component does not have any slots.

### Methods

This component does not have any methods.
- `reportValidity`
- triggers reportValidity

### Events

Expand Down
20 changes: 19 additions & 1 deletion packages/lib/src/form-fields/toggle.wc.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,33 @@
export let name: string = '';
export let value: string = '';
export let label: string | null = null;
export let required: boolean = false;
export let requiredValidationMessage: string = ''
export let size: 'small' | 'large' = 'small';
export let checked: boolean = false;
export let disabled = false;
let checkboxEl;
export const getValue = () => checked;
export const reportValidity = () => {
attachedInternals.reportValidity();
};
const dispatch = createEventDispatcher();
$: {
attachedInternals.checkValidity();
if (checkboxEl) {
if (checkboxEl.validity.valueMissing) {
if (requiredValidationMessage) {
attachedInternals.setValidity(
{ customError: true },
requiredValidationMessage
);
}
}
}
dispatch('value', checked);
}
</script>
Expand All @@ -40,7 +58,7 @@
<span class="label" style={`font-size: ${size == 'small' ? '12px' : '20px'}`}>{@html label}</span>
{/if}
<label class={'switch ' + size}>
<input type="checkbox" {name} {disabled} bind:checked bind:value hidden />
<input type="checkbox" {name} {disabled} {required} bind:checked bind:value hidden bind:this={checkboxEl} />
<span class="slider round" class:pointer={!disabled}></span>
</label>

Expand Down

0 comments on commit 5666b7a

Please sign in to comment.