@leitware/composables 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (209) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +387 -0
  3. package/dist/presets/brutalist.css +298 -0
  4. package/dist/presets/default.css +298 -0
  5. package/dist/presets/signal-pop.css +369 -0
  6. package/dist/styles.css +2 -0
  7. package/package.json +216 -0
  8. package/rules/composables.md +244 -0
  9. package/src/_internal/accordion.tsx +93 -0
  10. package/src/_internal/ai-elements/attachments.tsx +385 -0
  11. package/src/_internal/ai-elements/chain-of-thought.tsx +197 -0
  12. package/src/_internal/ai-elements/code-block.tsx +516 -0
  13. package/src/_internal/ai-elements/confirmation.tsx +150 -0
  14. package/src/_internal/ai-elements/conversation.tsx +148 -0
  15. package/src/_internal/ai-elements/inline-citation.tsx +248 -0
  16. package/src/_internal/ai-elements/message.tsx +292 -0
  17. package/src/_internal/ai-elements/plan.tsx +126 -0
  18. package/src/_internal/ai-elements/prompt-input.tsx +1285 -0
  19. package/src/_internal/ai-elements/reasoning.tsx +208 -0
  20. package/src/_internal/ai-elements/shimmer.tsx +72 -0
  21. package/src/_internal/ai-elements/sources.tsx +53 -0
  22. package/src/_internal/ai-elements/suggestion.tsx +48 -0
  23. package/src/_internal/ai-elements/task.tsx +64 -0
  24. package/src/_internal/ai-elements/tool.tsx +156 -0
  25. package/src/_internal/alert-dialog.tsx +159 -0
  26. package/src/_internal/alert.tsx +88 -0
  27. package/src/_internal/aspect-ratio.tsx +23 -0
  28. package/src/_internal/avatar.tsx +58 -0
  29. package/src/_internal/badge.tsx +60 -0
  30. package/src/_internal/breadcrumb.tsx +104 -0
  31. package/src/_internal/bubble.tsx +125 -0
  32. package/src/_internal/button-group.tsx +78 -0
  33. package/src/_internal/button.tsx +62 -0
  34. package/src/_internal/calendar.tsx +179 -0
  35. package/src/_internal/card.tsx +83 -0
  36. package/src/_internal/carousel.tsx +215 -0
  37. package/src/_internal/checkbox.tsx +26 -0
  38. package/src/_internal/code-block.tsx +53 -0
  39. package/src/_internal/collapsible.tsx +15 -0
  40. package/src/_internal/combobox.tsx +264 -0
  41. package/src/_internal/command.tsx +155 -0
  42. package/src/_internal/container.tsx +35 -0
  43. package/src/_internal/dialog.tsx +132 -0
  44. package/src/_internal/direction.tsx +12 -0
  45. package/src/_internal/divider.tsx +162 -0
  46. package/src/_internal/dropdown-menu.tsx +247 -0
  47. package/src/_internal/dropzone.tsx +481 -0
  48. package/src/_internal/empty.tsx +94 -0
  49. package/src/_internal/field.tsx +221 -0
  50. package/src/_internal/grid-overlay.tsx +48 -0
  51. package/src/_internal/grid.tsx +109 -0
  52. package/src/_internal/hover-card.tsx +47 -0
  53. package/src/_internal/icon.tsx +45 -0
  54. package/src/_internal/input-group.tsx +146 -0
  55. package/src/_internal/input.tsx +24 -0
  56. package/src/_internal/item.tsx +187 -0
  57. package/src/_internal/kbd.tsx +28 -0
  58. package/src/_internal/label.tsx +18 -0
  59. package/src/_internal/layout-bento.tsx +386 -0
  60. package/src/_internal/layout.tsx +174 -0
  61. package/src/_internal/marker.tsx +66 -0
  62. package/src/_internal/message.tsx +85 -0
  63. package/src/_internal/native-select.tsx +50 -0
  64. package/src/_internal/pagination.tsx +108 -0
  65. package/src/_internal/popover.tsx +83 -0
  66. package/src/_internal/progress.tsx +59 -0
  67. package/src/_internal/radio-group.tsx +36 -0
  68. package/src/_internal/resizable.tsx +44 -0
  69. package/src/_internal/scroll-area.tsx +48 -0
  70. package/src/_internal/select.tsx +187 -0
  71. package/src/_internal/separator.tsx +19 -0
  72. package/src/_internal/sheet.tsx +122 -0
  73. package/src/_internal/sidebar.tsx +687 -0
  74. package/src/_internal/skeleton.tsx +13 -0
  75. package/src/_internal/slider.tsx +60 -0
  76. package/src/_internal/sonner.tsx +51 -0
  77. package/src/_internal/spacer.tsx +42 -0
  78. package/src/_internal/spinner.tsx +16 -0
  79. package/src/_internal/stack.tsx +78 -0
  80. package/src/_internal/switch.tsx +31 -0
  81. package/src/_internal/table.tsx +84 -0
  82. package/src/_internal/tabs.tsx +74 -0
  83. package/src/_internal/textarea.tsx +19 -0
  84. package/src/_internal/toggle-group.tsx +99 -0
  85. package/src/_internal/toggle.tsx +42 -0
  86. package/src/_internal/tooltip.tsx +55 -0
  87. package/src/_internal/typography.tsx +129 -0
  88. package/src/hooks/use-mobile.ts +19 -0
  89. package/src/hooks/use-numeric-input.ts +119 -0
  90. package/src/index.ts +443 -0
  91. package/src/lib/numeric-input.ts +88 -0
  92. package/src/lib/utils.ts +39 -0
  93. package/src/opinionated/accordion.tsx +92 -0
  94. package/src/opinionated/ai/attachments.tsx +27 -0
  95. package/src/opinionated/ai/chain-of-thought.tsx +16 -0
  96. package/src/opinionated/ai/code-block.tsx +22 -0
  97. package/src/opinionated/ai/confirmation.tsx +16 -0
  98. package/src/opinionated/ai/conversation.tsx +13 -0
  99. package/src/opinionated/ai/index.ts +15 -0
  100. package/src/opinionated/ai/inline-citation.tsx +30 -0
  101. package/src/opinionated/ai/message.tsx +26 -0
  102. package/src/opinionated/ai/plan.tsx +18 -0
  103. package/src/opinionated/ai/prompt-input.tsx +29 -0
  104. package/src/opinionated/ai/reasoning.tsx +9 -0
  105. package/src/opinionated/ai/shimmer.tsx +4 -0
  106. package/src/opinionated/ai/sources.tsx +10 -0
  107. package/src/opinionated/ai/suggestion.tsx +6 -0
  108. package/src/opinionated/ai/task.tsx +12 -0
  109. package/src/opinionated/ai/tool.tsx +14 -0
  110. package/src/opinionated/alert-dialog.tsx +143 -0
  111. package/src/opinionated/alert.tsx +99 -0
  112. package/src/opinionated/aspect-ratio.tsx +12 -0
  113. package/src/opinionated/avatar.tsx +59 -0
  114. package/src/opinionated/badge.tsx +33 -0
  115. package/src/opinionated/breadcrumb.tsx +99 -0
  116. package/src/opinionated/bubble.tsx +9 -0
  117. package/src/opinionated/button.tsx +59 -0
  118. package/src/opinionated/calendar.tsx +20 -0
  119. package/src/opinionated/card.tsx +83 -0
  120. package/src/opinionated/carousel.tsx +99 -0
  121. package/src/opinionated/checkbox.tsx +11 -0
  122. package/src/opinionated/code-block.tsx +12 -0
  123. package/src/opinionated/collapsible.tsx +123 -0
  124. package/src/opinionated/combobox.tsx +123 -0
  125. package/src/opinionated/container.tsx +12 -0
  126. package/src/opinionated/dialog.tsx +124 -0
  127. package/src/opinionated/direction.tsx +6 -0
  128. package/src/opinionated/divider.tsx +13 -0
  129. package/src/opinionated/dropdown-menu.tsx +131 -0
  130. package/src/opinionated/dropzone.tsx +39 -0
  131. package/src/opinionated/empty.tsx +191 -0
  132. package/src/opinionated/field.tsx +26 -0
  133. package/src/opinionated/form-calendar-popover.tsx +138 -0
  134. package/src/opinionated/form-checkbox.tsx +107 -0
  135. package/src/opinionated/form-combobox.tsx +181 -0
  136. package/src/opinionated/form-date-of-birth.tsx +193 -0
  137. package/src/opinionated/form-dropzone.tsx +126 -0
  138. package/src/opinionated/form-input-group.tsx +184 -0
  139. package/src/opinionated/form-input.tsx +118 -0
  140. package/src/opinionated/form-multi-combobox.tsx +213 -0
  141. package/src/opinionated/form-multi-dropzone.tsx +141 -0
  142. package/src/opinionated/form-radiogroup.tsx +116 -0
  143. package/src/opinionated/form-select.tsx +176 -0
  144. package/src/opinionated/form-slider.tsx +100 -0
  145. package/src/opinionated/form-switch.tsx +117 -0
  146. package/src/opinionated/form-textarea.tsx +79 -0
  147. package/src/opinionated/grid-overlay.tsx +11 -0
  148. package/src/opinionated/grid.tsx +14 -0
  149. package/src/opinionated/hover-card.tsx +59 -0
  150. package/src/opinionated/icon.tsx +14 -0
  151. package/src/opinionated/input-group.tsx +20 -0
  152. package/src/opinionated/input.tsx +11 -0
  153. package/src/opinionated/item.tsx +182 -0
  154. package/src/opinionated/kbd.tsx +16 -0
  155. package/src/opinionated/label.tsx +11 -0
  156. package/src/opinionated/layout-bento.tsx +29 -0
  157. package/src/opinionated/layout.tsx +26 -0
  158. package/src/opinionated/list.tsx +94 -0
  159. package/src/opinionated/marker.tsx +7 -0
  160. package/src/opinionated/message.tsx +8 -0
  161. package/src/opinionated/native-select.tsx +51 -0
  162. package/src/opinionated/pagination.tsx +204 -0
  163. package/src/opinionated/popover.tsx +68 -0
  164. package/src/opinionated/progress.tsx +48 -0
  165. package/src/opinionated/radio-group.tsx +13 -0
  166. package/src/opinionated/resizable.tsx +2 -0
  167. package/src/opinionated/scroll-area.tsx +82 -0
  168. package/src/opinionated/select.tsx +101 -0
  169. package/src/opinionated/separator.tsx +11 -0
  170. package/src/opinionated/sheet.tsx +120 -0
  171. package/src/opinionated/sidebar.tsx +68 -0
  172. package/src/opinionated/skeleton.tsx +246 -0
  173. package/src/opinionated/slider.tsx +11 -0
  174. package/src/opinionated/sonner.tsx +15 -0
  175. package/src/opinionated/spacer.tsx +11 -0
  176. package/src/opinionated/stack.tsx +18 -0
  177. package/src/opinionated/switch.tsx +11 -0
  178. package/src/opinionated/table.tsx +11 -0
  179. package/src/opinionated/tabs.tsx +68 -0
  180. package/src/opinionated/textarea.tsx +11 -0
  181. package/src/opinionated/theme-injector.tsx +31 -0
  182. package/src/opinionated/toggle-group.tsx +63 -0
  183. package/src/opinionated/toggle.tsx +75 -0
  184. package/src/opinionated/token-config-panel/color-editor.tsx +85 -0
  185. package/src/opinionated/token-config-panel/helpers.ts +31 -0
  186. package/src/opinionated/token-config-panel/index.tsx +398 -0
  187. package/src/opinionated/token-config-panel/json-import-export.tsx +89 -0
  188. package/src/opinionated/token-config-panel/preset-manager.ts +31 -0
  189. package/src/opinionated/token-config-panel/presets-data.ts +23 -0
  190. package/src/opinionated/token-config-panel/spacing-editor.tsx +325 -0
  191. package/src/opinionated/token-config-panel/types.ts +39 -0
  192. package/src/opinionated/token-config-panel/typography-editor.tsx +145 -0
  193. package/src/opinionated/tooltip.tsx +62 -0
  194. package/src/opinionated/typography.tsx +7 -0
  195. package/src/scripts/lint-no-arbitrary.ts +155 -0
  196. package/src/styles/composable.css +68 -0
  197. package/src/styles/presets/brutalist.css +298 -0
  198. package/src/styles/presets/default.css +298 -0
  199. package/src/styles/presets/signal-pop.css +369 -0
  200. package/src/styles/presets-data/brutalist.ts +325 -0
  201. package/src/styles/presets-data/default.ts +315 -0
  202. package/src/styles/presets-data/index.ts +45 -0
  203. package/src/styles/presets-data/signal-pop.ts +393 -0
  204. package/src/styles/tokens/base.css +71 -0
  205. package/src/styles/tokens/components.css +150 -0
  206. package/src/styles/tokens/palette.css +795 -0
  207. package/src/styles/tokens/registry.ts +324 -0
  208. package/src/styles/tokens/semantic.css +266 -0
  209. package/src/styles/tokens/tailwind-theme.css +247 -0
