Skip to content

Open source website builder and Webflow alternative. Webstudio is an advanced visual builder that connects to any headless CMS, supports all CSS properties, and can be hosted anywhere, including with us.

WIP on downshift implementation

Andrey Okonetchnikov7c1099c17942
diff --git a/apps/designer/app/shared/design-system/components/combobox.stories.tsx b/apps/designer/app/shared/design-system/components/combobox.stories.tsx
index cea689061e..b6ed6d5f02 100644
--- a/apps/designer/app/shared/design-system/components/combobox.stories.tsx
+++ b/apps/designer/app/shared/design-system/components/combobox.stories.tsx
@@ -37,7 +37,9 @@ export const CustomInput: ComponentStory<typeof Combobox> = () => {
options={options}
value={value}
onOptionSelect={setValue}
- onOptionHighlight={(option) => {}}
+ onOptionHighlight={(option) => {
+ console.log(option);
+ }}
disclosure={(props) => (
<TextField
{...props}
diff --git a/apps/designer/app/shared/design-system/components/combobox.tsx b/apps/designer/app/shared/design-system/components/combobox.tsx
index e414dc52a2..5e8aa77e7b 100644
--- a/apps/designer/app/shared/design-system/components/combobox.tsx
+++ b/apps/designer/app/shared/design-system/components/combobox.tsx
@@ -1,8 +1,18 @@
-import { type ComponentProps, useState, useEffect } from "react";
+import { CheckIcon } from "@radix-ui/react-icons";
+import { useCombobox } from "downshift";
+import { matchSorter } from "match-sorter";
+import { type ComponentProps, useState } from "react";
+import {
+ Popover,
+ PopoverContent,
+ PopoverTrigger,
+ styled,
+} from "~/shared/design-system";
+import { itemCss } from "~/shared/design-system/components/menu";
+import { panelStyles } from "~/shared/design-system/components/panel";
import { ChevronDownIcon } from "~/shared/icons";
-import { TextField } from "./text-field";
import { IconButton } from "./icon-button";
-import { MenuAnchor, Menu, MenuContent, MenuItem } from "./menu";
+import { TextField } from "./text-field";
type BaseOption = { label: string };
@@ -21,6 +31,9 @@ type ComboboxProps<Option> = {
disclosure?: (props: DisclosureProps) => JSX.Element;
};
+const Listbox = styled("ul", panelStyles, { margin: 0 });
+const ListboxItem = styled("li", itemCss);
+
export const Combobox = <Option extends BaseOption>({
options,
value,
@@ -29,62 +42,62 @@ export const Combobox = <Option extends BaseOption>({
onOptionHighlight,
disclosure = (props) => <TextField {...props} />,
}: ComboboxProps<Option>) => {
- const [isOpen, setIsOpen] = useState(false);
- const [textValue, setTextValue] = useState(getTextValue<Option>(value));
-
- useEffect(() => {
- setTextValue(getTextValue<Option>(value));
- }, [value]);
+ const [items, setItems] = useState(options);
+ const {
+ isOpen,
+ // getToggleButtonProps,
+ // getLabelProps,
+ getMenuProps,
+ getInputProps,
+ getComboboxProps,
+ highlightedIndex,
+ getItemProps,
+ selectedItem,
+ } = useCombobox({
+ onInputValueChange({ inputValue }) {
+ const filteredItems = matchSorter(options, inputValue, {
+ keys: ["label", "value"],
+ });
+ setItems(filteredItems);
+ },
+ items,
+ selectedItem: value,
+ itemToString(item) {
+ return item ? item.label : "";
+ },
+ onSelectedItemChange({ selectedItem }) {
+ onOptionSelect?.(selectedItem);
+ },
+ onHighlightedIndexChange({ highlightedIndex }) {
+ onOptionHighlight?.(items[highlightedIndex]);
+ },
+ });
return (
- <Menu open={isOpen} modal={true} onOpenChange={setIsOpen}>
- <MenuAnchor asChild>
- {disclosure({
- name,
- autoComplete: "off",
- value: textValue,
- onChange: (event) => {
- setTextValue(event.target.value);
- },
- onKeyDown: (event) => {
- switch (event.key) {
- case "ArrowDown":
- case "ArrowUp": {
- setIsOpen(true);
- break;
- }
- case "Enter": {
- break;
- }
- }
- },
+ <div {...getComboboxProps()}>
+ {disclosure(getInputProps({ name }))}
+ <IconButton variant="ghost" size="1">
+ <ChevronDownIcon />
+ </IconButton>
+ <Listbox {...getMenuProps()}>
+ {items.map((item, index) => {
+ return (
+ <ListboxItem
+ key={index}
+ {...getItemProps({
+ item,
+ index,
+ disabled: item.disabled,
+ ...(item.disabled ? { "data-disabled": true } : {}),
+ ...(highlightedIndex === index ? { "data-found": true } : {}),
+ })}
+ >
+ {getTextValue<Option>(item)}
+ {selectedItem === item && <CheckIcon />}
+ </ListboxItem>
+ );
})}
- <IconButton variant="ghost" size="1">
- <ChevronDownIcon />
- </IconButton>
- </MenuAnchor>
- <MenuContent loop portalled asChild>
- <div>
- {options.map((option, index) => {
- return (
- <MenuItem
- key={index}
- onMouseEnter={() => {
- // onOptionHighlight(option);
- }}
- onFocus={() => {
- // onOptionHighlight(option);
- }}
- onSelect={() => {
- //onOptionSelect(option);
- }}
- >
- {getTextValue<Option>(option)}
- </MenuItem>
- );
- })}
- </div>
- </MenuContent>
- </Menu>
+ </Listbox>
+ </div>
);
};
diff --git a/apps/designer/package.json b/apps/designer/package.json
index 5494b629f3..44eb60c0d1 100644
--- a/apps/designer/package.json
+++ b/apps/designer/package.json
@@ -59,6 +59,7 @@
"bson-objectid": "^2.0.2",
"camelcase": "^6.3.0",
"debug": "^4.3.4",
+ "downshift": "^6.1.7",
"hyphenate-style-name": "^1.0.4",
"immer": "^9.0.12",
"immerhin": "^0.1.2",
@@ -69,6 +70,7 @@
"lodash.merge": "^4.6.2",
"lodash.noop": "^3.0.1",
"lodash.snakecase": "^4.1.1",
+ "match-sorter": "^6.3.1",
"mitt": "^3.0.0",
"nanoid": "^3.2.0",
"react": "^17.0.2",
diff --git a/yarn.lock b/yarn.lock
index e7ba9efbb6..d84da9d3c8 100644
--- a/yarn.lock
+++ b/yarn.lock
@@ -1088,6 +1088,13 @@
dependencies:
regenerator-runtime "^0.13.4"
+"@babel/runtime@^7.14.8":
+ version "7.18.9"
+ resolved "https://registry.yarnpkg.com/@babel/runtime/-/runtime-7.18.9.tgz#b4fcfce55db3d2e5e080d2490f608a3b9f407f4a"
+ integrity sha512-lkqXDcvlFT5rvEjiu6+QYO+1GXrEHRo2LOtS7E4GtX5ESIZOgepqsZBVIj6Pv+a6zqsya9VCgiK1KAK4BvJDAw==
+ dependencies:
+ regenerator-runtime "^0.13.4"
+
"@babel/runtime@~7.5.4":
version "7.5.5"
resolved "https://registry.yarnpkg.com/@babel/runtime/-/runtime-7.5.5.tgz#74fba56d35efbeca444091c7850ccd494fd2f132"
@@ -6273,6 +6280,11 @@ compression@^1.7.4:
safe-buffer "5.1.2"
vary "~1.1.2"
+compute-scroll-into-view@^1.0.17:
+ version "1.0.17"
+ resolved "https://registry.yarnpkg.com/compute-scroll-into-view/-/compute-scroll-into-view-1.0.17.tgz#6a88f18acd9d42e9cf4baa6bec7e0522607ab7ab"
+ integrity sha512-j4dx+Fb0URmzbwwMUrhqWM2BEWHdFGx+qZ9qqASHRPqvTYdqvWnHg0H1hIbcyLnvgnoNAVMlwkepyqM3DaIFUg==
+
version "0.0.1"
resolved "https://registry.yarnpkg.com/concat-map/-/concat-map-0.0.1.tgz#d8a96bd77fd68df7793a73036a3ba0d5405d477b"
@@ -7013,6 +7025,17 @@ dotenv@^8.0.0:
resolved "https://registry.yarnpkg.com/dotenv/-/dotenv-8.6.0.tgz#061af664d19f7f4d8fc6e4ff9b584ce237adcb8b"
integrity sha512-IrPdXQsk2BbzvCBGBOTmmSH5SodmqZNt4ERAZDmW4CT+tL8VtvinqywuANaFu4bOMWki16nqf0e4oC0QIaDr/g==
+downshift@^6.1.7:
+ version "6.1.7"
+ resolved "https://registry.yarnpkg.com/downshift/-/downshift-6.1.7.tgz#fdb4c4e4f1d11587985cd76e21e8b4b3fa72e44c"
+ integrity sha512-cVprZg/9Lvj/uhYRxELzlu1aezRcgPWBjTvspiGTVEU64gF5pRdSRKFVLcxqsZC637cLAGMbL40JavEfWnqgNg==
+ dependencies:
+ "@babel/runtime" "^7.14.8"
+ compute-scroll-into-view "^1.0.17"
+ prop-types "^15.7.2"
+ react-is "^17.0.2"
+ tslib "^2.3.0"
+
duplexify@^3.4.2, duplexify@^3.5.0, duplexify@^3.6.0:
version "3.7.1"
resolved "https://registry.yarnpkg.com/duplexify/-/duplexify-3.7.1.tgz#2a4df5317f6ccfd91f86d6fd25d8d8a103b88309"
@@ -11197,6 +11220,14 @@ markdown-extensions@^1.0.0:
resolved "https://registry.yarnpkg.com/markdown-extensions/-/markdown-extensions-1.1.1.tgz#fea03b539faeaee9b4ef02a3769b455b189f7fc3"
integrity sha512-WWC0ZuMzCyDHYCasEGs4IPvLyTGftYwh6wIEOULOF0HXcqZlhwRzrK0w2VUlxWA98xnvb/jszw4ZSkJ6ADpM6Q==
+match-sorter@^6.3.1:
+ version "6.3.1"
+ resolved "https://registry.yarnpkg.com/match-sorter/-/match-sorter-6.3.1.tgz#98cc37fda756093424ddf3cbc62bfe9c75b92bda"
+ integrity sha512-mxybbo3pPNuA+ZuCUhm5bwNkXrJTbsk5VWbR5wiwz/GC6LIiegBGn2w3O08UG/jdbYLinw51fSQ5xNU1U3MgBw==
+ dependencies:
+ "@babel/runtime" "^7.12.5"
+ remove-accents "0.4.2"
+
material-colors@^1.2.1:
version "1.2.6"
resolved "https://registry.yarnpkg.com/material-colors/-/material-colors-1.2.6.tgz#6d1958871126992ceecc72f4bcc4d8f010865f46"
@@ -13996,6 +14027,11 @@ remix-auth@^3.0.0, remix-auth@^3.1.0, remix-auth@^3.2.2:
dependencies:
uuid "^8.3.2"
+ version "0.4.2"
+ resolved "https://registry.yarnpkg.com/remove-accents/-/remove-accents-0.4.2.tgz#0a43d3aaae1e80db919e07ae254b285d9e1c7bb5"
+ integrity sha512-7pXIJqJOq5tFgG1A2Zxti3Ht8jJF337m4sowbuHsW30ZnkQFnDzy9qBNhgzX8ZLW4+UBcXiiR7SwR6pokHsxiA==
+
remove-trailing-separator@^1.0.1:
version "1.1.0"
resolved "https://registry.yarnpkg.com/remove-trailing-separator/-/remove-trailing-separator-1.1.0.tgz#c24bce2a283adad5bc3f58e0d48249b92379d8ef"
@@ -15541,7 +15577,7 @@ tslib@^1.0.0, tslib@^1.13.0, tslib@^1.8.1, tslib@^1.9.3:
resolved "https://registry.yarnpkg.com/tslib/-/tslib-1.14.1.tgz#cf2d38bdc34a134bcaf1091c41f6619e2f672d00"
integrity sha512-Xni35NKzjgMrwevysHTCArtLDpPvye8zV/0E4EyYn43P7/7qvQwPh9BGkHewbMulVntbigmcT7rdX3BNo9wRJg==
-tslib@^2.0.0, tslib@^2.0.1, tslib@^2.0.3, tslib@^2.1.0:
+tslib@^2.0.0, tslib@^2.0.1, tslib@^2.0.3, tslib@^2.1.0, tslib@^2.3.0:
version "2.4.0"
resolved "https://registry.yarnpkg.com/tslib/-/tslib-2.4.0.tgz#7cecaa7f073ce680a05847aa77be941098f36dc3"
integrity sha512-d6xOpEDfsi2CZVlPQzGeux8XMwLT9hssAsaPYExaQMuYskwb+x1x7J371tWlbBdWHroy99KnVB6qIkUbs5X3UQ==