@dinachi/cli 0.12.0 → 1.0.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.
- package/CHANGELOG.md +117 -0
- package/dist/index.js +73 -7
- package/package.json +1 -1
- package/templates/accordion/accordion.tsx +66 -12
- package/templates/agent/agent.tsx +1865 -0
- package/templates/agent/index.ts +52 -0
- package/templates/alert-dialog/alert-dialog.tsx +46 -17
- package/templates/alert-dialog/index.ts +2 -0
- package/templates/animated-tabs/animated-tabs.tsx +65 -5
- package/templates/autocomplete/autocomplete.tsx +174 -18
- package/templates/carousel/carousel.tsx +102 -24
- package/templates/checkbox/checkbox.tsx +60 -4
- package/templates/checkbox-group/checkbox-group.tsx +82 -8
- package/templates/collapsible/collapsible.tsx +13 -4
- package/templates/combobox/combobox.tsx +297 -23
- package/templates/compare-slider/compare-slider.tsx +103 -28
- package/templates/compare-slider/index.ts +1 -1
- package/templates/context-menu/context-menu.tsx +37 -15
- package/templates/dialog/dialog.tsx +41 -12
- package/templates/drawer/drawer.tsx +18 -3
- package/templates/expandable-card/expandable-card.tsx +65 -42
- package/templates/field/field.tsx +2 -0
- package/templates/hold-to-confirm/hold-to-confirm.tsx +99 -16
- package/templates/hold-to-confirm/index.ts +1 -1
- package/templates/input/input.tsx +59 -1
- package/templates/menu/menu.tsx +26 -9
- package/templates/menubar/menubar.tsx +26 -8
- package/templates/navigation-menu/navigation-menu.tsx +67 -23
- package/templates/number-field/index.ts +1 -0
- package/templates/number-field/number-field.tsx +71 -2
- package/templates/otp-field/otp-field.tsx +57 -11
- package/templates/popover/popover.tsx +18 -3
- package/templates/preview-card/preview-card.tsx +16 -2
- package/templates/radio/radio.tsx +71 -6
- package/templates/select/select.tsx +223 -10
- package/templates/slider/slider.tsx +77 -8
- package/templates/sortable/sortable.tsx +76 -9
- package/templates/swipeable-row/index.ts +1 -1
- package/templates/swipeable-row/swipeable-row.tsx +125 -41
- package/templates/switch/switch.tsx +41 -4
- package/templates/tabs/tabs.tsx +74 -10
- package/templates/textarea/index.ts +1 -1
- package/templates/textarea/textarea.tsx +70 -4
- package/templates/toast/toast.tsx +3 -1
- package/templates/toggle/toggle.tsx +39 -7
- package/templates/toggle-group/toggle-group.tsx +96 -18
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
export {
|
|
2
|
+
AgentProvider,
|
|
3
|
+
createAgent,
|
|
4
|
+
createAgentChangeDetails,
|
|
5
|
+
isAgentChange,
|
|
6
|
+
useAgent,
|
|
7
|
+
useAgentControl,
|
|
8
|
+
useAgentState,
|
|
9
|
+
useAgentOpen,
|
|
10
|
+
useHasAgent,
|
|
11
|
+
useMergedRef,
|
|
12
|
+
waitForAgent,
|
|
13
|
+
registerTools,
|
|
14
|
+
agentToolsFor,
|
|
15
|
+
useAgentAction,
|
|
16
|
+
runAgentTool,
|
|
17
|
+
agentTools,
|
|
18
|
+
changeAsAgent,
|
|
19
|
+
matchAgentChoice,
|
|
20
|
+
sameAgentValue,
|
|
21
|
+
agentChecked,
|
|
22
|
+
agentNodeText,
|
|
23
|
+
agentChoicesOf,
|
|
24
|
+
agentRound,
|
|
25
|
+
useAgentItem,
|
|
26
|
+
useAgentLatest,
|
|
27
|
+
AgentGroupContext,
|
|
28
|
+
} from "./agent"
|
|
29
|
+
export type {
|
|
30
|
+
Agent,
|
|
31
|
+
AgentAction,
|
|
32
|
+
AgentActionDefinition,
|
|
33
|
+
AgentActionInfo,
|
|
34
|
+
AgentApproval,
|
|
35
|
+
AgentChoice,
|
|
36
|
+
AgentGroup,
|
|
37
|
+
AgentItems,
|
|
38
|
+
AgentOpenParts,
|
|
39
|
+
AgentActResult,
|
|
40
|
+
AgentAllow,
|
|
41
|
+
AgentComponent,
|
|
42
|
+
AgentComponentKind,
|
|
43
|
+
AgentControl,
|
|
44
|
+
AgentOptions,
|
|
45
|
+
AgentQuery,
|
|
46
|
+
AgentStep,
|
|
47
|
+
AgentStepResult,
|
|
48
|
+
AgentToolName,
|
|
49
|
+
AgentToolDefinition,
|
|
50
|
+
AgentToolOptions,
|
|
51
|
+
RegisteredAction,
|
|
52
|
+
} from "./agent"
|
|
@@ -3,9 +3,27 @@
|
|
|
3
3
|
import * as React from "react"
|
|
4
4
|
import { AlertDialog as BaseAlertDialog } from "@base-ui/react/alert-dialog"
|
|
5
5
|
import { cn } from "@/lib/utils"
|
|
6
|
+
import { type AgentOpenParts, useAgentOpen, useHasAgent, useMergedRef } from "@/components/ui/agent"
|
|
6
7
|
|
|
7
|
-
const
|
|
8
|
-
|
|
8
|
+
const AlertDialogAgentContext = React.createContext<AgentOpenParts | null>(null)
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Base UI's root, holding its own open state so an agent can open and close it. A press
|
|
12
|
+
* inside needs a person's approval by default; closing it does not.
|
|
13
|
+
*/
|
|
14
|
+
function AlertDialog<Payload>({ open, defaultOpen, onOpenChange, ...props }: BaseAlertDialog.Root.Props<Payload>) {
|
|
15
|
+
const [state, change, parts] = useAgentOpen("dialog", open, defaultOpen, onOpenChange)
|
|
16
|
+
return (
|
|
17
|
+
<AlertDialogAgentContext.Provider value={useHasAgent() ? parts : null}>
|
|
18
|
+
<BaseAlertDialog.Root<Payload> {...props} open={state} onOpenChange={change} />
|
|
19
|
+
</AlertDialogAgentContext.Provider>
|
|
20
|
+
)
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function AlertDialogTrigger<Payload>({ ref, ...props }: BaseAlertDialog.Trigger.Props<Payload> & React.RefAttributes<HTMLElement>) {
|
|
24
|
+
const parts = React.useContext(AlertDialogAgentContext)
|
|
25
|
+
return <BaseAlertDialog.Trigger<Payload> ref={useMergedRef(ref, parts?.trigger)} {...props} />
|
|
26
|
+
}
|
|
9
27
|
const AlertDialogPortal = BaseAlertDialog.Portal
|
|
10
28
|
|
|
11
29
|
const AlertDialogBackdrop = React.forwardRef<
|
|
@@ -46,7 +64,7 @@ const AlertDialogPopup = React.forwardRef<
|
|
|
46
64
|
React.ComponentProps<typeof BaseAlertDialog.Popup>
|
|
47
65
|
>(({ className, ...props }, ref) => (
|
|
48
66
|
<BaseAlertDialog.Popup
|
|
49
|
-
ref={ref}
|
|
67
|
+
ref={useMergedRef(ref, React.useContext(AlertDialogAgentContext)?.popup)}
|
|
50
68
|
className={cn(
|
|
51
69
|
"w-[95%] max-w-lg gap-4 rounded-lg border bg-background p-6 shadow-lg",
|
|
52
70
|
// An alert dialog is not anchored to its trigger, so scaling from the centre is
|
|
@@ -92,15 +110,20 @@ AlertDialogDescription.displayName = "AlertDialogDescription"
|
|
|
92
110
|
const AlertDialogAction = React.forwardRef<
|
|
93
111
|
HTMLButtonElement,
|
|
94
112
|
React.ComponentProps<typeof BaseAlertDialog.Close>
|
|
95
|
-
>(({ className, ...props }, ref) => (
|
|
113
|
+
>(({ className, render, ...props }, ref) => (
|
|
96
114
|
<BaseAlertDialog.Close
|
|
97
115
|
ref={ref}
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
116
|
+
render={render}
|
|
117
|
+
className={
|
|
118
|
+
render
|
|
119
|
+
? className
|
|
120
|
+
: cn(
|
|
121
|
+
"inline-flex h-10 items-center justify-center rounded-md bg-primary px-4 py-2 text-sm font-medium text-primary-foreground transition-colors",
|
|
122
|
+
"hover:bg-primary/90 focus:outline-none focus:ring-2 focus:ring-ring focus:ring-offset-2",
|
|
123
|
+
"disabled:cursor-not-allowed disabled:opacity-50",
|
|
124
|
+
className,
|
|
125
|
+
)
|
|
126
|
+
}
|
|
104
127
|
{...props}
|
|
105
128
|
/>
|
|
106
129
|
))
|
|
@@ -109,15 +132,21 @@ AlertDialogAction.displayName = "AlertDialogAction"
|
|
|
109
132
|
const AlertDialogCancel = React.forwardRef<
|
|
110
133
|
HTMLButtonElement,
|
|
111
134
|
React.ComponentProps<typeof BaseAlertDialog.Close>
|
|
112
|
-
>(({ className, ...props }, ref) => (
|
|
135
|
+
>(({ className, render, ...props }, ref) => (
|
|
113
136
|
<BaseAlertDialog.Close
|
|
114
137
|
ref={ref}
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
138
|
+
data-agent-confirm="none"
|
|
139
|
+
render={render}
|
|
140
|
+
className={
|
|
141
|
+
render
|
|
142
|
+
? className
|
|
143
|
+
: cn(
|
|
144
|
+
"inline-flex h-10 items-center justify-center rounded-md border border-input bg-background px-4 py-2 text-sm font-medium shadow-sm transition-colors",
|
|
145
|
+
"hover:bg-accent hover:text-accent-foreground focus:outline-none focus:ring-2 focus:ring-ring focus:ring-offset-2",
|
|
146
|
+
"disabled:cursor-not-allowed disabled:opacity-50",
|
|
147
|
+
className,
|
|
148
|
+
)
|
|
149
|
+
}
|
|
121
150
|
{...props}
|
|
122
151
|
/>
|
|
123
152
|
))
|
|
@@ -3,6 +3,7 @@ export {
|
|
|
3
3
|
AlertDialogTrigger,
|
|
4
4
|
AlertDialogPortal,
|
|
5
5
|
AlertDialogBackdrop,
|
|
6
|
+
AlertDialogViewport,
|
|
6
7
|
AlertDialogPopup,
|
|
7
8
|
AlertDialogTitle,
|
|
8
9
|
AlertDialogDescription,
|
|
@@ -10,4 +11,5 @@ export {
|
|
|
10
11
|
AlertDialogCancel,
|
|
11
12
|
AlertDialogHeader,
|
|
12
13
|
AlertDialogFooter,
|
|
14
|
+
AlertDialogContent,
|
|
13
15
|
} from "./alert-dialog";
|
|
@@ -9,6 +9,16 @@ import {
|
|
|
9
9
|
type Transition,
|
|
10
10
|
} from "motion/react"
|
|
11
11
|
import { cn } from "@/lib/utils"
|
|
12
|
+
import {
|
|
13
|
+
agentChoicesOf,
|
|
14
|
+
changeAsAgent,
|
|
15
|
+
matchAgentChoice,
|
|
16
|
+
useAgentControl,
|
|
17
|
+
useAgentItem,
|
|
18
|
+
useHasAgent,
|
|
19
|
+
useMergedRef,
|
|
20
|
+
type AgentItems,
|
|
21
|
+
} from "@/components/ui/agent"
|
|
12
22
|
|
|
13
23
|
/** `useReducedMotion` reads the OS setting only, and only at mount. Also honour an
|
|
14
24
|
* app-level `<MotionConfig reducedMotion>`, so a user preference in the host app
|
|
@@ -51,6 +61,46 @@ interface AnimatedTabsContextValue {
|
|
|
51
61
|
value: unknown
|
|
52
62
|
layoutId: string
|
|
53
63
|
transition: Transition
|
|
64
|
+
/** Set inside an `AgentProvider`: the triggers, which the agent selects by name. */
|
|
65
|
+
items: AgentItems | null
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
type Details = BaseTabs.Root.ChangeEventDetails
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* The agent clicks the tab, so Base UI decides as it does for a person. Returns a reader
|
|
72
|
+
* for the agent's details, which the change handler takes in place of Base UI's.
|
|
73
|
+
*/
|
|
74
|
+
function useTabsAgent(items: AgentItems, element: React.RefObject<HTMLElement | null>) {
|
|
75
|
+
const pending = React.useRef<Details | null>(null)
|
|
76
|
+
useAgentControl(() => {
|
|
77
|
+
const choices = agentChoicesOf(items)
|
|
78
|
+
const tabs = [...items]
|
|
79
|
+
const active = tabs.find(([el]) => el.getAttribute("aria-selected") === "true")?.[1]
|
|
80
|
+
return {
|
|
81
|
+
kind: "tabs",
|
|
82
|
+
element: choices.length ? element.current : null,
|
|
83
|
+
value: choices.find((c) => c.value === active)?.label ?? "",
|
|
84
|
+
items: choices.map((c) => c.label),
|
|
85
|
+
disabled: choices.length > 0 && choices.every((c) => c.disabled),
|
|
86
|
+
actions: {
|
|
87
|
+
select(wanted: unknown) {
|
|
88
|
+
const match = matchAgentChoice(choices, wanted)
|
|
89
|
+
if (typeof match === "string") return match
|
|
90
|
+
return changeAsAgent<unknown, Details>((_, details) => {
|
|
91
|
+
pending.current = details
|
|
92
|
+
tabs.find(([, v]) => v === match.value)?.[0].click()
|
|
93
|
+
pending.current = null
|
|
94
|
+
}, match.value)
|
|
95
|
+
},
|
|
96
|
+
},
|
|
97
|
+
}
|
|
98
|
+
})
|
|
99
|
+
return React.useCallback(() => {
|
|
100
|
+
const details = pending.current
|
|
101
|
+
pending.current = null
|
|
102
|
+
return details
|
|
103
|
+
}, [])
|
|
54
104
|
}
|
|
55
105
|
|
|
56
106
|
const AnimatedTabsContext = React.createContext<AnimatedTabsContextValue | null>(
|
|
@@ -103,18 +153,24 @@ const AnimatedTabs = React.forwardRef<
|
|
|
103
153
|
const isControlled = valueProp !== undefined
|
|
104
154
|
const value = isControlled ? valueProp : internalValue
|
|
105
155
|
const reducedMotion = useReducedMotionConfig()
|
|
156
|
+
const element = React.useRef<HTMLDivElement | null>(null)
|
|
157
|
+
const [agentItems] = React.useState<AgentItems>(() => new Map())
|
|
158
|
+
const items = useHasAgent() ? agentItems : null
|
|
159
|
+
const takeAgentDetails = useTabsAgent(agentItems, element)
|
|
106
160
|
|
|
107
161
|
const handleValueChange = React.useCallback<
|
|
108
162
|
NonNullable<BaseRootProps["onValueChange"]>
|
|
109
163
|
>(
|
|
110
164
|
(next, eventDetails) => {
|
|
165
|
+
const details = takeAgentDetails() ?? eventDetails
|
|
166
|
+
onValueChange?.(next, details)
|
|
167
|
+
if (details.isCanceled) return eventDetails.cancel()
|
|
111
168
|
// Batched with the value change, so the render that moves the pill is
|
|
112
169
|
// already the one that knows whether it should move at all.
|
|
113
170
|
setInstant(isKeyboardActivation(eventDetails?.event))
|
|
114
171
|
if (!isControlled) setInternalValue(next)
|
|
115
|
-
onValueChange?.(next, eventDetails)
|
|
116
172
|
},
|
|
117
|
-
[isControlled, onValueChange]
|
|
173
|
+
[isControlled, onValueChange, takeAgentDetails]
|
|
118
174
|
)
|
|
119
175
|
|
|
120
176
|
const contextValue = React.useMemo<AnimatedTabsContextValue>(
|
|
@@ -122,14 +178,15 @@ const AnimatedTabs = React.forwardRef<
|
|
|
122
178
|
value,
|
|
123
179
|
layoutId,
|
|
124
180
|
transition: reducedMotion || instant ? INSTANT_TRANSITION : transition,
|
|
181
|
+
items,
|
|
125
182
|
}),
|
|
126
|
-
[value, layoutId, transition, reducedMotion, instant]
|
|
183
|
+
[value, layoutId, transition, reducedMotion, instant, items]
|
|
127
184
|
)
|
|
128
185
|
|
|
129
186
|
return (
|
|
130
187
|
<AnimatedTabsContext.Provider value={contextValue}>
|
|
131
188
|
<BaseTabs.Root
|
|
132
|
-
ref={ref}
|
|
189
|
+
ref={useMergedRef(ref, element)}
|
|
133
190
|
value={value}
|
|
134
191
|
defaultValue={defaultValue}
|
|
135
192
|
onValueChange={handleValueChange}
|
|
@@ -173,10 +230,13 @@ const AnimatedTabsTrigger = React.forwardRef<
|
|
|
173
230
|
>(({ className, indicatorClassName, children, value, ...props }, ref) => {
|
|
174
231
|
const context = useAnimatedTabsContext("AnimatedTabsTrigger")
|
|
175
232
|
const isActive = context.value === value
|
|
233
|
+
const element = React.useRef<HTMLButtonElement | null>(null)
|
|
234
|
+
useAgentItem(context.items, element, value)
|
|
176
235
|
|
|
177
236
|
return (
|
|
178
237
|
<BaseTabs.Tab
|
|
179
|
-
ref={ref}
|
|
238
|
+
ref={useMergedRef(ref, element)}
|
|
239
|
+
data-agent-part={context.items ? "" : undefined}
|
|
180
240
|
value={value}
|
|
181
241
|
className={cn(
|
|
182
242
|
"relative isolate inline-flex select-none items-center justify-center whitespace-nowrap",
|
|
@@ -4,8 +4,150 @@ import * as React from "react"
|
|
|
4
4
|
import { Autocomplete as AutocompletePrimitive } from "@base-ui/react/autocomplete"
|
|
5
5
|
import { ChevronDown, X } from "lucide-react"
|
|
6
6
|
import { cn } from "@/lib/utils"
|
|
7
|
+
import {
|
|
8
|
+
agentNodeText,
|
|
9
|
+
changeAsAgent,
|
|
10
|
+
useAgentControl,
|
|
11
|
+
useAgentState,
|
|
12
|
+
useHasAgent,
|
|
13
|
+
useMergedRef,
|
|
14
|
+
waitForAgent,
|
|
15
|
+
type AgentControl,
|
|
16
|
+
} from "@/components/ui/agent"
|
|
7
17
|
|
|
8
|
-
|
|
18
|
+
type ElementRef = React.MutableRefObject<HTMLElement | null>
|
|
19
|
+
|
|
20
|
+
interface AutocompleteAgent {
|
|
21
|
+
input: ElementRef
|
|
22
|
+
list: ElementRef
|
|
23
|
+
/** Suggestions in the open list, by element. */
|
|
24
|
+
items: Set<HTMLElement>
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const AutocompleteAgentContext = React.createContext<AutocompleteAgent | null>(null)
|
|
28
|
+
|
|
29
|
+
type ToText = ((item: never) => string) | undefined
|
|
30
|
+
|
|
31
|
+
/** Base UI's own text for an item: `itemToStringValue`, then `label`, then `value`. */
|
|
32
|
+
function textOf(item: unknown, toText: ToText): string {
|
|
33
|
+
if (toText) return (toText as (i: unknown) => string)(item) ?? ""
|
|
34
|
+
const o = item as { label?: unknown; value?: unknown }
|
|
35
|
+
return String(item && typeof item === "object" ? (o.label ?? o.value ?? "") : item)
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
const leavesOf = (data: readonly unknown[] = []) =>
|
|
39
|
+
data.flatMap((e) => ((e as { items?: unknown })?.items instanceof Array ? (e as { items: unknown[] }).items : [e]))
|
|
40
|
+
|
|
41
|
+
const liveOf = (items: Set<HTMLElement>) => Array.from(items, (el) => agentNodeText(el.textContent))
|
|
42
|
+
|
|
43
|
+
/** Waits a turn for React to filter, then for the list to be open with every item registered. */
|
|
44
|
+
async function settled({ list, items }: AutocompleteAgent) {
|
|
45
|
+
await new Promise((resolve) => setTimeout(resolve))
|
|
46
|
+
await waitForAgent(
|
|
47
|
+
() => list.current?.isConnected && list.current.querySelectorAll('[role="option"]').length === items.size
|
|
48
|
+
)
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
interface AutocompleteState {
|
|
52
|
+
s: AutocompleteAgent
|
|
53
|
+
value: string
|
|
54
|
+
open: boolean
|
|
55
|
+
catalog: string[]
|
|
56
|
+
disabled: boolean
|
|
57
|
+
change: (text: string) => string | undefined
|
|
58
|
+
setOpen: (open: boolean) => string | undefined
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function autocompleteControl(c: AutocompleteState): AgentControl {
|
|
62
|
+
const live = liveOf(c.s.items)
|
|
63
|
+
const items = live.length ? live : c.catalog
|
|
64
|
+
return {
|
|
65
|
+
kind: "combobox",
|
|
66
|
+
element: c.s.input.current,
|
|
67
|
+
value: c.value,
|
|
68
|
+
disabled: c.disabled,
|
|
69
|
+
...(items.length ? { items } : {}),
|
|
70
|
+
actions: {
|
|
71
|
+
setValue: (text: unknown) => c.change(String(text ?? "")),
|
|
72
|
+
search: async (text: unknown) => {
|
|
73
|
+
const query = String(text ?? "")
|
|
74
|
+
// Opens first, so a refusal at either step leaves the text and the list as they were.
|
|
75
|
+
const notOpened = c.open ? undefined : c.setOpen(true)
|
|
76
|
+
if (notOpened) return notOpened
|
|
77
|
+
const refused = c.change(query)
|
|
78
|
+
if (refused) {
|
|
79
|
+
if (!c.open) c.setOpen(false)
|
|
80
|
+
return refused
|
|
81
|
+
}
|
|
82
|
+
await settled(c.s)
|
|
83
|
+
return { result: liveOf(c.s.items) }
|
|
84
|
+
},
|
|
85
|
+
},
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
type RootProps = AutocompletePrimitive.Root.Props<unknown>
|
|
90
|
+
type Details = AutocompletePrimitive.Root.ChangeEventDetails
|
|
91
|
+
type OpenDetails = AutocompletePrimitive.Root.OpenChangeEventDetails
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Base UI's root, holding its text and open state inside AgentProvider so an agent can
|
|
95
|
+
* set them the way a parent would. Every change, the agent's included, goes through
|
|
96
|
+
* `onValueChange` and `onOpenChange` first, which can cancel it.
|
|
97
|
+
*/
|
|
98
|
+
function AgentAutocomplete({
|
|
99
|
+
value: valueProp,
|
|
100
|
+
defaultValue = "",
|
|
101
|
+
onValueChange,
|
|
102
|
+
open: openProp,
|
|
103
|
+
defaultOpen = false,
|
|
104
|
+
onOpenChange,
|
|
105
|
+
...props
|
|
106
|
+
}: RootProps) {
|
|
107
|
+
const [value, changeValue] = useAgentState<string, Details>(
|
|
108
|
+
valueProp == null ? undefined : String(valueProp),
|
|
109
|
+
String(defaultValue),
|
|
110
|
+
onValueChange
|
|
111
|
+
)
|
|
112
|
+
const [open, changeOpen] = useAgentState<boolean, OpenDetails>(openProp, defaultOpen, onOpenChange)
|
|
113
|
+
const [s] = React.useState<AutocompleteAgent>(() => ({
|
|
114
|
+
input: { current: null },
|
|
115
|
+
list: { current: null },
|
|
116
|
+
items: new Set(),
|
|
117
|
+
}))
|
|
118
|
+
|
|
119
|
+
useAgentControl(() =>
|
|
120
|
+
autocompleteControl({
|
|
121
|
+
s,
|
|
122
|
+
value,
|
|
123
|
+
open,
|
|
124
|
+
catalog: leavesOf(props.items).map((i) => textOf(i, props.itemToStringValue)),
|
|
125
|
+
disabled: Boolean(props.disabled),
|
|
126
|
+
change: (next) => (props.readOnly ? "The autocomplete is read-only." : changeAsAgent(changeValue, next)),
|
|
127
|
+
setOpen: (next) => changeAsAgent(changeOpen, next),
|
|
128
|
+
})
|
|
129
|
+
)
|
|
130
|
+
|
|
131
|
+
return (
|
|
132
|
+
<AutocompleteAgentContext.Provider value={s}>
|
|
133
|
+
<AutocompletePrimitive.Root
|
|
134
|
+
{...props}
|
|
135
|
+
value={value}
|
|
136
|
+
onValueChange={changeValue}
|
|
137
|
+
open={open}
|
|
138
|
+
onOpenChange={changeOpen}
|
|
139
|
+
/>
|
|
140
|
+
</AutocompleteAgentContext.Provider>
|
|
141
|
+
)
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/** Base UI's root as it is, until an AgentProvider is around it. */
|
|
145
|
+
function AutocompleteRoot(props: RootProps) {
|
|
146
|
+
if (!useHasAgent()) return <AutocompletePrimitive.Root {...props} />
|
|
147
|
+
return <AgentAutocomplete {...props} />
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
const Autocomplete = AutocompleteRoot as unknown as typeof AutocompletePrimitive.Root
|
|
9
151
|
const AutocompleteValue = AutocompletePrimitive.Value
|
|
10
152
|
const AutocompleteCollection = AutocompletePrimitive.Collection
|
|
11
153
|
const AutocompletePortal = AutocompletePrimitive.Portal
|
|
@@ -16,7 +158,7 @@ const AutocompleteInput = React.forwardRef<
|
|
|
16
158
|
React.ComponentPropsWithoutRef<typeof AutocompletePrimitive.Input>
|
|
17
159
|
>(({ className, ...props }, ref) => (
|
|
18
160
|
<AutocompletePrimitive.Input
|
|
19
|
-
ref={ref}
|
|
161
|
+
ref={useMergedRef(ref, React.useContext(AutocompleteAgentContext)?.input)}
|
|
20
162
|
className={cn(
|
|
21
163
|
"flex h-10 w-full bg-transparent px-3 py-2 text-sm",
|
|
22
164
|
"placeholder:text-muted-foreground",
|
|
@@ -105,7 +247,7 @@ const AutocompleteList = React.forwardRef<
|
|
|
105
247
|
React.ComponentPropsWithoutRef<typeof AutocompletePrimitive.List>
|
|
106
248
|
>(({ className, ...props }, ref) => (
|
|
107
249
|
<AutocompletePrimitive.List
|
|
108
|
-
ref={ref}
|
|
250
|
+
ref={useMergedRef(ref, React.useContext(AutocompleteAgentContext)?.list)}
|
|
109
251
|
className={cn("max-h-80 overflow-y-auto p-1", className)}
|
|
110
252
|
{...props}
|
|
111
253
|
/>
|
|
@@ -117,21 +259,35 @@ const AutocompleteItem = React.forwardRef<
|
|
|
117
259
|
React.ComponentPropsWithoutRef<typeof AutocompletePrimitive.Item> & {
|
|
118
260
|
inset?: boolean
|
|
119
261
|
}
|
|
120
|
-
>(({ className, children, inset, ...props }, ref) =>
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
262
|
+
>(({ className, children, inset, ...props }, ref) => {
|
|
263
|
+
const autocomplete = React.useContext(AutocompleteAgentContext)
|
|
264
|
+
const element = React.useRef<HTMLDivElement | null>(null)
|
|
265
|
+
|
|
266
|
+
React.useEffect(() => {
|
|
267
|
+
const el = element.current
|
|
268
|
+
if (!autocomplete || !el) return
|
|
269
|
+
autocomplete.items.add(el)
|
|
270
|
+
return () => {
|
|
271
|
+
autocomplete.items.delete(el)
|
|
272
|
+
}
|
|
273
|
+
}, [autocomplete])
|
|
274
|
+
|
|
275
|
+
return (
|
|
276
|
+
<AutocompletePrimitive.Item
|
|
277
|
+
ref={useMergedRef(ref, element)}
|
|
278
|
+
className={cn(
|
|
279
|
+
"relative flex cursor-default select-none items-center rounded-sm px-2 py-1.5 text-sm lg:text-base outline-none",
|
|
280
|
+
"data-highlighted:bg-accent font-normal data-highlighted:text-accent-foreground",
|
|
281
|
+
"data-disabled:pointer-events-none data-disabled:opacity-50",
|
|
282
|
+
inset && "pl-8",
|
|
283
|
+
className
|
|
284
|
+
)}
|
|
285
|
+
{...props}
|
|
286
|
+
>
|
|
287
|
+
{children}
|
|
288
|
+
</AutocompletePrimitive.Item>
|
|
289
|
+
)
|
|
290
|
+
})
|
|
135
291
|
AutocompleteItem.displayName = "AutocompleteItem"
|
|
136
292
|
|
|
137
293
|
const AutocompleteGroup = React.forwardRef<
|