checkbox.tsx 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. import { Checkbox as Kobalte } from "@kobalte/core/checkbox"
  2. import { children, 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. const resolved = children(() => local.children)
  12. return (
  13. <Kobalte {...others} data-component="checkbox">
  14. <Kobalte.Input data-slot="checkbox-checkbox-input" />
  15. <Kobalte.Control data-slot="checkbox-checkbox-control">
  16. <Kobalte.Indicator data-slot="checkbox-checkbox-indicator">
  17. {local.icon || (
  18. <svg viewBox="0 0 12 12" fill="none" xmlns="http://www.w3.org/2000/svg">
  19. <path
  20. d="M3 7.17905L5.02703 8.85135L9 3.5"
  21. stroke="currentColor"
  22. stroke-width="1.5"
  23. stroke-linecap="square"
  24. />
  25. </svg>
  26. )}
  27. </Kobalte.Indicator>
  28. </Kobalte.Control>
  29. <div data-slot="checkbox-checkbox-content">
  30. <Show when={resolved()}>
  31. <Kobalte.Label data-slot="checkbox-checkbox-label" classList={{ "sr-only": local.hideLabel }}>
  32. {resolved()}
  33. </Kobalte.Label>
  34. </Show>
  35. <Show when={local.description}>
  36. <Kobalte.Description data-slot="checkbox-checkbox-description">{local.description}</Kobalte.Description>
  37. </Show>
  38. <Kobalte.ErrorMessage data-slot="checkbox-checkbox-error" />
  39. </div>
  40. </Kobalte>
  41. )
  42. }