@firecms/ui 3.0.0-canary.120 → 3.0.0-canary.122

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/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@firecms/ui",
3
3
  "type": "module",
4
- "version": "3.0.0-canary.120",
4
+ "version": "3.0.0-canary.122",
5
5
  "description": "Awesome Firebase/Firestore-based headless open-source CMS",
6
6
  "funding": {
7
7
  "url": "https://github.com/sponsors/firecmsco"
@@ -105,7 +105,7 @@
105
105
  "src",
106
106
  "tailwind.config.js"
107
107
  ],
108
- "gitHead": "aa817730cb582b5724774a50ac8a512bf0643ef3",
108
+ "gitHead": "34cb0d35ba1246cad86def496affcb87fb9a91cf",
109
109
  "publishConfig": {
110
110
  "access": "public"
111
111
  }
@@ -1,190 +1,332 @@
1
+ import * as PopoverPrimitive from "@radix-ui/react-popover";
1
2
  import * as React from "react";
2
- import { useEffect } from "react";
3
- import * as Dialog from "@radix-ui/react-dialog";
4
-
3
+ import { ChangeEvent, Children, useEffect } from "react";
5
4
  import { Command as CommandPrimitive } from "cmdk";
6
-
7
- import { ExpandMoreIcon } from "../icons";
8
- import { fieldBackgroundDisabledMixin, fieldBackgroundHoverMixin, fieldBackgroundMixin, focusedDisabled } from "../styles";
9
5
  import { cls } from "../util";
6
+ import { CloseIcon, ExpandMoreIcon, Icon } from "../icons";
7
+ import { Separator } from "./Separator";
8
+ import { Chip } from "./Chip";
10
9
  import { SelectInputLabel } from "./common/SelectInputLabel";
