@cognitap/radiant 0.0.50 → 0.0.52

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.
package/src/Combobox.tsx CHANGED
@@ -21,6 +21,7 @@ import { useArrayFormBridge } from './react/useArrayFormBridge.js'
21
21
  import { useField, useFieldControl } from './Field.js'
22
22
  import type { DistributiveOmit } from './internal/types.js'
23
23
  import { cn } from './cn.js'
24
+ import { joinIds } from './internal/ids.js'
24
25
  import { controlMetrics } from './controlStyles.js'
25
26
  import {
26
27
  comboboxInputClass,
@@ -92,6 +93,7 @@ export type ComboboxMultipleState = Omit<ComboboxState, 'value' | 'setValue'> &
92
93
  type Context = Omit<ComboboxState, 'value' | 'setValue'> & {
93
94
  value: Selection
94
95
  multiple: boolean
96
+ textInput?: { name?: string; form?: string }
95
97
  readOnly: boolean
96
98
  setValue(value: Selection): void
97
99
  selected: readonly ComboboxOption[]
@@ -132,6 +134,23 @@ function useComboboxContext() {
132
134
  if (!context) throw new Error('Combobox parts must render inside Combobox.')
133
135
  return context
134
136
  }
137
+ /** Private text-mode seam; public families keep their own value contract. */
138
+ export function useAutocompleteParts() {
139
+ const box = useContext(ComboboxContext)
140
+ if (!box?.textInput) throw new Error('Autocomplete parts must render inside Autocomplete.')
141
+ }
142
+ function componentSlot(multiple: boolean, textMode: boolean, name: string) {
143
+ return (
144
+ joinIds(
145
+ name,
146
+ multiple
147
+ ? name.replaceAll('combobox-', 'multi-select-')
148
+ : textMode
149
+ ? name.replaceAll('combobox-', 'autocomplete-')
150
+ : undefined
151
+ ) ?? name
152
+ )
153
+ }
135
154
  const textOf = (item?: ComboboxOption) =>
136
155
  item?.textValue ?? (typeof item?.label === 'string' ? item.label : '')
137
156
  const localFilter = (item: ComboboxOption, query: string) =>
@@ -139,8 +158,18 @@ const localFilter = (item: ComboboxOption, query: string) =>
139
158
 
140
159
  /** Editable search and committed selection have independent owners; results never select themselves. */
141
160
  export function Combobox(props: ComboboxProps) {
161
+ return (
162
+ <ComboboxRoot
163
+ {...props}
164
+ textInput={undefined}
165
+ />
166
+ )
167
+ }
168
+ /** Private composition entry. Text mode edits the actual input instead of a selection carrier. */
169
+ export function ComboboxRoot(props: ComboboxProps & { textInput?: { reset: () => void } }) {
142
170
  const {
143
171
  items,
172
+ textInput,
144
173
  value,
145
174
  defaultValue,
146
175
  selectedItem,
@@ -165,6 +194,8 @@ export function Combobox(props: ComboboxProps) {
165
194
  controlSize = 'medium',
166
195
  children
167
196
  } = props
197
+ const textMode = !!textInput
198
+ if (textMode && multiple) throw new TypeError('Autocomplete is a single text input.')
168
199
  const [initialMultiple] = useState(multiple)
169
200
  if (multiple !== initialMultiple)
170
201
  throw new TypeError('Combobox cannot change multiple mode. Remount with a new key.')
@@ -247,15 +278,17 @@ export function Combobox(props: ComboboxProps) {
247
278
  const enabled = visible.filter(item => !item.disabled)
248
279
  const activeValue = enabled.some(item => item.value === active) ? active : null
249
280
  if (active !== null && activeValue === null) highlight(null)
250
- const text = multiple
281
+ const text = textMode
251
282
  ? search.value
252
- : isOpen && (searching || search.value !== '')
283
+ : multiple
253
284
  ? search.value
254
- : selected
255
- ? textOf(selected)
256
- : typeof selection.value === 'string'
257
- ? selection.value
258
- : ''
285
+ : isOpen && (searching || search.value !== '')
286
+ ? search.value
287
+ : selected
288
+ ? textOf(selected)
289
+ : typeof selection.value === 'string'
290
+ ? selection.value
291
+ : ''
259
292
  const setOpen = (next: boolean) => {
260
293
  if (next && unavailable()) return
261
294
  expansion.request(next)
@@ -265,7 +298,7 @@ export function Combobox(props: ComboboxProps) {
265
298
  expansion.request(false)
266
299
  highlight(null)
267
300
  setSearching(false)
268
- search.request('')
301
+ if (!textMode) search.request('')
269
302
  }
270
303
  const setQuery = (next: string) => {
271
304
  if (unavailable()) return
@@ -296,6 +329,23 @@ export function Combobox(props: ComboboxProps) {
296
329
  }
297
330
  const commit = (next: string | null) => {
298
331
  if (unavailable() || (next !== null && !enabled.some(item => item.value === next))) return
332
+ if (textMode) {
333
+ const completion = next ?? ''
334
+ if (input && input.value !== completion) {
335
+ const view = input.ownerDocument.defaultView
336
+ if (view) {
337
+ const setter = Object.getOwnPropertyDescriptor(
338
+ view.HTMLInputElement.prototype,
339
+ 'value'
340
+ )!.set!
341
+ setter.call(input, completion)
342
+ input.dispatchEvent(new view.Event('input', { bubbles: true }))
343
+ }
344
+ }
345
+ dismiss()
346
+ input?.focus({ preventScroll: true })
347
+ return
348
+ }
299
349
  const item = enabled.find(item => item.value === next)
300
350
  if (item) setRetained(previous => new Map(previous).set(item.value, item))
301
351
  if (multiple) {
@@ -311,12 +361,12 @@ export function Combobox(props: ComboboxProps) {
311
361
  input?.focus({ preventScroll: true })
312
362
  }
313
363
  const nativeValue = typeof selection.value === 'string' ? selection.value : ''
314
- const latest = useRef({ selection, search, nativeValue, expansion })
364
+ const latest = useRef({ selection, search, nativeValue, expansion, textReset: textInput?.reset })
315
365
  useLayoutEffect(() => {
316
- latest.current = { selection, search, nativeValue, expansion }
366
+ latest.current = { selection, search, nativeValue, expansion, textReset: textInput?.reset }
317
367
  })
318
368
  useLayoutEffect(() => {
319
- if (multiple) return
369
+ if (multiple || textMode) return
320
370
  const node = bridge.current
321
371
  if (!node) return
322
372
  const changedByUser = pending.current && Object.is(pending.current.value, selection.value)
@@ -338,7 +388,7 @@ export function Combobox(props: ComboboxProps) {
338
388
  }
339
389
  })
340
390
  useEffect(() => {
341
- if (multiple) return
391
+ if (multiple || textMode) return
342
392
  const node = bridge.current
343
393
  const owner = node?.form
344
394
  if (!node || !owner) return
@@ -361,7 +411,29 @@ export function Combobox(props: ComboboxProps) {
361
411
  owner.removeEventListener('reset', reset)
362
412
  win.clearTimeout(timer)
363
413
  }
364
- }, [input, form, multiple])
414
+ }, [input, form, multiple, textMode])
415
+ useEffect(() => {
416
+ if (!textMode || !input) return
417
+ const view = input.ownerDocument.defaultView
418
+ if (!view) return
419
+ const roots = new Set([input.ownerDocument, input.getRootNode()])
420
+ let timer: number | undefined
421
+ const reset = (event: Event) => {
422
+ if (event.target !== input.form) return
423
+ view.clearTimeout(timer)
424
+ timer = view.setTimeout(() => {
425
+ if (event.defaultPrevented || !input.isConnected) return
426
+ latest.current.textReset?.()
427
+ highlight(null)
428
+ latest.current.expansion.request(false)
429
+ }, 0)
430
+ }
431
+ for (const root of roots) root.addEventListener('reset', reset, true)
432
+ return () => {
433
+ for (const root of roots) root.removeEventListener('reset', reset, true)
434
+ view.clearTimeout(timer)
435
+ }
436
+ }, [input, textMode])
365
437
  useLayoutEffect(() => {
366
438
  if (!isOpen || activeValue === null) return
367
439
  const option = content.current?.ownerDocument.getElementById(
@@ -385,6 +457,7 @@ export function Combobox(props: ComboboxProps) {
385
457
  })
386
458
  const context: Context = {
387
459
  multiple,
460
+ textInput: textMode ? { name, form } : undefined,
388
461
  readOnly,
389
462
  selected: selectedEntries,
390
463
  remove(value) {
@@ -442,7 +515,7 @@ export function Combobox(props: ComboboxProps) {
442
515
  {multiple && <ComboboxTags />}
443
516
  <div
444
517
  className="relative w-full min-w-0"
445
- data-slot={multiple ? 'combobox-control multi-select-control' : 'combobox-control'}
518
+ data-slot={componentSlot(multiple, textMode, 'combobox-control')}
446
519
  >
447
520
  <ComboboxInput />
448
521
  <ComboboxTrigger className="absolute inset-y-0 end-0 w-12" />
@@ -452,7 +525,7 @@ export function Combobox(props: ComboboxProps) {
452
525
  </ComboboxPortal>
453
526
  </>
454
527
  )}
455
- {multiple ? (
528
+ {textMode ? null : multiple ? (
456
529
  <>
457
530
  <input
458
531
  ref={arrayBridgeRef}
@@ -550,7 +623,9 @@ export function ComboboxInput({
550
623
  ref={inputRef}
551
624
  type="text"
552
625
  role="combobox"
553
- required={false}
626
+ name={box.textInput?.name}
627
+ form={box.textInput?.form}
628
+ required={box.textInput ? control.required : false}
554
629
  readOnly={box.readOnly || props.readOnly}
555
630
  autoComplete={props.autoComplete ?? 'off'}
556
631
  value={box.text}
@@ -559,7 +634,7 @@ export function ComboboxInput({
559
634
  controlMetrics({ controlSize: box.controlSize }),
560
635
  className
561
636
  )}
562
- data-slot={box.multiple ? 'combobox-input multi-select-input' : 'combobox-input'}
637
+ data-slot={componentSlot(box.multiple, !!box.textInput, 'combobox-input')}
563
638
  data-control-size={box.controlSize}
564
639
  aria-autocomplete="list"
565
640
  aria-expanded={box.open}
@@ -602,7 +677,13 @@ export function ComboboxInput({
602
677
  box.remove(box.value.at(-1)!)
603
678
  return
604
679
  }
605
- if (!box.multiple && event.key === 'Escape' && !box.open && box.value !== null) {
680
+ if (
681
+ !box.textInput &&
682
+ !box.multiple &&
683
+ event.key === 'Escape' &&
684
+ !box.open &&
685
+ box.value !== null
686
+ ) {
606
687
  event.preventDefault()
607
688
  box.commit(null)
608
689
  return
@@ -613,6 +694,10 @@ export function ComboboxInput({
613
694
  box.dismiss()
614
695
  return
615
696
  }
697
+ if (box.textInput && event.key === 'Enter' && box.open && box.active === null) {
698
+ box.dismiss()
699
+ return
700
+ }
616
701
  if (event.key === 'Tab') {
617
702
  box.dismiss()
618
703
  return
@@ -692,12 +777,10 @@ export function ComboboxTrigger({
692
777
  return render ? render(native) : <button {...native} />
693
778
  }}
694
779
  tabIndex={-1}
695
- aria-label={props['aria-label'] ?? 'Show options'}
780
+ aria-label={props['aria-label'] ?? (box.textInput ? 'Show suggestions' : 'Show options')}
696
781
  aria-controls={box.open ? box.listId : undefined}
697
782
  aria-expanded={box.open}
698
- data-slot={
699
- box.multiple ? 'button combobox-trigger multi-select-trigger' : 'button combobox-trigger'
700
- }
783
+ data-slot={componentSlot(box.multiple, !!box.textInput, 'button combobox-trigger')}
701
784
  onPointerDown={event => {
702
785
  onPointerDown?.(event)
703
786
  if (!event.defaultPrevented) event.preventDefault()
@@ -739,7 +822,12 @@ export function ComboboxClear({
739
822
  ...props
740
823
  }: ComboboxClearProps) {
741
824
  const box = useComboboxContext()
742
- if (box.value === null || (Array.isArray(box.value) && !box.value.length)) return null
825
+ if (
826
+ box.textInput
827
+ ? box.query === ''
828
+ : box.value === null || (Array.isArray(box.value) && !box.value.length)
829
+ )
830
+ return null
743
831
  return (
744
832
  <Button
745
833
  variant="ghost"
@@ -755,10 +843,8 @@ export function ComboboxClear({
755
843
  return render ? render(native) : <button {...native} />
756
844
  }}
757
845
  tabIndex={box.multiple ? 0 : -1}
758
- aria-label={props['aria-label'] ?? 'Clear selection'}
759
- data-slot={
760
- box.multiple ? 'button combobox-clear multi-select-clear' : 'button combobox-clear'
761
- }
846
+ aria-label={props['aria-label'] ?? (box.textInput ? 'Clear text' : 'Clear selection')}
847
+ data-slot={componentSlot(box.multiple, !!box.textInput, 'button combobox-clear')}
762
848
  onPointerDown={event => {
763
849
  onPointerDown?.(event)
764
850
  if (!event.defaultPrevented) event.preventDefault()
@@ -821,7 +907,7 @@ export function ComboboxContent({
821
907
  {...props}
822
908
  ref={transitionRef}
823
909
  popover="manual"
824
- data-slot={box.multiple ? 'combobox-content multi-select-content' : 'combobox-content'}
910
+ data-slot={componentSlot(box.multiple, !!box.textInput, 'combobox-content')}
825
911
  data-motion={motion}
826
912
  data-state={box.open ? 'open' : 'closed'}
827
913
  className={cn(
@@ -840,7 +926,7 @@ export function ComboboxContent({
840
926
  aria-label={props['aria-label'] ?? box.input?.getAttribute('aria-label') ?? 'Suggestions'}
841
927
  aria-labelledby={props['aria-label'] ? undefined : box.input?.labels?.[0]?.id}
842
928
  aria-busy={box.loading || undefined}
843
- data-slot={box.multiple ? 'combobox-listbox multi-select-listbox' : 'combobox-listbox'}
929
+ data-slot={componentSlot(box.multiple, !!box.textInput, 'combobox-listbox')}
844
930
  className="max-h-[calc(var(--_rad-combobox-max-height,100dvh)-0.5rem)] overflow-y-auto overscroll-contain"
845
931
  >
846
932
  {children ??
@@ -880,7 +966,7 @@ export function ComboboxItem({
880
966
  onKeyDown={props.onKeyDown}
881
967
  aria-selected={box.multiple ? isItemOpen(box.value, value) : box.active === value}
882
968
  aria-disabled={item.disabled || undefined}
883
- data-slot={box.multiple ? 'combobox-item multi-select-item' : 'combobox-item'}
969
+ data-slot={componentSlot(box.multiple, !!box.textInput, 'combobox-item')}
884
970
  data-value={value}
885
971
  data-highlighted={box.active === value ? '' : undefined}
886
972
  data-selected={isItemOpen(box.value, value) ? '' : undefined}
@@ -925,7 +1011,7 @@ export function ComboboxStatus({ children, className, ...props }: ComboboxStatus
925
1011
  role="status"
926
1012
  aria-live="polite"
927
1013
  aria-atomic="true"
928
- data-slot={box.multiple ? 'combobox-status multi-select-status' : 'combobox-status'}
1014
+ data-slot={componentSlot(box.multiple, !!box.textInput, 'combobox-status')}
929
1015
  className={cn('px-3 py-2 empty:hidden', className)}
930
1016
  >
931
1017
  {box.open ? (children ?? box.status) : null}
package/src/Field.tsx CHANGED
@@ -174,9 +174,15 @@ export function Field(props: FieldProps) {
174
174
  const generatedId = useId()
175
175
  const controlId = rest.controlId ?? generatedId
176
176
  const labelId = `${generatedId}-label`
177
- const invalid = hasContent(error)
177
+ const hasError = hasContent(error)
178
+ const invalid =
179
+ hasError ||
180
+ (layout === 'control' &&
181
+ ariaInvalid != null &&
182
+ ariaInvalid !== false &&
183
+ ariaInvalid !== 'false')
178
184
  const descriptionId = hasContent(description) ? `${generatedId}-description` : undefined
179
- const errorId = invalid ? `${generatedId}-error` : undefined
185
+ const errorId = hasError ? `${generatedId}-error` : undefined
180
186
  const describedBy = joinIds(descriptionId, errorId)
181
187
  const controlProps = definedProps({
182
188
  id: controlId,
@@ -184,7 +190,7 @@ export function Field(props: FieldProps) {
184
190
  disabled: disabled || undefined,
185
191
  'aria-invalid': invalid || undefined,
186
192
  'aria-required': required || undefined,
187
- 'aria-describedby': describedBy
193
+ 'aria-describedby': joinIds(describedBy, ariaDescribedBy)
188
194
  })
189
195
  const state: FieldState = {
190
196
  layout,
@@ -215,9 +221,8 @@ export function Field(props: FieldProps) {
215
221
  </span>
216
222
  )
217
223
  const rootProps = {
218
- 'aria-describedby':
219
- layout === 'group' ? joinIds(describedBy, ariaDescribedBy) : ariaDescribedBy,
220
- 'aria-invalid': layout === 'group' && invalid ? true : ariaInvalid,
224
+ 'aria-describedby': layout === 'group' ? joinIds(describedBy, ariaDescribedBy) : undefined,
225
+ 'aria-invalid': layout === 'group' ? (invalid ? true : ariaInvalid) : undefined,
221
226
  className: cn(fieldVariants({ layout }), className),
222
227
  'data-disabled': disabled ? '' : undefined,
223
228
  'data-invalid': invalid ? '' : undefined,
@@ -1,6 +1,14 @@
1
1
  'use client'
2
2
 
3
- import { createContext, useCallback, useContext, useLayoutEffect, useRef, useState } from 'react'
3
+ import {
4
+ createContext,
5
+ useCallback,
6
+ useContext,
7
+ useId,
8
+ useLayoutEffect,
9
+ useRef,
10
+ useState
11
+ } from 'react'
4
12
  import type { ChangeEvent, ComponentProps, ReactNode } from 'react'
5
13
  import { cva } from 'class-variance-authority'
6
14
  import { cn } from './cn.js'
@@ -70,8 +78,8 @@ export type RadioGroupProps = Omit<
70
78
  'layout' | 'children' | 'onChange' | 'defaultValue' | 'parts'
71
79
  > &
72
80
  ValueOwner<string | null> & {
73
- /** Stable native group name. Options supply distinct string values, including ''. */
74
- name: string
81
+ /** Native group name, and the form data key. Defaults to a generated name. */
82
+ name?: string
75
83
  /** Shared Input/Checkbox/Radio metrics; each option may override them. */
76
84
  controlSize?: ControlSize
77
85
  parts?: NonNullable<FieldProps['parts']> & { options?: PartStyle }
@@ -83,7 +91,7 @@ export type RadioGroupProps = Omit<
83
91
 
84
92
  export type RadioProps = Omit<
85
93
  ComponentProps<'input'>,
86
- 'type' | 'value' | 'checked' | 'defaultChecked' | 'style'
94
+ 'type' | 'value' | 'checked' | 'defaultChecked' | 'readOnly' | 'style'
87
95
  > &
88
96
  CheckedOwner & {
89
97
  /** Convenience label. Without it, provide an accessible name on the native input. */
@@ -113,6 +121,7 @@ export function RadioGroup({
113
121
  parts,
114
122
  ...fieldProps
115
123
  }: RadioGroupProps) {
124
+ const generatedName = useId()
116
125
  const controlled = useNativeOwner('RadioGroup', value, defaultValue)
117
126
  const [initialDefault] = useState(defaultValue)
118
127
  const { options: optionsPart, ...fieldParts } = parts ?? {}
@@ -127,7 +136,7 @@ export function RadioGroup({
127
136
  >
128
137
  <RadioGroupContext.Provider
129
138
  value={{
130
- name,
139
+ name: name ?? generatedName,
131
140
  value,
132
141
  defaultValue: initialDefault,
133
142
  controlled,
package/src/index.ts CHANGED
@@ -594,3 +594,22 @@ export {
594
594
  type NavLinkGroupTriggerProps,
595
595
  type NavLinkGroupContentProps
596
596
  } from './NavLink.js'
597
+ export {
598
+ Autocomplete,
599
+ AutocompleteInput,
600
+ AutocompleteTrigger,
601
+ AutocompleteClear,
602
+ AutocompletePortal,
603
+ AutocompleteContent,
604
+ AutocompleteItem,
605
+ AutocompleteStatus,
606
+ type AutocompleteProps,
607
+ type AutocompleteOption,
608
+ type AutocompleteInputProps,
609
+ type AutocompleteTriggerProps,
610
+ type AutocompleteClearProps,
611
+ type AutocompletePortalProps,
612
+ type AutocompleteContentProps,
613
+ type AutocompleteItemProps,
614
+ type AutocompleteStatusProps
615
+ } from './Autocomplete.js'