Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 27 additions & 5 deletions components/mdc/Checkbox/Checkbox.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -19,26 +19,48 @@ export let disabled = false
export let uppercase = false
/** @type {string} random id prefixed with checkbox- for the input id*/
export let inputID = generateRandomID('checkbox-')
/** @type {string} name attribute for the checkbox input - important for forms */
export let name = ''
/** @type {string} value attribute when checked (default is "on") */
export let value = 'on'

let checkboxElement = {}
let formFieldElement = {}
let checkbox

$: if (checkbox) checkbox.checked = checked

onMount(() => {
checkbox = new MDCCheckbox(checkboxElement)
const formField = new MDCFormField(formFieldElement)

formField.input = checkbox

checkbox.checked = checked
})

const handleChange = () => dispatch(checkbox.checked ? 'checked' : 'unchecked')
$: if (checkbox && checkbox.checked !== checked) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this be checkbox?.checked?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yup. Thanks

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actually I don't think that's equivalent because if checkbox is undefined then the block won't run, but if checked is undefined it will as long as checked isn't undefined.

checkbox.checked = checked
}

const handleChange = () => {
if (checkbox) {
checked = checkbox.checked
dispatch(checkbox.checked ? 'checked' : 'unchecked')
}
}
</script>

<div class="mdc-form-field {$$props.class || ''}" bind:this={formFieldElement}>
<div class="mdc-checkbox" bind:this={checkboxElement}>
<input type="checkbox" {disabled} on:change={handleChange} class="mdc-checkbox__native-control" id={inputID} />
<input
type="checkbox"
{name}
{value}
{checked}
{disabled}
on:change={handleChange}
on:change
class="mdc-checkbox__native-control"
id={inputID}
/>
<div class="mdc-checkbox__background">
<svg class="mdc-checkbox__checkmark" viewBox="0 0 24 24">
<path class="mdc-checkbox__checkmark-path" fill="none" d="M1.73,12.91 8.1,19.28 22.79,4.59" />
Expand Down
2 changes: 2 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ declare module '@silintl/ui-components' {
uppercase?: boolean
class?: string
inputID?: string
name?: string
value?: string
}
export class Checkbox extends SvelteComponent<CheckboxProps> {}

Expand Down
Loading
Loading