11
- import { useOutsideAlerter } from "../hooks";
10
+ import {
11
+ defaultBorderMixin,
12
+ fieldBackgroundDisabledMixin,
13
+ fieldBackgroundHoverMixin,
14
+ fieldBackgroundInvisibleMixin,
15
+ fieldBackgroundMixin,
16
+ focusedDisabled
17
+ } from "../styles";
18
+ import { useInjectStyles } from "../hooks";
19
+
20
+ interface MultiSelectContextProps {
21
+ fieldValue?: string[];
22
+ onItemClick: (v: string) => void;
23
+ }
24
+
25
+ export const MultiSelectContext = React.createContext<MultiSelectContextProps>({} as any);
26
+
27
+ /**
28
+ * Props for MultiSelect component
29
+ */
30
+ interface MultiSelectProps {
31
+
32
+ /**
33
+ * The modality of the popover. When set to true, interaction with outside elements
34
+ * will be disabled and only popover content will be visible to screen readers.
35
+ * Optional, defaults to false.
36
+ */
37
+ modalPopover?: boolean;
38
+
39
+ /**
40
+ * Additional class names to apply custom styles to the multi-select component.
41
+ * Optional, can be used to add custom styles.
42
+ */
43
+ className?: string;
12
44
 
13
- export type MultiSelectProps = {
14
45
  open?: boolean,
15
46
  name?: string,
16
47
  id?: string,
17
48
  onOpenChange?: (open: boolean) => void,
18
49
  value?: string[],
19
- containerClassName?: string,
20
- className?: string,
21
50
  inputClassName?: string,
22
- onMultiValueChange?: (updatedValue: string[]) => void,
51
+ onChange?: React.EventHandler<ChangeEvent<HTMLSelectElement>>,
52
+ onValueChange?: (updatedValue: string[]) => void,
23
53
  placeholder?: React.ReactNode,
24
- renderValue?: (values: string, index:number) => React.ReactNode,
25
- renderValues?: (values: string[]) => React.ReactNode,
26
54
  size?: "small" | "medium",
27
- label?: React.ReactNode,
55
+ useChips?: boolean,
56
+ label?: React.ReactNode | string,
28
57
  disabled?: boolean,
29
58
  error?: boolean,
30
59
  position?: "item-aligned" | "popper",
60
+ endAdornment?: React.ReactNode,
61
+ multiple?: boolean,
62
+ includeClear?: boolean,
31
63
  inputRef?: React.RefObject<HTMLButtonElement>,
32
- children?: React.ReactNode,
33
- };
34
-
35
- interface MultiSelectContextProps {
36
- fieldValue?: string[];
37
- setInputValue: (v: string) => void;
38
- onValueChangeInternal: (v: string) => void;
64
+ padding?: boolean,
65
+ invisible?: boolean,
66
+ children: React.ReactNode;
67
+ renderValues?: (values: string[]) => React.ReactNode;
39
68
  }
40
69
 
41
- export const MultiSelectContext = React.createContext<MultiSelectContextProps>({} as any);
70
+ export const MultiSelect = React.forwardRef<
71
+ HTMLButtonElement,
72
+ MultiSelectProps
73
+ >(
74
+ (
75
+ {
76
+ value,
77
+ size,
78
+ label,
79
+ error,
80
+ onValueChange,
81
+ invisible,
82
+ disabled,
83
+ placeholder,
84
+ modalPopover = false,
85
+ includeClear = true,
86
+ useChips = true,
87
+ className,
88
+ children,
89
+ renderValues,
90
+ open,
91
+ onOpenChange,
92
+ },
93
+ ref
94
+ ) => {
95
+ const [isPopoverOpen, setIsPopoverOpen] = React.useState(open ?? false);
96
+ const [selectedValues, setSelectedValues] = React.useState<string[]>(value ?? []);
97
+ console.log("selectedValues", selectedValues);
42
98
 
43
- export function MultiSelect({
44
- value,
45
- open,
46
- onMultiValueChange,
47
- size = "medium",
48
- label,
49
- disabled,
50
- renderValue,
51
- renderValues,
52
- containerClassName,
53
- className,
54
- children,
55
- error
56
- }: MultiSelectProps) {
57
-
58
- const containerRef = React.useRef<HTMLInputElement>(null);
59
- const inputRef = React.useRef<HTMLInputElement>(null);
60
- const listRef = React.useRef<HTMLDivElement>(null);
61
- useOutsideAlerter(listRef, () => setOpenInternal(false));
62
-
63
- const [openInternal, setOpenInternal] = React.useState(false);
64
- useEffect(() => {
65
- setOpenInternal(open ?? false);
66
- }, [open]);
67
-
68
- const onValueChangeInternal = React.useCallback((newValue: string) => {
69
- if (Array.isArray(value) && value.includes(newValue)) {
70
- onMultiValueChange?.(value.filter(v => v !== newValue));
71
- } else {
72
- onMultiValueChange?.([...(value ?? []), newValue]);
99
+ const onPopoverOpenChange = (open: boolean) => {
100
+ setIsPopoverOpen(open);
101
+ onOpenChange?.(open);
73
102
  }
74
- }, [value, onMultiValueChange]);
75
-
76
- const [inputValue, setInputValue] = React.useState("");
77
- const [boundingRect, setBoundingRect] = React.useState<DOMRect | null>(null);
78
-
79
- const handleKeyDown = React.useCallback((e: React.KeyboardEvent<HTMLDivElement>) => {
80
- const input = inputRef.current
81
- if (input) {
82
- if (e.key === "Delete" || e.key === "Backspace") {
83
- if (input.value === "") {
84
- const newSelected = [...(value ?? [])];
85
- newSelected.pop();
86
- onMultiValueChange?.(newSelected);
103
+
104
+ useEffect(() => {
105
+ setIsPopoverOpen(open ?? false);
106
+ }, [open]);
107
+
108
+ const allValues = children
109
+ ?
110
+ // @ts-ignore
111
+ Children.map(children, (child) => {
112
+ if (React.isValidElement(child)) {
113
+ return child.props.value;
87
114
  }
115
+ return null;
116
+ }).filter(Boolean) as string[]
117
+ : [];
118
+
119
+ React.useEffect(() => {
120
+ setSelectedValues(value ?? []);
121
+ }, [value]);
122
+
123
+ function onItemClick(newValue: string) {
124
+ let newSelectedValues: string[];
125
+ if (selectedValues.includes(newValue)) {
126
+ newSelectedValues = selectedValues.filter((v) => v !== newValue);
127
+ } else {
128
+ newSelectedValues = [...selectedValues, newValue];
88
129
  }
89
- // This is not a default behaviour of the <input /> field
90
- if (e.key === "Escape") {
91
- input.blur();
92
- setOpenInternal(false);
93
- e.stopPropagation();
94
- }
130
+ updateValues(newSelectedValues);
95
131
  }
96
- }, [onMultiValueChange, value]);
97
-
98
- const openDialog = React.useCallback(() => {
99
- setBoundingRect(containerRef.current?.getBoundingClientRect() ?? null);
100
- setOpenInternal(true);
101
- }, []);
102
-
103
- const usedBoundingRect = boundingRect ?? containerRef.current?.getBoundingClientRect();
104
- const maxHeight = window.innerHeight - (usedBoundingRect?.top ?? 0) - (usedBoundingRect?.height ?? 0) - 16;
105
-
106
- return (<>
107
-
108
- {typeof label === "string" ? <SelectInputLabel error={error}>{label}</SelectInputLabel> : label}
109
-
110
- <CommandPrimitive onKeyDown={handleKeyDown}
111
- onClick={() => {
112
- inputRef.current?.focus();
113
- openDialog()
114
- }}
115
- className={cls("relative overflow-visible bg-transparent", containerClassName)}>
116
- <div
117
- ref={containerRef}
118
- className={cls(
119
- "flex flex-row",
120
- size === "small" ? "min-h-[42px]" : "min-h-[64px]",
121
- "select-none rounded-md text-sm",
122
- fieldBackgroundMixin,
123
- disabled ? fieldBackgroundDisabledMixin : fieldBackgroundHoverMixin,
124
- "relative flex items-center",
125
- "p-4",
126
- error ? "text-red-500 dark:text-red-600" : "focus:text-text-primary dark:focus:text-text-primary-dark",
127
- error ? "border border-red-500 dark:border-red-600" : "",
128
- className)}
132
+
133
+ function updateValues(values: string[]) {
134
+ setSelectedValues(values);
135
+ onValueChange?.(values);
136
+ }
137
+
138
+ const handleInputKeyDown = (
139
+ event: React.KeyboardEvent<HTMLInputElement>
140
+ ) => {
141
+ if (event.key === "Enter") {
142
+ onPopoverOpenChange(true);
143
+ } else if (event.key === "Backspace" && !event.currentTarget.value) {
144
+ const newSelectedValues = [...selectedValues];
145
+ newSelectedValues.pop();
146
+ updateValues(newSelectedValues);
147
+ }
148
+ };
149
+
150
+ const toggleOption = (value: string) => {
151
+ const newSelectedValues = selectedValues.includes(value)
152
+ ? selectedValues.filter((v) => v !== value)
153
+ : [...selectedValues, value];
154
+ updateValues(newSelectedValues);
155
+ };
156
+
157
+ const handleClear = () => {
158
+ updateValues([]);
159
+ };
160
+
161
+ const handleTogglePopover = () => {
162
+ onPopoverOpenChange(!isPopoverOpen);
163
+ };
164
+
165
+ const toggleAll = () => {
166
+ if (selectedValues.length === allValues.length) {
167
+ handleClear();
168
+ } else {
169
+ updateValues(allValues);
170
+ }
171
+ };
172
+
173
+ useInjectStyles("MultiSelect", `
174
+ [cmdk-group] {
175
+ max-height: 45vh;
176
+ overflow-y: auto;
177
+ // width: 400px;
178
+ } `)
179
+
180
+ return (
181
+ <MultiSelectContext.Provider
182
+ value={{
183
+ fieldValue: selectedValues,
184
+ onItemClick
185
+ }}>
186
+
187
+ {typeof label === "string" ? <SelectInputLabel error={error}>{label}</SelectInputLabel> : label}
188
+
189
+ <PopoverPrimitive.Root
190
+ open={isPopoverOpen}
191
+ onOpenChange={onPopoverOpenChange}
192
+ modal={modalPopover}
129
193
  >
130
- <div className={cls("flex-grow flex gap-1.5 flex-wrap items-center")}>
131
- {renderValue && (value ?? []).map((v, i) => renderValue(v, i))}
132
- {renderValues && renderValues(value ?? [])}
133
- <CommandPrimitive.Input
134
- ref={inputRef}
135
- value={inputValue}
136
- onValueChange={setInputValue}
137
- // onBlur={() => setOpenInternal(false)}
138
- onFocus={openDialog}
139
- className={cls("ml-2 bg-transparent outline-none flex-1 h-full w-full ", focusedDisabled)}
140
- />
141
- </div>
142
- <div className={"px-2 h-full flex items-center"}>
143
- <ExpandMoreIcon size={"small"}
144
- className={cls("transition ", openInternal ? "rotate-180" : "")}/>
145
- </div>
146
-
147
- </div>
148
-
149
- <Dialog.Root open={openInternal}
150
- modal={true}
151
- onOpenChange={setOpenInternal}>
152
- <Dialog.Portal>
153
- <MultiSelectContext.Provider
154
- value={{
155
- fieldValue: value,
156
- setInputValue,
157
- onValueChangeInternal
158
- }}>
159
- <div
160
- ref={listRef}
161
- className={"z-50 absolute overflow-auto outline-none"}
162
- style={{
163
- pointerEvents: openInternal ? "auto" : "none",
164
- top: (usedBoundingRect?.top ?? 0) + (usedBoundingRect?.height ?? 0),
165
- left: usedBoundingRect?.left,
166
- // right: boundingRect?.right,
167
- width: usedBoundingRect?.width,
168
- maxHeight: maxHeight,
169
-
170
- }}>
171
-
172
- <CommandPrimitive.Group
173
- className="mt-2 text-slate-900 dark:text-white animate-in z-50 border border-slate-200 dark:border-slate-800 bg-white dark:bg-slate-800 p-2 rounded-lg shadow-lg flex flex-col outline-none w-full"
174
- >
194
+ <PopoverPrimitive.Trigger asChild>
195
+ <button
196
+ ref={ref}
197
+ onClick={handleTogglePopover}
198
+ className={cls(
199
+ size === "small" ? "min-h-[42px]" : "min-h-[64px]",
200
+ "py-2",
201
+ "px-4",
202
+ "select-none rounded-md text-sm",
203
+ invisible ? fieldBackgroundInvisibleMixin : fieldBackgroundMixin,
204
+ disabled ? fieldBackgroundDisabledMixin : fieldBackgroundHoverMixin,
205
+ "relative flex items-center",
206
+ className
207
+ )}
208
+ >
209
+ {selectedValues.length > 0 ? (
210
+ <div className="flex justify-between items-center w-full">
211
+ <div className="flex flex-wrap items-center gap-1.5 text-start">
212
+ {renderValues && renderValues(selectedValues)}
213
+ {!renderValues && selectedValues.map((value) => {
214
+
215
+ // @ts-ignore
216
+ const childrenProps: MultiSelectItemProps[] = Children.map(children, (child) => {
217
+ if (React.isValidElement(child)) {
218
+ return child.props;
219
+ }
220
+ }).filter(Boolean);
175
221
 
222
+ const option = childrenProps.find((o) => o.value === value);
223
+ if (!useChips) {
224
+ return option?.children;
225
+ }
226
+ return (
227
+ <Chip
228
+ size={"small"}
229
+ key={value}
230
+ className={cls("flex flex-row items-center p-1")}
231
+ >
232
+ {option?.children}
233
+ <CloseIcon
234
+ size={"smallest"}
235
+ onClick={(event) => {
236
+ event.stopPropagation();
237
+ toggleOption(value);
238
+ }}
239
+ />
240
+ </Chip>
241
+ );
242
+ })}
243
+ </div>
244
+ <div className="flex items-center justify-between">
245
+ {includeClear && <CloseIcon
246
+ className={"ml-4"}
247
+ size={"small"}
248
+ onClick={(event) => {
249
+ event.stopPropagation();
250
+ handleClear();
251
+ }}
252
+ />}
253
+ <div className={cls("px-2 h-full flex items-center")}>
254
+ <ExpandMoreIcon size={"small"}
255
+ className={cls("transition", isPopoverOpen ? "rotate-180" : "")}/>
256
+ </div>
257
+ </div>
258
+ </div>
259
+ ) : (
260
+ <div className="flex items-center justify-between w-full mx-auto">
261
+ <span className="text-sm">
262
+ {placeholder}
263
+ </span>
264
+ <div className={cls("px-2 h-full flex items-center")}>
265
+ <ExpandMoreIcon size={"small"}
266
+ className={cls("transition", isPopoverOpen ? "rotate-180" : "")}/>
267
+ </div>
268
+ </div>
269
+ )}
270
+ </button>
271
+ </PopoverPrimitive.Trigger>
272
+ <PopoverPrimitive.Content
273
+ className={cls("z-50 relative overflow-hidden border bg-white dark:bg-gray-900 rounded-lg w-[400px]", defaultBorderMixin)}
274
+ align="start"
275
+ sideOffset={8}
276
+ onEscapeKeyDown={() => onPopoverOpenChange(false)}
277
+ >
278
+ <CommandPrimitive>
279
+ <div className={"flex flex-row items-center"}>
280
+ <CommandPrimitive.Input
281
+ className={cls(focusedDisabled, "bg-transparent outline-none flex-1 h-full w-full m-4 flex-grow")}
282
+ placeholder="Search..."
283
+ onKeyDown={handleInputKeyDown}
284
+ />
285
+ {selectedValues.length > 0 && (
286
+ <div
287
+ onClick={handleClear}
288
+ className="text-sm justify-center cursor-pointer py-3 px-4 text-text-secondary dark:text-text-secondary-dark">
289
+ Clear
290
+ </div>
291
+ )}
292
+ </div>
293
+ <Separator orientation={"horizontal"} className={"my-0"}/>
294
+ <CommandPrimitive.List>
295
+ <CommandPrimitive.Empty className={"px-4 py-2"}>
296
+ No results found.
297
+ </CommandPrimitive.Empty>
298
+ <CommandPrimitive.Group>
299
+ <CommandPrimitive.Item
300
+ key="all"
301
+ onSelect={toggleAll}
302
+ className={
303
+ cls(
304
+ "flex flex-row items-center gap-1.5",
305
+ "cursor-pointer",
306
+ "m-1",
307
+ "ring-offset-transparent",
308
+ "p-1 rounded aria-[selected=true]:outline-none aria-[selected=true]:ring-2 aria-[selected=true]:ring-primary aria-[selected=true]:ring-opacity-75 aria-[selected=true]:ring-offset-2",
309
+ "aria-[selected=true]:bg-slate-100 aria-[selected=true]:dark:bg-slate-900",
310
+ "cursor-pointer p-2 rounded aria-[selected=true]:bg-slate-100 aria-[selected=true]:dark:bg-slate-900"
311
+ )
312
+ }
313
+ >
314
+ <InnerCheckBox checked={selectedValues.length === allValues.length}/>
315
+ <span className={"text-sm text-text-secondary dark:text-text-secondary-dark"}>(Select All)</span>
316
+ </CommandPrimitive.Item>
176
317
  {children}
177
318
  </CommandPrimitive.Group>
178
319
 
179
- </div>
180
- </MultiSelectContext.Provider>
181
- </Dialog.Portal>
182
- </Dialog.Root>
183
- </CommandPrimitive>
320
+ </CommandPrimitive.List>
321
+ </CommandPrimitive>
322
+ </PopoverPrimitive.Content>
323
+ </PopoverPrimitive.Root>
324
+ </MultiSelectContext.Provider>
325
+ );
326
+ }
327
+ );
184
328
 
185
- </>
186
- )
187
- }
329
+ MultiSelect.displayName = "MultiSelect";
188
330
 
189
331
  export interface MultiSelectItemProps {
190
332
  value: string;
@@ -192,32 +334,63 @@ export interface MultiSelectItemProps {
192
334
  className?: string;
193
335
  }
194
336
 
195
- export function MultiSelectItem({ children, value, className }: MultiSelectItemProps) {
337
+ export function MultiSelectItem({
338
+ children,
339
+ value,
340
+ className
341
+ }: MultiSelectItemProps) {
196
342
 
197
343
  const context = React.useContext(MultiSelectContext);
198
344
  if (!context) throw new Error("MultiSelectItem must be used inside a MultiSelect");
199
- const { fieldValue, setInputValue, onValueChangeInternal } = context;
345
+ const {
346
+ fieldValue,
347
+ onItemClick
348
+ } = context;
200
349
 
350
+ const isSelected = (fieldValue ?? []).includes(value);
201
351
  return <CommandPrimitive.Item
352
+ // value={value}
202
353
  onMouseDown={(e) => {
203
354
  e.preventDefault();
204
355
  e.stopPropagation();
205
356
  }}
206
357
  onSelect={(_) => {
207
- setInputValue("");
208
- onValueChangeInternal(value);
358
+ onItemClick(value);
209
359
  }}
210
360
  className={cls(
211
- (fieldValue ?? []).includes(value) ? "bg-slate-200 dark:bg-slate-950" : "",
361
+ "flex flex-row items-center gap-1.5",
362
+ isSelected ? "bg-slate-200 dark:bg-slate-950" : "",
212
363
  "cursor-pointer",
213
364
  "m-1",
214
365
  "ring-offset-transparent",
215
- "p-2 rounded aria-[selected=true]:outline-none aria-[selected=true]:ring-2 aria-[selected=true]:ring-primary aria-[selected=true]:ring-opacity-75 aria-[selected=true]:ring-offset-2",
366
+ "p-1 rounded aria-[selected=true]:outline-none aria-[selected=true]:ring-2 aria-[selected=true]:ring-primary aria-[selected=true]:ring-opacity-75 aria-[selected=true]:ring-offset-2",
216
367
  "aria-[selected=true]:bg-slate-100 aria-[selected=true]:dark:bg-slate-900",
217
368
  "cursor-pointer p-2 rounded aria-[selected=true]:bg-slate-100 aria-[selected=true]:dark:bg-slate-900",
218
369
  className
219
370
  )}
220
371
  >
372
+ <InnerCheckBox checked={isSelected}/>
221
373
  {children}
222
374
  </CommandPrimitive.Item>;
375
+
223
376
  }
377
+
378
+ function InnerCheckBox({ checked }: { checked: boolean }) {
379
+ return <div className={cls(
380
+ "p-2",
381
+ "w-8 h-8",
382
+ "inline-flex items-center justify-center text-sm font-medium focus:outline-none transition-colors ease-in-out duration-150",
383
+ )}>
384
+ <div
385
+ className={cls(
386
+ "border-2 relative transition-colors ease-in-out duration-150",
387
+ "w-4 h-4 rounded flex items-center justify-center",
388
+ (checked ? "bg-primary" : "bg-white dark:bg-slate-900"),
389
+ (checked) ? "text-slate-100 dark:text-slate-900" : "",
390
+ (checked ? "border-transparent" : "border-slate-800 dark:border-slate-200")
391
+ )}>
392
+ {checked && <Icon iconKey={"check"} size={16} className={"absolute"}/>}
393
+ </div>
394
+ </div>
395
+ }
396
+
@@ -60,7 +60,7 @@ export function Popover({
60
60
  <PopoverPrimitive.Portal container={portalContainer}>
61
61
  <PopoverPrimitive.Content
62
62
  className={cls(paperMixin,
63
- "PopoverContent shadow z-40", className)}
63
+ "PopoverContent z-40", className)}
64
64
  side={side}
65
65
  sideOffset={sideOffset}
66
66
  align={align}