-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #68 from kunai-consulting/headless-checkbox
Headless checkbox
- Loading branch information
Showing
21 changed files
with
644 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { component$, useStyles$ } from "@builder.io/qwik"; | ||
import { Checkbox } from "@kunai-consulting/qwik-components"; | ||
import { LuCheck } from "@qwikest/icons/lucide"; | ||
|
||
export default component$(() => { | ||
useStyles$(styles); | ||
|
||
const description = "By checking this box, you acknowledge that you have read, understood, and agree to our Terms of Service and Privacy Policy. This includes consent to process your personal data as described in our policies."; | ||
|
||
return ( | ||
<Checkbox.Root isDescription> | ||
<div style={{ display: "flex", alignItems: "center", gap: "8px", marginBottom: "8px" }}> | ||
<Checkbox.Trigger class="checkbox-trigger"> | ||
<Checkbox.Indicator class="checkbox-indicator"> | ||
<LuCheck /> | ||
</Checkbox.Indicator> | ||
</Checkbox.Trigger> | ||
<Checkbox.Label>I accept the Terms and Conditions</Checkbox.Label> | ||
</div> | ||
<Checkbox.Description> | ||
By checking this box, you acknowledge that you have read, understood, and agree to our Terms of Service and Privacy Policy. This includes consent to process your personal data as described in our policies. | ||
</Checkbox.Description> | ||
</Checkbox.Root> | ||
); | ||
}); | ||
|
||
// example styles | ||
import styles from "./checkbox.css?inline"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { $, component$, type Signal, useSignal, useStyles$ } from "@builder.io/qwik"; | ||
import { Checkbox } from "@kunai-consulting/qwik-components"; | ||
import { LuCheck } from "@qwikest/icons/lucide"; | ||
|
||
export default component$(() => { | ||
useStyles$(styles); | ||
|
||
const formData = useSignal<Record<string, FormDataEntryValue>>(); | ||
|
||
const isChecked = useSignal<boolean | "mixed">("mixed"); | ||
|
||
return ( | ||
<form | ||
preventdefault:submit | ||
onSubmit$={(e) => { | ||
const form = e.target as HTMLFormElement; | ||
formData.value = Object.fromEntries(new FormData(form)); | ||
}} | ||
style={{ display: "flex", flexDirection: "column", gap: "8px" }} | ||
> | ||
<TermsCheckbox isChecked={isChecked} /> | ||
<button | ||
data-test-mixed | ||
onClick$={() => { | ||
isChecked.value = "mixed"; | ||
}} | ||
type="button" | ||
> | ||
Make it mixed | ||
</button> | ||
</form> | ||
); | ||
}); | ||
|
||
export const TermsCheckbox = component$( | ||
({ isChecked }: { isChecked: Signal<boolean | "mixed"> }) => { | ||
return ( | ||
<Checkbox.Root bind:checked={isChecked}> | ||
<Checkbox.HiddenNativeInput /> | ||
<div style={{ display: "flex", alignItems: "center", gap: "8px" }}> | ||
<Checkbox.Trigger class="checkbox-trigger"> | ||
<Checkbox.Indicator class="checkbox-indicator"> | ||
<LuCheck /> | ||
</Checkbox.Indicator> | ||
</Checkbox.Trigger> | ||
<Checkbox.Label>I accept the Terms and Conditions</Checkbox.Label> | ||
</div> | ||
</Checkbox.Root> | ||
); | ||
} | ||
); | ||
|
||
// example styles | ||
import styles from "./checkbox.css?inline"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { $, component$, useSignal, useStyles$, useStylesScoped$ } from "@builder.io/qwik"; | ||
import { Checkbox } from "@kunai-consulting/qwik-components"; | ||
import { LuCheck } from "@qwikest/icons/lucide"; | ||
|
||
export default component$(() => { | ||
useStyles$(styles); | ||
|
||
const formData = useSignal<Record<string, FormDataEntryValue>>(); | ||
|
||
return ( | ||
<form | ||
preventdefault:submit | ||
onSubmit$={(e) => { | ||
const form = e.target as HTMLFormElement; | ||
formData.value = Object.fromEntries(new FormData(form)); | ||
}} | ||
style={{ display: "flex", flexDirection: "column", gap: "8px" }} | ||
> | ||
<TermsCheckbox /> | ||
<button type="submit">Submit</button> | ||
{formData.value && <div>Submitted: {JSON.stringify(formData.value, null, 2)}</div>} | ||
</form> | ||
); | ||
}); | ||
|
||
export const TermsCheckbox = component$(() => { | ||
return ( | ||
<Checkbox.Root name="terms"> | ||
<Checkbox.HiddenNativeInput /> | ||
<div style={{ display: "flex", alignItems: "center", gap: "8px" }}> | ||
<Checkbox.Trigger class="checkbox-trigger"> | ||
<Checkbox.Indicator class="checkbox-indicator"> | ||
<LuCheck /> | ||
</Checkbox.Indicator> | ||
</Checkbox.Trigger> | ||
<Checkbox.Label>I accept the Terms and Conditions</Checkbox.Label> | ||
</div> | ||
</Checkbox.Root> | ||
); | ||
}); | ||
|
||
// example styles | ||
import styles from "./checkbox.css?inline"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { $, component$, useSignal, useStyles$, useStylesScoped$ } from "@builder.io/qwik"; | ||
import { Checkbox } from "@kunai-consulting/qwik-components"; | ||
import { LuCheck } from "@qwikest/icons/lucide"; | ||
|
||
export default component$(() => { | ||
useStyles$(styles); | ||
|
||
const formData = useSignal<Record<string, FormDataEntryValue>>(); | ||
|
||
return ( | ||
<form | ||
preventdefault:submit | ||
onSubmit$={(e) => { | ||
const form = e.target as HTMLFormElement; | ||
formData.value = Object.fromEntries(new FormData(form)); | ||
}} | ||
style={{ display: "flex", flexDirection: "column", gap: "8px" }} | ||
> | ||
<TermsCheckbox /> | ||
<button type="submit">Submit</button> | ||
{formData.value && <div>Submitted: {JSON.stringify(formData.value, null, 2)}</div>} | ||
</form> | ||
); | ||
}); | ||
|
||
export const TermsCheckbox = component$(() => { | ||
return ( | ||
<Checkbox.Root name="terms" required> | ||
<Checkbox.HiddenNativeInput /> | ||
<div style={{ display: "flex", alignItems: "center", gap: "8px" }}> | ||
<Checkbox.Trigger class="checkbox-trigger"> | ||
<Checkbox.Indicator class="checkbox-indicator"> | ||
<LuCheck /> | ||
</Checkbox.Indicator> | ||
</Checkbox.Trigger> | ||
<Checkbox.Label>I accept the Terms and Conditions</Checkbox.Label> | ||
</div> | ||
</Checkbox.Root> | ||
); | ||
}); | ||
|
||
// example styles | ||
import styles from "./checkbox.css?inline"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import { | ||
component$, | ||
type Signal, | ||
useComputed$, | ||
useSignal, | ||
useStyles$ | ||
} from "@builder.io/qwik"; | ||
import { Checkbox } from "@kunai-consulting/qwik-components"; | ||
import { LuCheck } from "@qwikest/icons/lucide"; | ||
|
||
export default component$(() => { | ||
useStyles$(styles); | ||
|
||
const formData = useSignal<Record<string, FormDataEntryValue>>(); | ||
const isChecked = useSignal(false); | ||
const isSubmitAttempt = useSignal(false); | ||
const isError = useComputed$(() => !isChecked.value && isSubmitAttempt.value); | ||
|
||
return ( | ||
<form | ||
preventdefault:submit | ||
noValidate | ||
onSubmit$={(e) => { | ||
const form = e.target as HTMLFormElement; | ||
if (!isChecked.value) { | ||
isSubmitAttempt.value = true; | ||
return; | ||
} | ||
|
||
formData.value = Object.fromEntries(new FormData(form)); | ||
}} | ||
style={{ display: "flex", flexDirection: "column", gap: "8px" }} | ||
> | ||
<TermsCheckbox isChecked={isChecked} isError={isError} /> | ||
<button type="submit">Submit</button> | ||
{formData.value && <div>Submitted: {JSON.stringify(formData.value, null, 2)}</div>} | ||
</form> | ||
); | ||
}); | ||
|
||
type TermsCheckboxProps = { | ||
isChecked: Signal<boolean>; | ||
isError: Signal<boolean>; | ||
}; | ||
|
||
export const TermsCheckbox = component$(({ isChecked, isError }: TermsCheckboxProps) => { | ||
return ( | ||
<Checkbox.Root name="terms" required bind:checked={isChecked}> | ||
<Checkbox.HiddenNativeInput /> | ||
<div | ||
style={{ display: "flex", alignItems: "center", gap: "8px", marginBottom: "8px" }} | ||
> | ||
<Checkbox.Trigger class="checkbox-trigger"> | ||
<Checkbox.Indicator class="checkbox-indicator"> | ||
<LuCheck /> | ||
</Checkbox.Indicator> | ||
</Checkbox.Trigger> | ||
<Checkbox.Label>I accept the Terms and Conditions</Checkbox.Label> | ||
</div> | ||
{isError.value && ( | ||
<Checkbox.ErrorMessage style={{ color: "red" }}> | ||
Please accept the terms and conditions | ||
</Checkbox.ErrorMessage> | ||
)} | ||
</Checkbox.Root> | ||
); | ||
}); | ||
|
||
// example styles | ||
import styles from "./checkbox.css?inline"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { $, component$, useSignal, useStyles$, useStylesScoped$ } from "@builder.io/qwik"; | ||
import { Checkbox } from "@kunai-consulting/qwik-components"; | ||
import { LuCheck } from "@qwikest/icons/lucide"; | ||
|
||
export default component$(() => { | ||
useStyles$(styles); | ||
|
||
const formData = useSignal<Record<string, FormDataEntryValue>>(); | ||
|
||
return ( | ||
<form | ||
preventdefault:submit | ||
onSubmit$={(e) => { | ||
const form = e.target as HTMLFormElement; | ||
formData.value = Object.fromEntries(new FormData(form)); | ||
}} | ||
style={{ display: "flex", flexDirection: "column", gap: "8px" }} | ||
> | ||
<TermsCheckbox /> | ||
<button type="submit">Submit</button> | ||
{formData.value && <div>Submitted: {JSON.stringify(formData.value, null, 2)}</div>} | ||
</form> | ||
); | ||
}); | ||
|
||
export const TermsCheckbox = component$(() => { | ||
return ( | ||
<Checkbox.Root name="terms" value="checked"> | ||
<Checkbox.HiddenNativeInput /> | ||
<div style={{ display: "flex", alignItems: "center", gap: "8px" }}> | ||
<Checkbox.Trigger class="checkbox-trigger"> | ||
<Checkbox.Indicator class="checkbox-indicator"> | ||
<LuCheck /> | ||
</Checkbox.Indicator> | ||
</Checkbox.Trigger> | ||
<Checkbox.Label>I accept the Terms and Conditions</Checkbox.Label> | ||
</div> | ||
</Checkbox.Root> | ||
); | ||
}); | ||
|
||
// example styles | ||
import styles from "./checkbox.css?inline"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,15 @@ | ||
import { createContextId, type Signal } from "@builder.io/qwik"; | ||
import { createContextId, JSXNode, type Signal } from "@builder.io/qwik"; | ||
|
||
export const checkboxContextId = createContextId<CheckboxContext>("qds-checkbox-context"); | ||
|
||
export type CheckboxContext = { | ||
isCheckedSig: Signal<boolean | "mixed">; | ||
isDisabledSig: Signal<boolean | undefined>; | ||
isMixedSig: Signal<boolean | undefined>; | ||
isErrorSig: Signal<boolean | undefined>; | ||
localId: string; | ||
isDescription: boolean | undefined; | ||
name: string | undefined; | ||
required: boolean | undefined; | ||
value: string | undefined; | ||
triggerRef: Signal<HTMLButtonElement | undefined>; | ||
}; |
Oops, something went wrong.