@@ -0,0 +1,193 @@
1
+ import type { VariantProps } from 'class-variance-authority'
2
+ import * as React from 'react'
3
+
4
+ import { Field, FieldContent, FieldDescription, FieldError, FieldLabel } from '../_internal/field'
5
+ import { Input } from '../_internal/input'
6
+ import { cn } from '../lib/utils'
7
+
8
+ export interface DateOfBirthValue {
9
+ day: string
10
+ month: string
11
+ year: string
12
+ }
13
+
14
+ export interface FormDateOfBirthProps {
15
+ label?: string
16
+ description?: string
17
+ error?: string | string[] | Array<{ message?: string } | undefined>
18
+ disabled?: boolean
19
+ required?: boolean
20
+ orientation?: VariantProps<typeof Field>['orientation']
21
+ className?: string
22
+ id?: string
23
+ name?: string
24
+ value?: DateOfBirthValue
25
+ onChange?: (value: DateOfBirthValue) => void
26
+ 'aria-label'?: string
27
+ 'aria-labelledby'?: string
28
+ }
29
+
30
+ function FormDateOfBirth({
31
+ label,
32
+ description,
33
+ error,
34
+ disabled = false,
35
+ required = false,
36
+ orientation = 'vertical',
37
+ className,
38
+ id,
39
+ name,
40
+ value,
41
+ onChange,
42
+ 'aria-label': ariaLabel,
43
+ 'aria-labelledby': ariaLabelledBy,
44
+ }: FormDateOfBirthProps) {
45
+ const errorArray = React.useMemo(() => {
46
+ if (!error) return undefined
47
+ if (Array.isArray(error)) {
48
+ return error.map((e) => (typeof e === 'string' ? { message: e } : e))
49
+ }
50
+ return [{ message: error }]
51
+ }, [error])
52
+
53
+ const hasError = Boolean(error)
54
+ const generatedId = React.useId()
55
+ const fieldId = id || generatedId
56
+
57
+ const dayRef = React.useRef<HTMLInputElement>(null)
58
+ const monthRef = React.useRef<HTMLInputElement>(null)
59
+ const yearRef = React.useRef<HTMLInputElement>(null)
60
+
61
+ const [internalValue, setInternalValue] = React.useState<DateOfBirthValue>({
62
+ day: '',
63
+ month: '',
64
+ year: '',
65
+ })
66
+
67
+ const currentValue = value ?? internalValue
68
+
69
+ const handleChange = (field: keyof DateOfBirthValue, inputValue: string) => {
70
+ // Only allow numeric values
71
+ const numericValue = inputValue.replace(/\D/g, '')
72
+
73
+ // Enforce max length
74
+ const maxLengths = { day: 2, month: 2, year: 4 }
75
+ const truncatedValue = numericValue.slice(0, maxLengths[field])
76
+
77
+ const newValue = {
78
+ ...currentValue,
79
+ [field]: truncatedValue,
80
+ }
81
+
82
+ if (onChange) {
83
+ onChange(newValue)
84
+ } else {
85
+ setInternalValue(newValue)
86
+ }
87
+
88
+ // Auto-focus next input when max length is reached
89
+ if (truncatedValue.length === maxLengths[field]) {
90
+ if (field === 'day') {
91
+ monthRef.current?.focus()
92
+ } else if (field === 'month') {
93
+ yearRef.current?.focus()
94
+ }
95
+ }
96
+ }
97
+
98
+ const handleKeyDown = (
99
+ field: keyof DateOfBirthValue,
100
+ e: React.KeyboardEvent<HTMLInputElement>,
101
+ ) => {
102
+ // Handle backspace to move to previous input when empty
103
+ if (e.key === 'Backspace' && currentValue[field] === '') {
104
+ if (field === 'month') {
105
+ dayRef.current?.focus()
106
+ } else if (field === 'year') {
107
+ monthRef.current?.focus()
108
+ }
109
+ }
110
+ }
111
+
112
+ const inputClassName = cn(
113
+ 'text-center tabular-nums',
114
+ hasError && 'aria-invalid:border-stroke-critical aria-invalid:ring-stroke-critical/20',
115
+ )
116
+
117
+ return (
118
+ <Field orientation={orientation} className={className} data-invalid={hasError}>
119
+ {label && (
120
+ <FieldLabel htmlFor={`${fieldId}-day`}>
121
+ {label}
122
+ {required && <span className="text-danger">*</span>}
123
+ </FieldLabel>
124
+ )}
125
+ <FieldContent>
126
+ <fieldset
127
+ className="flex items-center gap-1 border-none p-0 m-0 min-w-0"
128
+ aria-label={ariaLabel || label || 'Date of birth'}
129
+ aria-labelledby={ariaLabelledBy}
130
+ >
131
+ <Input
132
+ ref={dayRef}
133
+ id={`${fieldId}-day`}
134
+ name={name ? `${name}-day` : undefined}
135
+ type="text"
136
+ inputMode="numeric"
137
+ placeholder="DD"
138
+ value={currentValue.day}
139
+ onChange={(e) => handleChange('day', e.target.value)}
140
+ onKeyDown={(e) => handleKeyDown('day', e)}
141
+ disabled={disabled}
142
+ required={required}
143
+ aria-invalid={hasError}
144
+ aria-required={required}
145
+ aria-label="Day"
146
+ className={cn(inputClassName, 'w-10')}
147
+ maxLength={2}
148
+ />
149
+ <span className="text-muted-foreground text-sm">/</span>
150
+ <Input
151
+ ref={monthRef}
152
+ id={`${fieldId}-month`}
153
+ name={name ? `${name}-month` : undefined}
154
+ type="text"
155
+ inputMode="numeric"
156
+ placeholder="MM"
157
+ value={currentValue.month}
158
+ onChange={(e) => handleChange('month', e.target.value)}
159
+ onKeyDown={(e) => handleKeyDown('month', e)}
160
+ disabled={disabled}
161
+ required={required}
162
+ aria-invalid={hasError}
163
+ aria-label="Month"
164
+ className={cn(inputClassName, 'w-10')}
165
+ maxLength={2}
166
+ />
167
+ <span className="text-muted-foreground text-sm">/</span>
168
+ <Input
169
+ ref={yearRef}
170
+ id={`${fieldId}-year`}
171
+ name={name ? `${name}-year` : undefined}
172
+ type="text"
173
+ inputMode="numeric"
174
+ placeholder="YYYY"
175
+ value={currentValue.year}
176
+ onChange={(e) => handleChange('year', e.target.value)}
177
+ onKeyDown={(e) => handleKeyDown('year', e)}
178
+ disabled={disabled}
179
+ required={required}
180
+ aria-invalid={hasError}
181
+ aria-label="Year"
182
+ className={cn(inputClassName, 'w-14')}
183
+ maxLength={4}
184
+ />
185
+ </fieldset>
186
+ {description && <FieldDescription>{description}</FieldDescription>}
187
+ {hasError && <FieldError errors={errorArray} />}
188
+ </FieldContent>
189
+ </Field>
190
+ )
191
+ }
192
+
193
+ export { FormDateOfBirth }
@@ -0,0 +1,126 @@
1
+ import type { VariantProps } from 'class-variance-authority'
2
+ import * as React from 'react'
3
+
4
+ import {
5
+ ACCEPT_PRESETS,
6
+ type Accept,
7
+ DEFAULT_ACCEPT,
8
+ DEFAULT_MAX_SIZE,
9
+ DropZone,
10
+ DropZoneArea,
11
+ DropZoneContent,
12
+ DropZoneFile,
13
+ DropZoneInput,
14
+ type FileRejection,
15
+ getRejectionMessage,
16
+ } from '../_internal/dropzone'
17
+ import { Field, FieldContent, FieldDescription, FieldError, FieldLabel } from '../_internal/field'
18
+
19
+ export interface FormDropZoneProps {
20
+ ref?: React.Ref<HTMLDivElement>
21
+ value: File | null
22
+ onChange: (file: File | null) => void
23
+ label?: string
24
+ description?: string
25
+ prompt?: string
26
+ error?: string | string[] | Array<{ message?: string } | undefined>
27
+ maxSizeBytes?: number
28
+ accept?: Accept
29
+ disabled?: boolean
30
+ required?: boolean
31
+ orientation?: VariantProps<typeof Field>['orientation']
32
+ className?: string
33
+ }
34
+
35
+ function FormDropZone({
36
+ ref,
37
+ value,
38
+ onChange,
39
+ label,
40
+ description,
41
+ prompt,
42
+ error,
43
+ maxSizeBytes = DEFAULT_MAX_SIZE,
44
+ accept = DEFAULT_ACCEPT,
45
+ disabled = false,
46
+ required = false,
47
+ orientation = 'vertical',
48
+ className,
49
+ }: FormDropZoneProps) {
50
+ const [rejectionError, setRejectionError] = React.useState<string | null>(null)
51
+
52
+ const onDrop = React.useCallback(
53
+ (acceptedFiles: File[], fileRejections: FileRejection[]) => {
54
+ if (acceptedFiles.length > 0) {
55
+ onChange(acceptedFiles[0])
56
+ setRejectionError(null)
57
+ } else if (fileRejections.length > 0) {
58
+ const code = fileRejections[0].errors[0]?.code
59
+ if (code) {
60
+ setRejectionError(getRejectionMessage(code, maxSizeBytes, accept))
61
+ }
62
+ }
63
+ },
64
+ [onChange, maxSizeBytes, accept],
65
+ )
66
+
67
+ const handleRemove = React.useCallback(() => {
68
+ onChange(null)
69
+ setRejectionError(null)
70
+ }, [onChange])
71
+
72
+ const errorArray = React.useMemo(() => {
73
+ if (error) {
74
+ if (Array.isArray(error)) {
75
+ return error.map((e) => (typeof e === 'string' ? { message: e } : e))
76
+ }
77
+ return [{ message: error }]
78
+ }
79
+ if (rejectionError) {
80
+ return [{ message: rejectionError }]
81
+ }
82
+ return undefined
83
+ }, [error, rejectionError])
84
+
85
+ const hasError = Boolean(errorArray?.length)
86
+
87
+ return (
88
+ <Field
89
+ ref={ref}
90
+ tabIndex={-1}
91
+ orientation={orientation}
92
+ className={className}
93
+ data-invalid={hasError}
94
+ >
95
+ {label && (
96
+ <FieldLabel>
97
+ {label}
98
+ {required && <span className="text-danger">*</span>}
99
+ </FieldLabel>
100
+ )}
101
+ <FieldContent>
102
+ <DropZone
103
+ onDrop={onDrop}
104
+ accept={accept}
105
+ maxSizeBytes={maxSizeBytes}
106
+ disabled={disabled}
107
+ hasError={hasError}
108
+ hasFiles={!!value}
109
+ >
110
+ <DropZoneArea>
111
+ <DropZoneInput />
112
+ {value ? (
113
+ <DropZoneFile file={value} onRemove={handleRemove} disabled={disabled} />
114
+ ) : (
115
+ <DropZoneContent description={prompt} />
116
+ )}
117
+ </DropZoneArea>
118
+ </DropZone>
119
+ {description && <FieldDescription>{description}</FieldDescription>}
120
+ {hasError && <FieldError errors={errorArray} />}
121
+ </FieldContent>
122
+ </Field>
123
+ )
124
+ }
125
+
126
+ export { FormDropZone, ACCEPT_PRESETS }
@@ -0,0 +1,184 @@
1
+ import type { VariantProps } from 'class-variance-authority'
2
+ import * as React from 'react'
3
+
4
+ import { Field, FieldContent, FieldDescription, FieldError, FieldLabel } from '../_internal/field'
5
+ import {
6
+ InputGroup,
7
+ InputGroupAddon,
8
+ InputGroupButton,
9
+ InputGroupInput,
10
+ InputGroupText,
11
+ InputGroupTextarea,
12
+ } from '../_internal/input-group'
13
+ import { type NumericInputOptions, useNumericInput } from '../hooks/use-numeric-input'
14
+
15
+ export interface FormInputGroupInputProps
16
+ extends
17
+ Omit<React.ComponentProps<'input'>, 'disabled' | 'required' | 'aria-invalid' | 'aria-required'>,
18
+ NumericInputOptions {}
19
+
20
+ export interface FormInputGroupProps {
21
+ ref?: React.Ref<HTMLInputElement>
22
+ label?: string
23
+ description?: string
24
+ error?: string | string[] | Array<{ message?: string } | undefined>
25
+ disabled?: boolean
26
+ required?: boolean
27
+ orientation?: VariantProps<typeof Field>['orientation']
28
+ className?: string
29
+ // Input props (includes numeric options)
30
+ inputProps?: FormInputGroupInputProps
31
+ // Textarea props (alternative to input)
32
+ textareaProps?: Omit<
33
+ React.ComponentProps<'textarea'>,
34
+ 'disabled' | 'required' | 'aria-invalid' | 'aria-required'
35
+ >
36
+ // Addon props
37
+ startAddon?: React.ReactNode
38
+ startAddonProps?: React.ComponentProps<typeof InputGroupAddon>
39
+ endAddon?: React.ReactNode
40
+ endAddonProps?: React.ComponentProps<typeof InputGroupAddon>
41
+ // Button props
42
+ startButton?: React.ReactNode
43
+ startButtonProps?: React.ComponentProps<typeof InputGroupButton>
44
+ endButton?: React.ReactNode
45
+ endButtonProps?: React.ComponentProps<typeof InputGroupButton>
46
+ // Text props
47
+ startText?: React.ReactNode
48
+ endText?: React.ReactNode
49
+ id?: string
50
+ }
51
+
52
+ function FormInputGroup({
53
+ ref,
54
+ label,
55
+ description,
56
+ error,
57
+ disabled = false,
58
+ required = false,
59
+ orientation = 'vertical',
60
+ className,
61
+ inputProps,
62
+ textareaProps,
63
+ startAddon,
64
+ startAddonProps,
65
+ endAddon,
66
+ endAddonProps,
67
+ startButton,
68
+ startButtonProps,
69
+ endButton,
70
+ endButtonProps,
71
+ startText,
72
+ endText,
73
+ id,
74
+ }: FormInputGroupProps) {
75
+ const errorArray = React.useMemo(() => {
76
+ if (!error) return undefined
77
+ if (Array.isArray(error)) {
78
+ return error.map((e) => (typeof e === 'string' ? { message: e } : e))
79
+ }
80
+ return [{ message: error }]
81
+ }, [error])
82
+
83
+ const hasError = Boolean(error)
84
+ const generatedId = React.useId()
85
+ const inputId = id || generatedId
86
+ const isTextarea = Boolean(textareaProps)
87
+
88
+ // Extract numeric options from inputProps
89
+ const {
90
+ numericMode,
91
+ numericValue,
92
+ onNumericChange,
93
+ maxDecimalPlaces,
94
+ value,
95
+ onChange,
96
+ onBlur,
97
+ onFocus,
98
+ type,
99
+ ...restInputProps
100
+ } = inputProps ?? {}
101
+
102
+ const { inputProps: numericInputProps } = useNumericInput({
103
+ numericMode,
104
+ numericValue,
105
+ onNumericChange,
106
+ maxDecimalPlaces,
107
+ value,
108
+ onChange,
109
+ onBlur,
110
+ onFocus,
111
+ type,
112
+ })
113
+
114
+ return (
115
+ <Field orientation={orientation} className={className} data-invalid={hasError}>
116
+ {label && (
117
+ <FieldLabel htmlFor={inputId}>
118
+ {label}
119
+ {required && <span className="text-danger">*</span>}
120
+ </FieldLabel>
121
+ )}
122
+ <FieldContent>
123
+ <InputGroup data-invalid={hasError}>
124
+ {startAddon && (
125
+ <InputGroupAddon align="inline-start" {...startAddonProps}>
126
+ {startAddon}
127
+ </InputGroupAddon>
128
+ )}
129
+ {startButton && (
130
+ <InputGroupAddon align="inline-start" {...startAddonProps}>
131
+ <InputGroupButton {...startButtonProps}>{startButton}</InputGroupButton>
132
+ </InputGroupAddon>
133
+ )}
134
+ {startText && (
135
+ <InputGroupAddon align="inline-start" {...startAddonProps}>
136
+ <InputGroupText>{startText}</InputGroupText>
137
+ </InputGroupAddon>
138
+ )}
139
+ {isTextarea ? (
140
+ <InputGroupTextarea
141
+ ref={ref as React.Ref<HTMLTextAreaElement>}
142
+ id={inputId}
143
+ disabled={disabled}
144
+ required={required}
145
+ aria-invalid={hasError}
146
+ aria-required={required}
147
+ {...textareaProps}
148
+ />
149
+ ) : (
150
+ <InputGroupInput
151
+ ref={ref}
152
+ id={inputId}
153
+ disabled={disabled}
154
+ required={required}
155
+ aria-invalid={hasError}
156
+ aria-required={required}
157
+ {...restInputProps}
158
+ {...numericInputProps}
159
+ />
160
+ )}
161
+ {endText && (
162
+ <InputGroupAddon align="inline-end" {...endAddonProps}>
163
+ <InputGroupText>{endText}</InputGroupText>
164
+ </InputGroupAddon>
165
+ )}
166
+ {endButton && (
167
+ <InputGroupAddon align="inline-end" {...endAddonProps}>
168
+ <InputGroupButton {...endButtonProps}>{endButton}</InputGroupButton>
169
+ </InputGroupAddon>
170
+ )}
171
+ {endAddon && (
172
+ <InputGroupAddon align="inline-end" {...endAddonProps}>
173
+ {endAddon}
174
+ </InputGroupAddon>
175
+ )}
176
+ </InputGroup>
177
+ {description && <FieldDescription>{description}</FieldDescription>}
178
+ {hasError && <FieldError errors={errorArray} />}
179
+ </FieldContent>
180
+ </Field>
181
+ )
182
+ }
183
+
184
+ export { FormInputGroup }
@@ -0,0 +1,118 @@
1
+ import type { VariantProps } from 'class-variance-authority'
2
+ import * as React from 'react'
3
+
4
+ import { Field, FieldContent, FieldDescription, FieldError, FieldLabel } from '../_internal/field'
5
+ import { Input } from '../_internal/input'
6
+ import { type NumericInputOptions, useNumericInput } from '../hooks/use-numeric-input'
7
+
8
+ /**
9
+ * Props for the opinionated FormInput component.
10
+ *
11
+ * Wraps an HTML input with a Field (label + description + error), handles
12
+ * accessibility wiring, and supports a numeric mode for formatted number entry.
13
+ *
14
+ * @example
15
+ * ```tsx
16
+ * // Basic text input
17
+ * <FormInput label="Email" type="email" placeholder="you@example.com" />
18
+ *
19
+ * // With validation error
20
+ * <FormInput label="Name" error="Name is required" required />
21
+ *
22
+ * // Numeric mode (formatted currency input)
23
+ * <FormInput
24
+ * label="Amount"
25
+ * numericMode="decimal"
26
+ * numericValue={amount}
27
+ * onNumericChange={setAmount}
28
+ * maxDecimalPlaces={2}
29
+ * />
30
+ * ```
31
+ */
32
+ export interface FormInputProps
33
+ extends Omit<React.ComponentProps<'input'>, 'size'>, NumericInputOptions {
34
+ /** Label text rendered above the input. */
35
+ label?: string
36
+ /** Helper text rendered below the input. */
37
+ description?: string
38
+ /** Error message(s). Accepts a string, array of strings, or react-hook-form error array. */
39
+ error?: string | string[] | Array<{ message?: string } | undefined>
40
+ /** Disables the input. @default false */
41
+ disabled?: boolean
42
+ /** Marks the field as required (shows asterisk + aria-required). @default false */
43
+ required?: boolean
44
+ /** Label/input layout direction. @default 'vertical' */
45
+ orientation?: VariantProps<typeof Field>['orientation']
46
+ className?: string
47
+ }
48
+
49
+ function FormInput({
50
+ label,
51
+ description,
52
+ error,
53
+ disabled = false,
54
+ required = false,
55
+ orientation = 'vertical',
56
+ className,
57
+ id,
58
+ numericMode,
59
+ numericValue,
60
+ onNumericChange,
61
+ maxDecimalPlaces,
62
+ value,
63
+ onChange,
64
+ onBlur,
65
+ onFocus,
66
+ type,
67
+ ...restInputProps
68
+ }: FormInputProps) {
69
+ const errorArray = React.useMemo(() => {
70
+ if (!error) return undefined
71
+ if (Array.isArray(error)) {
72
+ return error.map((e) => (typeof e === 'string' ? { message: e } : e))
73
+ }
74
+ return [{ message: error }]
75
+ }, [error])
76
+
77
+ const hasError = Boolean(error)
78
+ const generatedId = React.useId()
79
+ const inputId = id || generatedId
80
+
81
+ const { inputProps: numericInputProps } = useNumericInput({
82
+ numericMode,
83
+ numericValue,
84
+ onNumericChange,
85
+ maxDecimalPlaces,
86
+ value,
87
+ onChange,
88
+ onBlur,
89
+ onFocus,
90
+ type,
91
+ })
92
+
93
+ return (
94
+ <Field orientation={orientation} className={className} data-invalid={hasError}>
95
+ {label && (
96
+ <FieldLabel htmlFor={inputId}>
97
+ {label}
98
+ {required && <span className="text-danger">*</span>}
99
+ </FieldLabel>
100
+ )}
101
+ <FieldContent>
102
+ <Input
103
+ {...restInputProps}
104
+ {...numericInputProps}
105
+ id={inputId}
106
+ disabled={disabled}
107
+ required={required}
108
+ aria-invalid={hasError}
109
+ aria-required={required}
110
+ />
111
+ {description && <FieldDescription>{description}</FieldDescription>}
112
+ {hasError && <FieldError errors={errorArray} />}
113
+ </FieldContent>
114
+ </Field>
115
+ )
116
+ }
117
+
118
+ export { FormInput }