mirror of
https://github.com/carbon-design-system/carbon-components-svelte.git
synced 2025-09-15 02:11:05 +00:00
fix(checkbox): support two way binding
- Forward events, inline functions - Add style, remove exported props (#7)
This commit is contained in:
parent
6cc2eecd88
commit
724196abb5
4 changed files with 21 additions and 21 deletions
|
@ -1,13 +1,13 @@
|
||||||
<script>
|
<script>
|
||||||
let className = undefined;
|
let className = undefined;
|
||||||
export { className as class };
|
export { className as class };
|
||||||
export let props = {};
|
export let style = undefined;
|
||||||
|
|
||||||
import { cx } from '../../lib';
|
import { cx } from '../../lib';
|
||||||
|
|
||||||
const _class = cx('--form-item', '--checkbox-wrapper', className);
|
const _class = cx('--form-item', '--checkbox-wrapper', className);
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<div {...props} class={_class}>
|
<div on:click on:mouseover on:mouseenter on:mouseleave class={_class} {style}>
|
||||||
<span class={cx('--checkbox-label', '--skeleton')} />
|
<span class={cx('--checkbox-label', '--skeleton')} />
|
||||||
</div>
|
</div>
|
||||||
|
|
|
@ -1,19 +1,13 @@
|
||||||
<script>
|
<script>
|
||||||
export let story = undefined;
|
export let story = undefined;
|
||||||
const { labelText, indeterminate, disabled, hideLabel, wrapperClassName } = $$props;
|
|
||||||
|
|
||||||
import { cx } from '../../lib';
|
import { cx } from '../../lib';
|
||||||
import Layout from '../../internal/ui/Layout.svelte';
|
import Layout from '../../internal/ui/Layout.svelte';
|
||||||
import Checkbox from './Checkbox.svelte';
|
import Checkbox from './Checkbox.svelte';
|
||||||
import CheckboxSkeleton from './Checkbox.Skeleton.svelte';
|
import CheckboxSkeleton from './Checkbox.Skeleton.svelte';
|
||||||
|
|
||||||
const checkboxProps = {
|
const { labelText, indeterminate, disabled, hideLabel, wrapperClassName } = $$props;
|
||||||
labelText,
|
const checkboxProps = { labelText, indeterminate, disabled, hideLabel, wrapperClassName };
|
||||||
indeterminate,
|
|
||||||
disabled,
|
|
||||||
hideLabel,
|
|
||||||
wrapperClassName
|
|
||||||
};
|
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<Layout>
|
<Layout>
|
||||||
|
|
|
@ -26,7 +26,4 @@ export const Unchecked = () => ({
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
export const Skeleton = () => ({
|
export const Skeleton = () => ({ Component, props: { story: 'skeleton' } });
|
||||||
Component,
|
|
||||||
props: { story: 'skeleton' }
|
|
||||||
});
|
|
||||||
|
|
|
@ -4,13 +4,13 @@
|
||||||
export let checked = false;
|
export let checked = false;
|
||||||
export let indeterminate = false;
|
export let indeterminate = false;
|
||||||
export let disabled = false;
|
export let disabled = false;
|
||||||
export let id = undefined;
|
export let id = Math.random();
|
||||||
export let labelText = undefined;
|
export let labelText = undefined;
|
||||||
export let hideLabel = false;
|
export let hideLabel = false;
|
||||||
export let title = '';
|
export let title = '';
|
||||||
export let wrapperClassName = undefined;
|
export let wrapperClassName = undefined;
|
||||||
export { wrapperClassName as wrapperClass };
|
export { wrapperClassName as wrapperClass };
|
||||||
export let props = {};
|
export let style = undefined;
|
||||||
|
|
||||||
import { createEventDispatcher } from 'svelte';
|
import { createEventDispatcher } from 'svelte';
|
||||||
import { cx } from '../../lib';
|
import { cx } from '../../lib';
|
||||||
|
@ -20,17 +20,26 @@
|
||||||
const _innerLabelClass = cx('--checkbox-label-text', hideLabel && '--visually-hidden');
|
const _innerLabelClass = cx('--checkbox-label-text', hideLabel && '--visually-hidden');
|
||||||
const _wrapperClass = cx('--form-item', '--checkbox-wrapper', wrapperClassName);
|
const _wrapperClass = cx('--form-item', '--checkbox-wrapper', wrapperClassName);
|
||||||
|
|
||||||
function handleChange(event) {
|
let inputRef = undefined;
|
||||||
dispatch('change', { checked: event.target.checked, id, event });
|
|
||||||
|
$: {
|
||||||
|
dispatch('check', { id, checked });
|
||||||
|
|
||||||
|
if (inputRef) {
|
||||||
|
inputRef.checked = checked;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<div class={_wrapperClass}>
|
<div on:click on:mouseover on:mouseenter on:mouseleave class={_wrapperClass} {style}>
|
||||||
<input
|
<input
|
||||||
{...props}
|
bind:this={inputRef}
|
||||||
type="checkbox"
|
type="checkbox"
|
||||||
class={cx('--checkbox')}
|
class={cx('--checkbox')}
|
||||||
on:change={handleChange}
|
on:change
|
||||||
|
on:change={() => {
|
||||||
|
checked = !checked;
|
||||||
|
}}
|
||||||
{indeterminate}
|
{indeterminate}
|
||||||
{disabled}
|
{disabled}
|
||||||
{checked}
|
{checked}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue