@choice-ui/command 0.0.3 → 0.0.4
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/dist/index.d.ts +67 -6
- package/dist/index.js +278 -266
- package/package.json +13 -15
- package/dist/index.cjs +0 -1309
- package/dist/index.d.cts +0 -130
- package/src/command-score.ts +0 -171
- package/src/command.tsx +0 -482
- package/src/components/command-divider.tsx +0 -30
- package/src/components/command-empty.tsx +0 -30
- package/src/components/command-footer.tsx +0 -22
- package/src/components/command-group.tsx +0 -76
- package/src/components/command-input.tsx +0 -66
- package/src/components/command-item.tsx +0 -165
- package/src/components/command-list.tsx +0 -77
- package/src/components/command-loading.tsx +0 -30
- package/src/components/command-tabs.tsx +0 -20
- package/src/components/command-value.tsx +0 -23
- package/src/components/index.ts +0 -10
- package/src/context/command-context.ts +0 -5
- package/src/context/create-command-context.ts +0 -140
- package/src/context/index.ts +0 -2
- package/src/hooks/index.ts +0 -10
- package/src/hooks/use-as-ref.ts +0 -12
- package/src/hooks/use-command-state.ts +0 -18
- package/src/hooks/use-command.ts +0 -10
- package/src/hooks/use-schedule-layout-effect.ts +0 -19
- package/src/hooks/use-value.ts +0 -39
- package/src/index.ts +0 -31
- package/src/store/index.ts +0 -1
- package/src/tv.ts +0 -248
- package/src/types.ts +0 -84
- package/src/utils/constants.ts +0 -7
- package/src/utils/dom.ts +0 -19
- package/src/utils/helpers.ts +0 -45
- package/src/utils/index.ts +0 -3
package/dist/index.js
CHANGED
|
@@ -1,15 +1,69 @@
|
|
|
1
1
|
import { tcv, useLazyRef, tcx } from '@choice-ui/shared';
|
|
2
|
-
import
|
|
2
|
+
import React, { createContext, forwardRef, useId, useRef, useMemo, useCallback, memo, useEffect, useState } from 'react';
|
|
3
3
|
import { useIsomorphicLayoutEffect, useEventCallback } from 'usehooks-ts';
|
|
4
4
|
import { jsxs, jsx } from 'react/jsx-runtime';
|
|
5
5
|
import { Tabs, TabItem } from '@choice-ui/tabs';
|
|
6
|
-
import {
|
|
6
|
+
import { TextField } from '@choice-ui/text-field';
|
|
7
7
|
import { Kbd } from '@choice-ui/kbd';
|
|
8
8
|
import { ScrollArea } from '@choice-ui/scroll-area';
|
|
9
9
|
|
|
10
10
|
// src/command.tsx
|
|
11
|
+
var CommandContext = createContext(void 0);
|
|
12
|
+
var StoreContext = createContext(void 0);
|
|
13
|
+
|
|
14
|
+
// src/utils/constants.ts
|
|
15
|
+
var GROUP_SELECTOR = `[role="presentation"]`;
|
|
16
|
+
var GROUP_ITEMS_SELECTOR = `[role="group"]`;
|
|
17
|
+
var GROUP_HEADING_SELECTOR = `[aria-hidden="true"]`;
|
|
18
|
+
var ITEM_SELECTOR = `[role="option"]`;
|
|
19
|
+
var VALID_ITEM_SELECTOR = `${ITEM_SELECTOR}:not([aria-disabled="true"]):not([data-hidden="true"])`;
|
|
20
|
+
var SELECT_EVENT = `cmdk-item-select`;
|
|
21
|
+
var VALUE_ATTR = `data-value`;
|
|
22
|
+
|
|
23
|
+
// src/utils/dom.ts
|
|
24
|
+
function findNextSibling(el, selector) {
|
|
25
|
+
let sibling = el.nextElementSibling;
|
|
26
|
+
while (sibling) {
|
|
27
|
+
if (sibling.matches(selector)) return sibling;
|
|
28
|
+
sibling = sibling.nextElementSibling;
|
|
29
|
+
}
|
|
30
|
+
return null;
|
|
31
|
+
}
|
|
32
|
+
function findPreviousSibling(el, selector) {
|
|
33
|
+
let sibling = el.previousElementSibling;
|
|
34
|
+
while (sibling) {
|
|
35
|
+
if (sibling.matches(selector)) return sibling;
|
|
36
|
+
sibling = sibling.previousElementSibling;
|
|
37
|
+
}
|
|
38
|
+
return null;
|
|
39
|
+
}
|
|
40
|
+
function renderChildren(children) {
|
|
41
|
+
const childrenType = children.type;
|
|
42
|
+
if (typeof childrenType === "function") {
|
|
43
|
+
try {
|
|
44
|
+
return childrenType(children.props);
|
|
45
|
+
} catch {
|
|
46
|
+
return children;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
if (typeof childrenType === "object" && childrenType !== null && "render" in childrenType) {
|
|
50
|
+
const forwardRefComponent = childrenType;
|
|
51
|
+
return forwardRefComponent.render(children.props, null);
|
|
52
|
+
}
|
|
53
|
+
return children;
|
|
54
|
+
}
|
|
55
|
+
function SlottableWithNestedChildren({ asChild, children }, render) {
|
|
56
|
+
if (asChild && React.isValidElement(children)) {
|
|
57
|
+
const renderedChild = renderChildren(children);
|
|
58
|
+
if (React.isValidElement(renderedChild)) {
|
|
59
|
+
const childProps = children.props;
|
|
60
|
+
return React.cloneElement(renderedChild, {}, render(childProps.children));
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
return render(children);
|
|
64
|
+
}
|
|
11
65
|
|
|
12
|
-
// src/command-score.ts
|
|
66
|
+
// src/utils/command-score.ts
|
|
13
67
|
var SCORE_CONTINUE_MATCH = 1;
|
|
14
68
|
var SCORE_SPACE_WORD_JUMP = 0.9;
|
|
15
69
|
var SCORE_NON_SPACE_WORD_JUMP = 0.8;
|
|
@@ -110,8 +164,6 @@ function commandScore(string, abbreviation, aliases) {
|
|
|
110
164
|
{}
|
|
111
165
|
);
|
|
112
166
|
}
|
|
113
|
-
var CommandContext = createContext(void 0);
|
|
114
|
-
var StoreContext = createContext(void 0);
|
|
115
167
|
|
|
116
168
|
// src/context/create-command-context.ts
|
|
117
169
|
function createCommandContext(options) {
|
|
@@ -217,14 +269,14 @@ function useAsRef(data) {
|
|
|
217
269
|
return ref;
|
|
218
270
|
}
|
|
219
271
|
var useCommand = () => {
|
|
220
|
-
const context =
|
|
272
|
+
const context = React.useContext(CommandContext);
|
|
221
273
|
if (!context) {
|
|
222
274
|
throw new Error("useCommand must be used within a Command component");
|
|
223
275
|
}
|
|
224
276
|
return context;
|
|
225
277
|
};
|
|
226
278
|
var useStore = () => {
|
|
227
|
-
const store =
|
|
279
|
+
const store = React.useContext(StoreContext);
|
|
228
280
|
if (!store) {
|
|
229
281
|
throw new Error("useStore must be used within a Command component");
|
|
230
282
|
}
|
|
@@ -233,7 +285,7 @@ var useStore = () => {
|
|
|
233
285
|
function useCommandState(selector) {
|
|
234
286
|
const store = useStore();
|
|
235
287
|
const cb = () => selector(store.snapshot());
|
|
236
|
-
return
|
|
288
|
+
return React.useSyncExternalStore(store.subscribe, cb, cb);
|
|
237
289
|
}
|
|
238
290
|
var useScheduleLayoutEffect = () => {
|
|
239
291
|
const [updateCount, setUpdateCount] = useState(0);
|
|
@@ -247,65 +299,10 @@ var useScheduleLayoutEffect = () => {
|
|
|
247
299
|
setUpdateCount((prev) => prev + 1);
|
|
248
300
|
}, []);
|
|
249
301
|
};
|
|
250
|
-
|
|
251
|
-
// src/utils/constants.ts
|
|
252
|
-
var GROUP_SELECTOR = `[role="presentation"]`;
|
|
253
|
-
var GROUP_ITEMS_SELECTOR = `[role="group"]`;
|
|
254
|
-
var GROUP_HEADING_SELECTOR = `[aria-hidden="true"]`;
|
|
255
|
-
var ITEM_SELECTOR = `[role="option"]`;
|
|
256
|
-
var VALID_ITEM_SELECTOR = `${ITEM_SELECTOR}:not([aria-disabled="true"])`;
|
|
257
|
-
var SELECT_EVENT = `cmdk-item-select`;
|
|
258
|
-
var VALUE_ATTR = `data-value`;
|
|
259
|
-
|
|
260
|
-
// src/utils/dom.ts
|
|
261
|
-
function findNextSibling(el, selector) {
|
|
262
|
-
let sibling = el.nextElementSibling;
|
|
263
|
-
while (sibling) {
|
|
264
|
-
if (sibling.matches(selector)) return sibling;
|
|
265
|
-
sibling = sibling.nextElementSibling;
|
|
266
|
-
}
|
|
267
|
-
return null;
|
|
268
|
-
}
|
|
269
|
-
function findPreviousSibling(el, selector) {
|
|
270
|
-
let sibling = el.previousElementSibling;
|
|
271
|
-
while (sibling) {
|
|
272
|
-
if (sibling.matches(selector)) return sibling;
|
|
273
|
-
sibling = sibling.previousElementSibling;
|
|
274
|
-
}
|
|
275
|
-
return null;
|
|
276
|
-
}
|
|
277
|
-
var useLayoutEffect = typeof window === "undefined" ? React4.useEffect : React4.useLayoutEffect;
|
|
278
|
-
function renderChildren(children) {
|
|
279
|
-
const childrenType = children.type;
|
|
280
|
-
if (typeof childrenType === "function") {
|
|
281
|
-
try {
|
|
282
|
-
return childrenType(children.props);
|
|
283
|
-
} catch {
|
|
284
|
-
return children;
|
|
285
|
-
}
|
|
286
|
-
}
|
|
287
|
-
if (typeof childrenType === "object" && childrenType !== null && "render" in childrenType) {
|
|
288
|
-
const forwardRefComponent = childrenType;
|
|
289
|
-
return forwardRefComponent.render(children.props, null);
|
|
290
|
-
}
|
|
291
|
-
return children;
|
|
292
|
-
}
|
|
293
|
-
function SlottableWithNestedChildren({ asChild, children }, render) {
|
|
294
|
-
if (asChild && React4.isValidElement(children)) {
|
|
295
|
-
const renderedChild = renderChildren(children);
|
|
296
|
-
if (React4.isValidElement(renderedChild)) {
|
|
297
|
-
const childProps = children.props;
|
|
298
|
-
return React4.cloneElement(renderedChild, {}, render(childProps.children));
|
|
299
|
-
}
|
|
300
|
-
}
|
|
301
|
-
return render(children);
|
|
302
|
-
}
|
|
303
|
-
|
|
304
|
-
// src/hooks/use-value.ts
|
|
305
302
|
function useValue(id, ref, deps, aliases = []) {
|
|
306
303
|
const valueRef = useRef();
|
|
307
304
|
const context = useCommand();
|
|
308
|
-
|
|
305
|
+
useIsomorphicLayoutEffect(() => {
|
|
309
306
|
const value = (() => {
|
|
310
307
|
for (const part of deps) {
|
|
311
308
|
if (typeof part === "string") {
|
|
@@ -327,7 +324,7 @@ function useValue(id, ref, deps, aliases = []) {
|
|
|
327
324
|
});
|
|
328
325
|
return valueRef;
|
|
329
326
|
}
|
|
330
|
-
var GroupContext =
|
|
327
|
+
var GroupContext = React.createContext(void 0);
|
|
331
328
|
var commandTv = tcv({
|
|
332
329
|
slots: {
|
|
333
330
|
root: "flex h-full w-full flex-col overflow-hidden",
|
|
@@ -348,16 +345,13 @@ var commandTv = tcv({
|
|
|
348
345
|
});
|
|
349
346
|
var commandInputTv = tcv({
|
|
350
347
|
slots: {
|
|
351
|
-
|
|
352
|
-
input: "w-full rounded-lg"
|
|
348
|
+
input: "m-2"
|
|
353
349
|
},
|
|
354
350
|
variants: {
|
|
355
351
|
size: {
|
|
356
|
-
default: {
|
|
357
|
-
input: "text-body-medium h-8 px-2"
|
|
358
|
-
},
|
|
352
|
+
default: {},
|
|
359
353
|
large: {
|
|
360
|
-
input: "leading-lg tracking-lg
|
|
354
|
+
input: "leading-lg tracking-lg px-4 text-body-large"
|
|
361
355
|
}
|
|
362
356
|
}
|
|
363
357
|
},
|
|
@@ -384,6 +378,11 @@ var commandGroupTv = tcv({
|
|
|
384
378
|
dark: {
|
|
385
379
|
heading: "text-white/50"
|
|
386
380
|
}
|
|
381
|
+
},
|
|
382
|
+
hidden: {
|
|
383
|
+
true: {
|
|
384
|
+
root: "hidden"
|
|
385
|
+
}
|
|
387
386
|
}
|
|
388
387
|
}
|
|
389
388
|
});
|
|
@@ -427,6 +426,11 @@ var commandItemTv = tcv({
|
|
|
427
426
|
true: {
|
|
428
427
|
root: "pointer-events-none"
|
|
429
428
|
}
|
|
429
|
+
},
|
|
430
|
+
hidden: {
|
|
431
|
+
true: {
|
|
432
|
+
root: "hidden"
|
|
433
|
+
}
|
|
430
434
|
}
|
|
431
435
|
},
|
|
432
436
|
compoundVariants: [
|
|
@@ -721,8 +725,10 @@ var Command = forwardRef((props, forwardedRef) => {
|
|
|
721
725
|
state.current.filtered.groups = /* @__PURE__ */ new Set();
|
|
722
726
|
let itemCount = 0;
|
|
723
727
|
for (const id of allItems.current) {
|
|
724
|
-
const
|
|
725
|
-
|
|
728
|
+
const itemData = ids.current.get(id);
|
|
729
|
+
if (!itemData) continue;
|
|
730
|
+
const value2 = itemData.value ?? "";
|
|
731
|
+
const keywords = itemData.keywords ?? [];
|
|
726
732
|
const rank = score(value2, keywords);
|
|
727
733
|
state.current.filtered.items.set(id, rank);
|
|
728
734
|
if (rank > 0) itemCount++;
|
|
@@ -804,7 +810,7 @@ var Command = forwardRef((props, forwardedRef) => {
|
|
|
804
810
|
variant
|
|
805
811
|
}),
|
|
806
812
|
[]
|
|
807
|
-
//
|
|
813
|
+
// Empty deps array, consistent with original cmdk implementation
|
|
808
814
|
);
|
|
809
815
|
const last = () => updateSelectedToIndex(getValidItems().length - 1);
|
|
810
816
|
const next = (e) => {
|
|
@@ -934,59 +940,61 @@ var CommandFooter = forwardRef(
|
|
|
934
940
|
}
|
|
935
941
|
);
|
|
936
942
|
CommandFooter.displayName = "CommandFooter";
|
|
937
|
-
var CommandGroup =
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
|
|
975
|
-
|
|
976
|
-
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
|
|
943
|
+
var CommandGroup = memo(
|
|
944
|
+
forwardRef((props, forwardedRef) => {
|
|
945
|
+
const { className, heading, children, forceMount, value, ...rest } = props;
|
|
946
|
+
const id = useId();
|
|
947
|
+
const ref = useRef(null);
|
|
948
|
+
const headingRef = useRef(null);
|
|
949
|
+
const headingId = React.useId();
|
|
950
|
+
const context = useCommand();
|
|
951
|
+
const render = useCommandState(
|
|
952
|
+
(state) => forceMount ? true : context.filter() === false ? true : !state.search ? true : state.filtered.groups.has(id)
|
|
953
|
+
);
|
|
954
|
+
useEffect(() => {
|
|
955
|
+
return context.group(id);
|
|
956
|
+
}, [context, id]);
|
|
957
|
+
const valueDeps = useMemo(() => [value, heading, headingRef], [value, heading]);
|
|
958
|
+
useValue(id, ref, valueDeps);
|
|
959
|
+
const contextValue = useMemo(() => ({ id, forceMount }), [id, forceMount]);
|
|
960
|
+
const tv = commandGroupTv({ variant: context.variant, hidden: !render });
|
|
961
|
+
return /* @__PURE__ */ jsxs(
|
|
962
|
+
"div",
|
|
963
|
+
{
|
|
964
|
+
ref: (el) => {
|
|
965
|
+
ref.current = el;
|
|
966
|
+
if (typeof forwardedRef === "function") forwardedRef(el);
|
|
967
|
+
else if (forwardedRef) forwardedRef.current = el;
|
|
968
|
+
},
|
|
969
|
+
...rest,
|
|
970
|
+
className: tv.root({ className }),
|
|
971
|
+
role: "presentation",
|
|
972
|
+
"data-value": value,
|
|
973
|
+
"data-hidden": !render || void 0,
|
|
974
|
+
children: [
|
|
975
|
+
heading && /* @__PURE__ */ jsx(
|
|
976
|
+
"div",
|
|
977
|
+
{
|
|
978
|
+
ref: headingRef,
|
|
979
|
+
className: tcx(tv.heading()),
|
|
980
|
+
"aria-hidden": true,
|
|
981
|
+
id: headingId,
|
|
982
|
+
children: heading
|
|
983
|
+
}
|
|
984
|
+
),
|
|
985
|
+
/* @__PURE__ */ jsx(
|
|
986
|
+
"div",
|
|
987
|
+
{
|
|
988
|
+
role: "group",
|
|
989
|
+
"aria-labelledby": heading ? headingId : void 0,
|
|
990
|
+
children: /* @__PURE__ */ jsx(GroupContext.Provider, { value: contextValue, children })
|
|
991
|
+
}
|
|
992
|
+
)
|
|
993
|
+
]
|
|
994
|
+
}
|
|
995
|
+
);
|
|
996
|
+
})
|
|
997
|
+
);
|
|
990
998
|
CommandGroup.displayName = "CommandGroup";
|
|
991
999
|
var CommandInput = forwardRef((props, ref) => {
|
|
992
1000
|
const { className, onChange, value, prefixElement, suffixElement, ...rest } = props;
|
|
@@ -1000,155 +1008,159 @@ var CommandInput = forwardRef((props, ref) => {
|
|
|
1000
1008
|
store.setState("search", value);
|
|
1001
1009
|
}
|
|
1002
1010
|
}, [value, store]);
|
|
1003
|
-
const handleChange = useEventCallback((
|
|
1011
|
+
const handleChange = useEventCallback((newValue) => {
|
|
1004
1012
|
if (!isControlled) {
|
|
1005
|
-
store.setState("search",
|
|
1013
|
+
store.setState("search", newValue);
|
|
1006
1014
|
}
|
|
1007
|
-
onChange?.(
|
|
1015
|
+
onChange?.(newValue);
|
|
1008
1016
|
});
|
|
1009
1017
|
const tv = commandInputTv({ size: context.size });
|
|
1010
|
-
return /* @__PURE__ */ jsxs("div", { className: tcx(tv.root({ className })), children: [
|
|
1011
|
-
prefixElement,
|
|
1012
|
-
/* @__PURE__ */ jsx(
|
|
1013
|
-
Input,
|
|
1014
|
-
{
|
|
1015
|
-
ref,
|
|
1016
|
-
...rest,
|
|
1017
|
-
className: tcx(tv.input({ className })),
|
|
1018
|
-
variant: props.variant || context.variant,
|
|
1019
|
-
"data-command-input": "",
|
|
1020
|
-
autoComplete: "off",
|
|
1021
|
-
autoCorrect: "off",
|
|
1022
|
-
spellCheck: false,
|
|
1023
|
-
"aria-autocomplete": "list",
|
|
1024
|
-
role: "combobox",
|
|
1025
|
-
"aria-expanded": true,
|
|
1026
|
-
"aria-controls": context.listId,
|
|
1027
|
-
"aria-labelledby": context.labelId,
|
|
1028
|
-
"aria-activedescendant": selectedItemId,
|
|
1029
|
-
id: context.inputId,
|
|
1030
|
-
type: "text",
|
|
1031
|
-
value: isControlled ? value : search,
|
|
1032
|
-
onChange: handleChange
|
|
1033
|
-
}
|
|
1034
|
-
),
|
|
1035
|
-
suffixElement
|
|
1036
|
-
] });
|
|
1037
|
-
});
|
|
1038
|
-
CommandInput.displayName = "CommandInput";
|
|
1039
|
-
var CommandItem = forwardRef((props, forwardedRef) => {
|
|
1040
|
-
const {
|
|
1041
|
-
className,
|
|
1042
|
-
disabled,
|
|
1043
|
-
forceMount,
|
|
1044
|
-
keywords,
|
|
1045
|
-
onSelect,
|
|
1046
|
-
value,
|
|
1047
|
-
children,
|
|
1048
|
-
prefixElement,
|
|
1049
|
-
suffixElement,
|
|
1050
|
-
shortcut,
|
|
1051
|
-
...rest
|
|
1052
|
-
} = props;
|
|
1053
|
-
const ref = useRef(null);
|
|
1054
|
-
const id = React4.useId();
|
|
1055
|
-
const context = useCommand();
|
|
1056
|
-
const groupContext = React4.useContext(GroupContext);
|
|
1057
|
-
const propsRef = useRef({
|
|
1058
|
-
disabled,
|
|
1059
|
-
forceMount: forceMount ?? groupContext?.forceMount,
|
|
1060
|
-
keywords,
|
|
1061
|
-
onSelect,
|
|
1062
|
-
value
|
|
1063
|
-
});
|
|
1064
|
-
propsRef.current = {
|
|
1065
|
-
disabled,
|
|
1066
|
-
forceMount: forceMount ?? groupContext?.forceMount,
|
|
1067
|
-
keywords,
|
|
1068
|
-
onSelect,
|
|
1069
|
-
value
|
|
1070
|
-
};
|
|
1071
|
-
useEffect(() => {
|
|
1072
|
-
if (!propsRef.current.forceMount) {
|
|
1073
|
-
return context.item(id, groupContext?.id);
|
|
1074
|
-
}
|
|
1075
|
-
}, [context, groupContext?.id, id]);
|
|
1076
|
-
const valueDeps = useMemo(() => [value, children, ref], [value, children]);
|
|
1077
|
-
const stableKeywords = useMemo(() => keywords || [], [keywords]);
|
|
1078
|
-
const valueRef = useValue(id, ref, valueDeps, stableKeywords);
|
|
1079
|
-
const store = context.store;
|
|
1080
|
-
const selected = useCommandState(
|
|
1081
|
-
(state) => Boolean(state.value && state.value === valueRef?.current)
|
|
1082
|
-
);
|
|
1083
|
-
const render = useCommandState(
|
|
1084
|
-
(state) => propsRef.current.forceMount ? true : context.filter() === false ? true : !state.search ? true : (state.filtered.items.get(id) ?? 0) > 0
|
|
1085
|
-
);
|
|
1086
|
-
useEffect(() => {
|
|
1087
|
-
const element = ref.current;
|
|
1088
|
-
if (!element || disabled) return;
|
|
1089
|
-
const handleSelect = () => {
|
|
1090
|
-
select();
|
|
1091
|
-
propsRef.current.onSelect?.(valueRef.current || "");
|
|
1092
|
-
};
|
|
1093
|
-
element.addEventListener(SELECT_EVENT, handleSelect);
|
|
1094
|
-
return () => element.removeEventListener(SELECT_EVENT, handleSelect);
|
|
1095
|
-
}, [render, disabled, valueRef]);
|
|
1096
|
-
const select = () => {
|
|
1097
|
-
store.setState("value", valueRef.current || "", true);
|
|
1098
|
-
};
|
|
1099
|
-
const hasValidShortcut = shortcut && (shortcut.modifier || shortcut.keys);
|
|
1100
|
-
const handlePointerMove = useEventCallback(() => {
|
|
1101
|
-
if (disabled || context.getDisablePointerSelection()) return;
|
|
1102
|
-
select();
|
|
1103
|
-
});
|
|
1104
|
-
const handleClick = useEventCallback(() => {
|
|
1105
|
-
if (disabled) return;
|
|
1106
|
-
propsRef.current.onSelect?.(valueRef.current || "");
|
|
1107
|
-
});
|
|
1108
|
-
const tv = commandItemTv({
|
|
1109
|
-
selected,
|
|
1110
|
-
disabled,
|
|
1111
|
-
size: context.size,
|
|
1112
|
-
hasPrefix: !!prefixElement,
|
|
1113
|
-
hasSuffix: !!suffixElement,
|
|
1114
|
-
variant: context.variant
|
|
1115
|
-
});
|
|
1116
|
-
if (!render) return null;
|
|
1117
1018
|
return /* @__PURE__ */ jsxs(
|
|
1118
|
-
|
|
1019
|
+
TextField,
|
|
1119
1020
|
{
|
|
1120
|
-
ref
|
|
1121
|
-
ref.current = el;
|
|
1122
|
-
if (typeof forwardedRef === "function") forwardedRef(el);
|
|
1123
|
-
else if (forwardedRef) forwardedRef.current = el;
|
|
1124
|
-
},
|
|
1021
|
+
ref,
|
|
1125
1022
|
...rest,
|
|
1126
|
-
|
|
1127
|
-
|
|
1128
|
-
|
|
1129
|
-
"
|
|
1130
|
-
"
|
|
1131
|
-
"
|
|
1132
|
-
|
|
1133
|
-
"
|
|
1134
|
-
|
|
1135
|
-
|
|
1023
|
+
className: tcx(tv.input({ className })),
|
|
1024
|
+
size: "large",
|
|
1025
|
+
variant: props.variant || context.variant,
|
|
1026
|
+
"data-command-input": "",
|
|
1027
|
+
autoComplete: "off",
|
|
1028
|
+
autoCorrect: "off",
|
|
1029
|
+
spellCheck: false,
|
|
1030
|
+
"aria-autocomplete": "list",
|
|
1031
|
+
role: "combobox",
|
|
1032
|
+
"aria-expanded": true,
|
|
1033
|
+
"aria-controls": context.listId,
|
|
1034
|
+
"aria-labelledby": context.labelId,
|
|
1035
|
+
"aria-activedescendant": selectedItemId,
|
|
1036
|
+
id: context.inputId,
|
|
1037
|
+
type: "text",
|
|
1038
|
+
value: isControlled ? value : search,
|
|
1039
|
+
onChange: handleChange,
|
|
1136
1040
|
children: [
|
|
1137
|
-
prefixElement && /* @__PURE__ */ jsx(
|
|
1138
|
-
children
|
|
1139
|
-
hasValidShortcut && /* @__PURE__ */ jsx(
|
|
1140
|
-
Kbd,
|
|
1141
|
-
{
|
|
1142
|
-
className: tv.shortcut(),
|
|
1143
|
-
keys: shortcut.modifier,
|
|
1144
|
-
children: shortcut.keys
|
|
1145
|
-
}
|
|
1146
|
-
),
|
|
1147
|
-
suffixElement && /* @__PURE__ */ jsx("div", { className: tv.icon(), children: suffixElement })
|
|
1041
|
+
prefixElement && /* @__PURE__ */ jsx(TextField.Prefix, { children: prefixElement }),
|
|
1042
|
+
suffixElement && /* @__PURE__ */ jsx(TextField.Suffix, { children: suffixElement })
|
|
1148
1043
|
]
|
|
1149
1044
|
}
|
|
1150
1045
|
);
|
|
1151
1046
|
});
|
|
1047
|
+
CommandInput.displayName = "CommandInput";
|
|
1048
|
+
var CommandItem = memo(
|
|
1049
|
+
forwardRef((props, forwardedRef) => {
|
|
1050
|
+
const {
|
|
1051
|
+
className,
|
|
1052
|
+
disabled,
|
|
1053
|
+
forceMount,
|
|
1054
|
+
keywords,
|
|
1055
|
+
onSelect,
|
|
1056
|
+
value,
|
|
1057
|
+
children,
|
|
1058
|
+
prefixElement,
|
|
1059
|
+
suffixElement,
|
|
1060
|
+
shortcut,
|
|
1061
|
+
...rest
|
|
1062
|
+
} = props;
|
|
1063
|
+
const ref = useRef(null);
|
|
1064
|
+
const id = React.useId();
|
|
1065
|
+
const context = useCommand();
|
|
1066
|
+
const groupContext = React.useContext(GroupContext);
|
|
1067
|
+
const propsRef = useRef({
|
|
1068
|
+
disabled,
|
|
1069
|
+
forceMount: forceMount ?? groupContext?.forceMount,
|
|
1070
|
+
keywords,
|
|
1071
|
+
onSelect,
|
|
1072
|
+
value
|
|
1073
|
+
});
|
|
1074
|
+
propsRef.current = {
|
|
1075
|
+
disabled,
|
|
1076
|
+
forceMount: forceMount ?? groupContext?.forceMount,
|
|
1077
|
+
keywords,
|
|
1078
|
+
onSelect,
|
|
1079
|
+
value
|
|
1080
|
+
};
|
|
1081
|
+
useEffect(() => {
|
|
1082
|
+
if (!propsRef.current.forceMount) {
|
|
1083
|
+
return context.item(id, groupContext?.id);
|
|
1084
|
+
}
|
|
1085
|
+
}, [context, groupContext?.id, id]);
|
|
1086
|
+
const valueDeps = useMemo(() => [value, children, ref], [value, children]);
|
|
1087
|
+
const stableKeywords = useMemo(() => keywords || [], [keywords]);
|
|
1088
|
+
const valueRef = useValue(id, ref, valueDeps, stableKeywords);
|
|
1089
|
+
const store = context.store;
|
|
1090
|
+
const selected = useCommandState(
|
|
1091
|
+
(state) => Boolean(state.value && state.value === valueRef?.current)
|
|
1092
|
+
);
|
|
1093
|
+
const render = useCommandState(
|
|
1094
|
+
(state) => propsRef.current.forceMount ? true : context.filter() === false ? true : !state.search ? true : (state.filtered.items.get(id) ?? 0) > 0
|
|
1095
|
+
);
|
|
1096
|
+
useEffect(() => {
|
|
1097
|
+
const element = ref.current;
|
|
1098
|
+
if (!element || disabled) return;
|
|
1099
|
+
const handleSelect = () => {
|
|
1100
|
+
select();
|
|
1101
|
+
propsRef.current.onSelect?.(valueRef.current || "");
|
|
1102
|
+
};
|
|
1103
|
+
element.addEventListener(SELECT_EVENT, handleSelect);
|
|
1104
|
+
return () => element.removeEventListener(SELECT_EVENT, handleSelect);
|
|
1105
|
+
}, [render, disabled, valueRef]);
|
|
1106
|
+
const select = () => {
|
|
1107
|
+
store.setState("value", valueRef.current || "", true);
|
|
1108
|
+
};
|
|
1109
|
+
const hasValidShortcut = shortcut && (shortcut.modifier || shortcut.keys);
|
|
1110
|
+
const handlePointerMove = useEventCallback(() => {
|
|
1111
|
+
if (disabled || context.getDisablePointerSelection()) return;
|
|
1112
|
+
select();
|
|
1113
|
+
});
|
|
1114
|
+
const handleClick = useEventCallback(() => {
|
|
1115
|
+
if (disabled) return;
|
|
1116
|
+
propsRef.current.onSelect?.(valueRef.current || "");
|
|
1117
|
+
});
|
|
1118
|
+
const tv = commandItemTv({
|
|
1119
|
+
selected,
|
|
1120
|
+
disabled,
|
|
1121
|
+
size: context.size,
|
|
1122
|
+
hasPrefix: !!prefixElement,
|
|
1123
|
+
hasSuffix: !!suffixElement,
|
|
1124
|
+
variant: context.variant,
|
|
1125
|
+
hidden: !render
|
|
1126
|
+
});
|
|
1127
|
+
return /* @__PURE__ */ jsxs(
|
|
1128
|
+
"div",
|
|
1129
|
+
{
|
|
1130
|
+
ref: (el) => {
|
|
1131
|
+
ref.current = el;
|
|
1132
|
+
if (typeof forwardedRef === "function") forwardedRef(el);
|
|
1133
|
+
else if (forwardedRef) forwardedRef.current = el;
|
|
1134
|
+
},
|
|
1135
|
+
...rest,
|
|
1136
|
+
id,
|
|
1137
|
+
className: tv.root({ className }),
|
|
1138
|
+
role: "option",
|
|
1139
|
+
"aria-disabled": disabled,
|
|
1140
|
+
"aria-selected": selected || void 0,
|
|
1141
|
+
"data-hidden": !render || void 0,
|
|
1142
|
+
"data-disabled": disabled,
|
|
1143
|
+
"data-selected": selected,
|
|
1144
|
+
"data-value": valueRef.current,
|
|
1145
|
+
onPointerMove: handlePointerMove,
|
|
1146
|
+
onClick: handleClick,
|
|
1147
|
+
children: [
|
|
1148
|
+
prefixElement && /* @__PURE__ */ jsx("div", { className: tv.icon(), children: prefixElement }),
|
|
1149
|
+
children,
|
|
1150
|
+
hasValidShortcut && /* @__PURE__ */ jsx(
|
|
1151
|
+
Kbd,
|
|
1152
|
+
{
|
|
1153
|
+
className: tv.shortcut(),
|
|
1154
|
+
keys: shortcut.modifier,
|
|
1155
|
+
children: shortcut.keys
|
|
1156
|
+
}
|
|
1157
|
+
),
|
|
1158
|
+
suffixElement && /* @__PURE__ */ jsx("div", { className: tv.icon(), children: suffixElement })
|
|
1159
|
+
]
|
|
1160
|
+
}
|
|
1161
|
+
);
|
|
1162
|
+
})
|
|
1163
|
+
);
|
|
1152
1164
|
CommandItem.displayName = "CommandItem";
|
|
1153
1165
|
var CommandList = forwardRef((props, forwardedRef) => {
|
|
1154
1166
|
const { children, className, label = "Suggestions", hoverBoundary = "none", ...rest } = props;
|
|
@@ -1222,7 +1234,7 @@ var CommandLoading = forwardRef((props, ref) => {
|
|
|
1222
1234
|
"div",
|
|
1223
1235
|
{
|
|
1224
1236
|
ref,
|
|
1225
|
-
...
|
|
1237
|
+
...rest,
|
|
1226
1238
|
className: tcx(tv.root({ className })),
|
|
1227
1239
|
role: "progressbar",
|
|
1228
1240
|
"aria-valuenow": progress,
|
|
@@ -1297,4 +1309,4 @@ var Command2 = Object.assign(Command, {
|
|
|
1297
1309
|
TabItem
|
|
1298
1310
|
});
|
|
1299
1311
|
|
|
1300
|
-
export { Command2 as Command, Command as CommandRoot, defaultFilter, useCommandState };
|
|
1312
|
+
export { Command2 as Command, Command as CommandRoot, commandScore, defaultFilter, useCommand, useCommandState, useValue };
|