input.tsx 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import { TextField as Kobalte } from "@kobalte/core/text-field"
  2. import { Show, splitProps } from "solid-js"
  3. import type { ComponentProps } from "solid-js"
  4. export interface InputProps
  5. extends ComponentProps<typeof Kobalte.Input>,
  6. Partial<
  7. Pick<
  8. ComponentProps<typeof Kobalte>,
  9. | "name"
  10. | "defaultValue"
  11. | "value"
  12. | "onChange"
  13. | "onKeyDown"
  14. | "validationState"
  15. | "required"
  16. | "disabled"
  17. | "readOnly"
  18. >
  19. > {
  20. label?: string
  21. hideLabel?: boolean
  22. hidden?: boolean
  23. description?: string
  24. error?: string
  25. variant?: "normal" | "ghost"
  26. }
  27. export function Input(props: InputProps) {
  28. const [local, others] = splitProps(props, [
  29. "name",
  30. "defaultValue",
  31. "value",
  32. "onChange",
  33. "onKeyDown",
  34. "validationState",
  35. "required",
  36. "disabled",
  37. "readOnly",
  38. "class",
  39. "label",
  40. "hidden",
  41. "hideLabel",
  42. "description",
  43. "error",
  44. "variant",
  45. ])
  46. return (
  47. <Kobalte
  48. data-component="input"
  49. data-variant={local.variant || "normal"}
  50. name={local.name}
  51. defaultValue={local.defaultValue}
  52. value={local.value}
  53. onChange={local.onChange}
  54. onKeyDown={local.onKeyDown}
  55. required={local.required}
  56. disabled={local.disabled}
  57. readOnly={local.readOnly}
  58. style={{ height: local.hidden ? 0 : undefined }}
  59. validationState={local.validationState}
  60. >
  61. <Show when={local.label}>
  62. <Kobalte.Label data-slot="input-label" classList={{ "sr-only": local.hideLabel }}>
  63. {local.label}
  64. </Kobalte.Label>
  65. </Show>
  66. <Kobalte.Input {...others} data-slot="input-input" class={local.class} />
  67. <Show when={local.description}>
  68. <Kobalte.Description data-slot="input-description">{local.description}</Kobalte.Description>
  69. </Show>
  70. <Kobalte.ErrorMessage data-slot="input-error">{local.error}</Kobalte.ErrorMessage>
  71. </Kobalte>
  72. )
  73. }