Skip to content

Commit 3b2eeaf

Browse files
committed
fix(styled checkbox): make one way and two way data binding work
1 parent e8fd4b0 commit 3b2eeaf

File tree

4 files changed

+43
-16
lines changed

4 files changed

+43
-16
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { component$, useSignal } from '@builder.io/qwik';
2+
import { Checkbox, Label } from '~/components/ui';
3+
4+
export default component$(() => {
5+
const checkedSig = useSignal(false);
6+
return (
7+
<div>
8+
<div class="flex items-center space-x-2">
9+
<Checkbox id="terms" bind:checked={checkedSig} />
10+
<div>
11+
<Label for="terms">Accept terms and conditions</Label>
12+
</div>
13+
</div>
14+
<p class="text-sm text-muted-foreground">checked: {checkedSig.value.toString()}</p>
15+
</div>
16+
);
17+
});

apps/website/src/routes/docs/styled/checkbox/index.mdx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,7 @@ export const Checkbox = component$<PropsOf<'input'>>(({ name, ...props }) => {
5353
### Disabled
5454

5555
<Showcase name="disabled" vertical />
56+
57+
### Data binding
58+
59+
<Showcase name="data-binding" vertical />
Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,23 @@
1-
import { PropsOf, component$ } from '@builder.io/qwik';
1+
import { $, PropsOf, component$ } from '@builder.io/qwik';
22
import { cn } from '@qwik-ui/utils';
33

4-
export const Checkbox = component$<PropsOf<'input'>>(({ id, name, ...props }) => {
5-
const inputId = id || name;
6-
return (
7-
<input
8-
type="checkbox"
9-
{...props}
10-
class={cn(
11-
'peer peer h-4 w-4 shrink-0 border-primary text-primary accent-primary ring-offset-background focus:ring-ring focus-visible:ring-ring disabled:cursor-not-allowed disabled:opacity-50 data-[state=checked]:bg-primary data-[state=checked]:text-primary-foreground',
12-
props.class,
13-
)}
14-
id={inputId}
15-
/>
16-
);
17-
});
4+
export const Checkbox = component$<PropsOf<'input'>>(
5+
({ id, name, ['bind:checked']: checkedSig, checked, onInput$, ...props }) => {
6+
const inputId = id || name;
7+
return (
8+
<input
9+
// workaround to support two way data-binding on the Input component (https://github.com/QwikDev/qwik/issues/3926)
10+
checked={checkedSig ? checkedSig.value : checked}
11+
onInput$={checkedSig ? $((_, el) => (checkedSig.value = el.checked)) : onInput$}
12+
data-checked={checked || checkedSig?.value || ''}
13+
type="checkbox"
14+
{...props}
15+
class={cn(
16+
'peer h-4 w-4 shrink-0 border-primary text-primary accent-primary ring-offset-background focus:ring-ring focus-visible:ring-ring disabled:cursor-not-allowed disabled:opacity-50',
17+
props.class,
18+
)}
19+
id={inputId}
20+
/>
21+
);
22+
},
23+
);

packages/kit-styled/src/components/input/input.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export const Input = component$<InputProps>(
1616
{...props}
1717
aria-errormessage={`${inputId}-error`}
1818
aria-invaid={!!error}
19-
// workaround for one way and two way data-binding https://github.com/QwikDev/qwik/issues/3926
19+
// workaround to support two way data-binding on the Input component (https://github.com/QwikDev/qwik/issues/3926)
2020
value={valueSig ? valueSig.value : value}
2121
onInput$={valueSig ? $((__, el) => (valueSig.value = el.value)) : onInput$}
2222
ref={inputRef}

0 commit comments

Comments
 (0)