checkbox.tsx 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. import { Checkbox as Kobalte } from "@kobalte/core/checkbox"
  2. import { Show, splitProps } from "solid-js"
  3. import type { ComponentProps, JSX, ParentProps } from "solid-js"
  4. export interface CheckboxProps extends ParentProps<ComponentProps<typeof Kobalte>> {
  5. hideLabel?: boolean
  6. description?: string
  7. icon?: JSX.Element
  8. }
  9. export function Checkbox(props: CheckboxProps) {
  10. const [local, others] = splitProps(props, ["children", "class", "label", "hideLabel", "description", "icon"])
  11. return (
  12. <Kobalte {...others} data-component="checkbox">
  13. <Kobalte.Input data-slot="checkbox-checkbox-input" />
  14. <Kobalte.Control data-slot="checkbox-checkbox-control">
  15. <Kobalte.Indicator data-slot="checkbox-checkbox-indicator">
  16. {local.icon || (
  17. <svg viewBox="0 0 12 12" fill="none" width="10" height="10" xmlns="http://www.w3.org/2000/svg">
  18. <path
  19. d="M3 7.17905L5.02703 8.85135L9 3.5"
  20. stroke="currentColor"
  21. stroke-width="1.5"
  22. stroke-linecap="square"
  23. />
  24. </svg>
  25. )}
  26. </Kobalte.Indicator>
  27. </Kobalte.Control>
  28. <div data-slot="checkbox-checkbox-content">
  29. <Show when={props.children}>
  30. <Kobalte.Label data-slot="checkbox-checkbox-label" classList={{ "sr-only": local.hideLabel }}>
  31. {props.children}
  32. </Kobalte.Label>
  33. </Show>
  34. <Show when={local.description}>
  35. <Kobalte.Description data-slot="checkbox-checkbox-description">{local.description}</Kobalte.Description>
  36. </Show>
  37. <Kobalte.ErrorMessage data-slot="checkbox-checkbox-error" />
  38. </div>
  39. </Kobalte>
  40. )
  41. }