@cognite/aura 1.48651.0 → 1.48714.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.
|
@@ -1,10 +1,15 @@
|
|
|
1
1
|
import { type ForwardRefExoticComponent, type RefAttributes, type ComponentProps } from "react";
|
|
2
2
|
import { Button } from "../button/button";
|
|
3
|
+
type ComboboxOption = {
|
|
4
|
+
value: string;
|
|
5
|
+
label: string;
|
|
6
|
+
};
|
|
7
|
+
type ComboboxGroup = {
|
|
8
|
+
label?: string;
|
|
9
|
+
options: ComboboxOption[];
|
|
10
|
+
};
|
|
3
11
|
type ComboboxProps = Omit<ComponentProps<typeof Button>, "value" | "onChange" | "variant" | "size" | "render" | "nativeButton"> & {
|
|
4
|
-
options:
|
|
5
|
-
value: string;
|
|
6
|
-
label: string;
|
|
7
|
-
}>;
|
|
12
|
+
options: ComboboxOption[] | ComboboxGroup[];
|
|
8
13
|
value?: string;
|
|
9
14
|
onValueChange?: (value: string) => void;
|
|
10
15
|
placeholder?: string;
|
|
@@ -13,3 +18,4 @@ type ComboboxProps = Omit<ComponentProps<typeof Button>, "value" | "onChange" |
|
|
|
13
18
|
};
|
|
14
19
|
declare const Combobox: ForwardRefExoticComponent<Omit<ComboboxProps, "ref"> & RefAttributes<HTMLElement>>;
|
|
15
20
|
export { Combobox };
|
|
21
|
+
export type { ComboboxGroup, ComboboxOption };
|
|
@@ -1,17 +1,25 @@
|
|
|
1
1
|
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
-
import { forwardRef, useId, useState } from "react";
|
|
2
|
+
import { Fragment, forwardRef, useId, useState } from "react";
|
|
3
3
|
import { IconCaretUpDown, IconCheck } from "@tabler/icons-react";
|
|
4
4
|
import { cn, formFieldOpenState, formFieldTriggerClasses } from "../../../../lib/utils.js";
|
|
5
5
|
import { Button } from "../button/button.js";
|
|
6
|
-
import { Command, CommandEmpty, CommandGroup, CommandInput, CommandItem, CommandList } from "../command/command.js";
|
|
6
|
+
import { Command, CommandEmpty, CommandGroup, CommandInput, CommandItem, CommandList, CommandSeparator } from "../command/command.js";
|
|
7
7
|
import { Popover, PopoverContent, PopoverTrigger } from "../popover/popover.js";
|
|
8
|
+
function isGroupedOptions(options) {
|
|
9
|
+
return options.length > 0 && 'options' in options[0];
|
|
10
|
+
}
|
|
8
11
|
const Combobox = /*#__PURE__*/ forwardRef(({ options, value, onValueChange, placeholder = 'Select option...', searchPlaceholder = 'Search...', emptyText = 'No option found.', className, type = 'button', ...props }, ref)=>{
|
|
9
12
|
const [open, setOpen] = useState(false);
|
|
10
13
|
const listboxId = useId();
|
|
11
14
|
const [internalValue, setInternalValue] = useState(value ?? '');
|
|
12
15
|
const isControlled = value !== undefined;
|
|
13
16
|
const selectedValue = isControlled ? value : internalValue;
|
|
14
|
-
const
|
|
17
|
+
const groups = isGroupedOptions(options) ? options : [
|
|
18
|
+
{
|
|
19
|
+
options
|
|
20
|
+
}
|
|
21
|
+
];
|
|
22
|
+
const selectedLabel = selectedValue ? groups.flatMap((group)=>group.options).find((option)=>option.value === selectedValue)?.label : undefined;
|
|
15
23
|
const handleValueChange = (newValue)=>{
|
|
16
24
|
const finalValue = newValue === selectedValue ? '' : newValue;
|
|
17
25
|
if (!isControlled) {
|
|
@@ -63,18 +71,27 @@ const Combobox = /*#__PURE__*/ forwardRef(({ options, value, onValueChange, plac
|
|
|
63
71
|
/*#__PURE__*/ _jsx(CommandEmpty, {
|
|
64
72
|
children: emptyText
|
|
65
73
|
}),
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
74
|
+
groups.map((group, groupIndex)=>/*#__PURE__*/ _jsxs(Fragment, {
|
|
75
|
+
children: [
|
|
76
|
+
groupIndex > 0 && /*#__PURE__*/ _jsx(CommandSeparator, {}),
|
|
77
|
+
/*#__PURE__*/ _jsx(CommandGroup, {
|
|
78
|
+
heading: group.label,
|
|
79
|
+
children: group.options.map((option)=>/*#__PURE__*/ _jsxs(CommandItem, {
|
|
80
|
+
value: option.value,
|
|
81
|
+
onSelect: handleValueChange,
|
|
82
|
+
children: [
|
|
83
|
+
/*#__PURE__*/ _jsx("span", {
|
|
84
|
+
className: "flex-1 truncate",
|
|
85
|
+
children: option.label
|
|
86
|
+
}),
|
|
87
|
+
/*#__PURE__*/ _jsx(IconCheck, {
|
|
88
|
+
className: cn('size-4', selectedValue === option.value ? 'opacity-100' : 'opacity-0')
|
|
89
|
+
})
|
|
90
|
+
]
|
|
91
|
+
}, option.value))
|
|
92
|
+
})
|
|
93
|
+
]
|
|
94
|
+
}, group.label ?? groupIndex))
|
|
78
95
|
]
|
|
79
96
|
})
|
|
80
97
|
]
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../../../src/components/ui/core/combobox/combobox.tsx"],"sourcesContent":["import {\n type ForwardRefExoticComponent,\n type RefAttributes,\n type ComponentProps,\n type ComponentRef,\n forwardRef,\n useId,\n useState,\n} from 'react';\n\nimport { IconCaretUpDown, IconCheck } from '@tabler/icons-react';\n\nimport {\n cn,\n formFieldOpenState,\n formFieldTriggerClasses,\n} from '../../../../lib/utils';\nimport { Button } from '../button/button';\nimport {\n Command,\n CommandEmpty,\n CommandGroup,\n CommandInput,\n CommandItem,\n CommandList,\n} from '../command/command';\nimport { Popover, PopoverContent, PopoverTrigger } from '../popover/popover';\n\ntype ComboboxProps = Omit<\n ComponentProps<typeof Button>,\n 'value' | 'onChange' | 'variant' | 'size' | 'render' | 'nativeButton'\n> & {\n options:
|
|
1
|
+
{"version":3,"sources":["../../../../../src/components/ui/core/combobox/combobox.tsx"],"sourcesContent":["import {\n type ForwardRefExoticComponent,\n type RefAttributes,\n type ComponentProps,\n type ComponentRef,\n Fragment,\n forwardRef,\n useId,\n useState,\n} from 'react';\n\nimport { IconCaretUpDown, IconCheck } from '@tabler/icons-react';\n\nimport {\n cn,\n formFieldOpenState,\n formFieldTriggerClasses,\n} from '../../../../lib/utils';\nimport { Button } from '../button/button';\nimport {\n Command,\n CommandEmpty,\n CommandGroup,\n CommandInput,\n CommandItem,\n CommandList,\n CommandSeparator,\n} from '../command/command';\nimport { Popover, PopoverContent, PopoverTrigger } from '../popover/popover';\n\ntype ComboboxOption = { value: string; label: string };\n\ntype ComboboxGroup = { label?: string; options: ComboboxOption[] };\n\nfunction isGroupedOptions(\n options: ComboboxOption[] | ComboboxGroup[]\n): options is ComboboxGroup[] {\n return options.length > 0 && 'options' in options[0];\n}\n\ntype ComboboxProps = Omit<\n ComponentProps<typeof Button>,\n 'value' | 'onChange' | 'variant' | 'size' | 'render' | 'nativeButton'\n> & {\n options: ComboboxOption[] | ComboboxGroup[];\n value?: string;\n onValueChange?: (value: string) => void;\n placeholder?: string;\n searchPlaceholder?: string;\n emptyText?: string;\n};\n\nconst Combobox: ForwardRefExoticComponent<\n Omit<ComboboxProps, 'ref'> & RefAttributes<HTMLElement>\n> = forwardRef<ComponentRef<typeof Button>, ComboboxProps>(\n (\n {\n options,\n value,\n onValueChange,\n placeholder = 'Select option...',\n searchPlaceholder = 'Search...',\n emptyText = 'No option found.',\n className,\n type = 'button',\n ...props\n },\n ref\n ) => {\n const [open, setOpen] = useState(false);\n const listboxId = useId();\n const [internalValue, setInternalValue] = useState(value ?? '');\n const isControlled = value !== undefined;\n const selectedValue = isControlled ? value : internalValue;\n const groups = isGroupedOptions(options) ? options : [{ options }];\n const selectedLabel = selectedValue\n ? groups\n .flatMap((group) => group.options)\n .find((option) => option.value === selectedValue)?.label\n : undefined;\n\n const handleValueChange = (newValue: string) => {\n const finalValue = newValue === selectedValue ? '' : newValue;\n if (!isControlled) {\n setInternalValue(finalValue);\n }\n onValueChange?.(finalValue);\n setOpen(false);\n };\n\n return (\n <div data-slot=\"combobox\">\n <Popover open={open} onOpenChange={setOpen}>\n <PopoverTrigger\n render={\n <Button\n ref={ref}\n type={type}\n nativeButton\n variant={null}\n size={null}\n data-slot=\"combobox-trigger\"\n role=\"combobox\"\n aria-expanded={open}\n aria-controls={listboxId}\n className={cn(\n formFieldTriggerClasses,\n open && formFieldOpenState,\n className\n )}\n {...props}\n >\n <span\n className={cn(\n 'flex-1 truncate text-left',\n selectedLabel\n ? 'text-base-foreground disabled:text-base-foreground-disabled'\n : 'text-muted-foreground disabled:text-base-foreground-disabled'\n )}\n >\n {selectedLabel ?? placeholder}\n </span>\n <IconCaretUpDown className=\"size-4 opacity-50\" />\n </Button>\n }\n />\n <PopoverContent id={listboxId} className={cn('w-50 p-0', className)}>\n <Command>\n <CommandInput placeholder={searchPlaceholder} />\n <CommandList>\n <CommandEmpty>{emptyText}</CommandEmpty>\n {groups.map((group, groupIndex) => (\n <Fragment key={group.label ?? groupIndex}>\n {groupIndex > 0 && <CommandSeparator />}\n <CommandGroup heading={group.label}>\n {group.options.map((option) => (\n <CommandItem\n key={option.value}\n value={option.value}\n onSelect={handleValueChange}\n >\n <span className=\"flex-1 truncate\">\n {option.label}\n </span>\n <IconCheck\n className={cn(\n 'size-4',\n selectedValue === option.value\n ? 'opacity-100'\n : 'opacity-0'\n )}\n />\n </CommandItem>\n ))}\n </CommandGroup>\n </Fragment>\n ))}\n </CommandList>\n </Command>\n </PopoverContent>\n </Popover>\n </div>\n );\n }\n);\n\nCombobox.displayName = 'Combobox';\n\nexport { Combobox };\nexport type { ComboboxGroup, ComboboxOption };\n"],"names":["Fragment","forwardRef","useId","useState","IconCaretUpDown","IconCheck","cn","formFieldOpenState","formFieldTriggerClasses","Button","Command","CommandEmpty","CommandGroup","CommandInput","CommandItem","CommandList","CommandSeparator","Popover","PopoverContent","PopoverTrigger","isGroupedOptions","options","length","Combobox","value","onValueChange","placeholder","searchPlaceholder","emptyText","className","type","props","ref","open","setOpen","listboxId","internalValue","setInternalValue","isControlled","undefined","selectedValue","groups","selectedLabel","flatMap","group","find","option","label","handleValueChange","newValue","finalValue","div","data-slot","onOpenChange","render","nativeButton","variant","size","role","aria-expanded","aria-controls","span","id","map","groupIndex","heading","onSelect","displayName"],"mappings":";AAAA,SAKEA,QAAQ,EACRC,UAAU,EACVC,KAAK,EACLC,QAAQ,QACH,QAAQ;AAEf,SAASC,eAAe,EAAEC,SAAS,QAAQ,sBAAsB;AAEjE,SACEC,EAAE,EACFC,kBAAkB,EAClBC,uBAAuB,QAClB,2BAAwB;AAC/B,SAASC,MAAM,QAAQ,sBAAmB;AAC1C,SACEC,OAAO,EACPC,YAAY,EACZC,YAAY,EACZC,YAAY,EACZC,WAAW,EACXC,WAAW,EACXC,gBAAgB,QACX,wBAAqB;AAC5B,SAASC,OAAO,EAAEC,cAAc,EAAEC,cAAc,QAAQ,wBAAqB;AAM7E,SAASC,iBACPC,OAA2C;IAE3C,OAAOA,QAAQC,MAAM,GAAG,KAAK,aAAaD,OAAO,CAAC,EAAE;AACtD;AAcA,MAAME,yBAEFtB,WACF,CACE,EACEoB,OAAO,EACPG,KAAK,EACLC,aAAa,EACbC,cAAc,kBAAkB,EAChCC,oBAAoB,WAAW,EAC/BC,YAAY,kBAAkB,EAC9BC,SAAS,EACTC,OAAO,QAAQ,EACf,GAAGC,OACJ,EACDC;IAEA,MAAM,CAACC,MAAMC,QAAQ,GAAG/B,SAAS;IACjC,MAAMgC,YAAYjC;IAClB,MAAM,CAACkC,eAAeC,iBAAiB,GAAGlC,SAASqB,SAAS;IAC5D,MAAMc,eAAed,UAAUe;IAC/B,MAAMC,gBAAgBF,eAAed,QAAQY;IAC7C,MAAMK,SAASrB,iBAAiBC,WAAWA,UAAU;QAAC;YAAEA;QAAQ;KAAE;IAClE,MAAMqB,gBAAgBF,gBAClBC,OACGE,OAAO,CAAC,CAACC,QAAUA,MAAMvB,OAAO,EAChCwB,IAAI,CAAC,CAACC,SAAWA,OAAOtB,KAAK,KAAKgB,gBAAgBO,QACrDR;IAEJ,MAAMS,oBAAoB,CAACC;QACzB,MAAMC,aAAaD,aAAaT,gB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