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.

use new combobox in props

Oleg Isonen5f5cfae96b90
diff --git a/apps/designer/app/auth/login.tsx b/apps/designer/app/auth/login.tsx
index 8172ed3d86..9e2794fc14 100644
--- a/apps/designer/app/auth/login.tsx
+++ b/apps/designer/app/auth/login.tsx
@@ -1,9 +1,7 @@
import { LinksFunction, MetaFunction } from "@remix-run/node";
import { Form, useLoaderData } from "@remix-run/react";
import { useState } from "react";
-
import {
- Box,
Card,
Flex,
Heading,
@@ -12,7 +10,6 @@ import {
TextField,
} from "@webstudio-is/design-system";
import interStyles from "~/shared/font-faces/inter.css";
-
import { GithubIcon, CommitIcon, GoogleIcon } from "@webstudio-is/icons";
import { LoginButton } from "./components/login-button";
import loginStyles from "./login.css";
diff --git a/apps/designer/app/designer/features/props-panel/props-panel.tsx b/apps/designer/app/designer/features/props-panel/props-panel.tsx
index 74a599f36b..e698e2a6f0 100644
--- a/apps/designer/app/designer/features/props-panel/props-panel.tsx
+++ b/apps/designer/app/designer/features/props-panel/props-panel.tsx
@@ -10,19 +10,99 @@ import type { SelectedInstanceData } from "~/shared/canvas-components";
import {
Box,
Button,
- Combobox,
- ComboboxTextField,
- ComboboxPopperContent,
Grid,
+ TextField,
Tooltip,
+ useCombobox,
+ ComboboxPrimitive,
+ ComboboxContent,
+ ComboboxAnchor,
+ ComboboxListbox,
+ ComboboxListboxItem,
+ IconButton,
} from "@webstudio-is/design-system";
import {
PlusIcon,
TrashIcon,
ExclamationTriangleIcon,
+ ChevronDownIcon,
} from "@webstudio-is/icons";
import { handleChangePropType, usePropsLogic } from "./use-props-logic";
+type ComboboxProps = {
+ isReadonly: boolean;
+ isInvalid: boolean;
+ items: Array<string>;
+ value: string;
+ onItemSelect: (value: string | null) => void;
+ onSubmit: (value: string) => void;
+};
+
+export const Combobox = ({
+ isReadonly,
+ isInvalid,
+ items: itemsProp,
+ value,
+ onItemSelect,
+ onSubmit,
+}: ComboboxProps) => {
+ const {
+ items,
+ getInputProps,
+ getComboboxProps,
+ getToggleButtonProps,
+ getMenuProps,
+ getItemProps,
+ isOpen,
+ } = useCombobox({
+ items: itemsProp,
+ value,
+ itemToString: (item) => item ?? "",
+ onItemSelect,
+ });
+
+ return (
+ <ComboboxPrimitive>
+ <Box {...getComboboxProps()}>
+ <ComboboxAnchor>
+ <TextField
+ {...getInputProps()}
+ name="prop"
+ placeholder="Property"
+ readOnly={isReadonly}
+ state={isInvalid ? "invalid" : undefined}
+ suffix={
+ <IconButton {...getToggleButtonProps()}>
+ <ChevronDownIcon />
+ </IconButton>
+ }
+ onKeyPress={(event) => {
+ if (event.key === "Enter") {
+ onSubmit(event.currentTarget.value);
+ }
+ }}
+ />
+ </ComboboxAnchor>
+ <ComboboxContent align="start" sideOffset={5}>
+ <ComboboxListbox {...getMenuProps()}>
+ {isOpen &&
+ items.map((item, index) => {
+ return (
+ <ComboboxListboxItem
+ {...getItemProps({ item, index })}
+ key={index}
+ >
+ {item}
+ </ComboboxListboxItem>
+ );
+ })}
+ </ComboboxListbox>
+ </ComboboxContent>
+ </Box>
+ </ComboboxPrimitive>
+ );
+};
+
type PropertyProps = UserProp & {
component: Instance["component"];
onChange: handleChangePropType;
@@ -33,14 +113,14 @@ const Property = ({
id,
prop,
value,
- required,
+ required = false,
component,
onChange,
onDelete,
}: PropertyProps) => {
const meta = componentsMeta[component];
const argType = meta.argTypes?.[prop as keyof typeof meta.argTypes];
- const isInvalidProp =
+ const isInvalid =
prop != null &&
prop.length > 0 &&
typeof argType === "undefined" &&
@@ -56,30 +136,20 @@ const Property = ({
css={{ gridTemplateColumns: "1fr 1fr auto", alignItems: "center" }}
>
<Combobox
- name="prop"
- placeholder="Property"
items={allProps}
value={prop}
- itemToString={(item) => item ?? ""}
- onItemSelect={(value) => {
+ onItemSelect={(value: PropertyProps["value"] | null) => {
+ if (value !== null) {
+ onChange(id, "prop", value);
+ }
+ }}
+ onSubmit={(value) => {
onChange(id, "prop", value);
}}
- renderTextField={({ inputProps, toggleProps }) => (
- <ComboboxTextField
- toggleProps={toggleProps}
- inputProps={{
- ...inputProps,
- readOnly: required,
- state: isInvalidProp ? "invalid" : undefined,
- }}
- />
- )}
- renderPopperContent={(props) => (
- <ComboboxPopperContent {...props} align="start" sideOffset={5} />
- )}
+ isInvalid={isInvalid}
+ isReadonly={required}
/>
-
- {isInvalidProp ? (
+ {isInvalid ? (
<Tooltip content={`Invalid property name: ${prop}`}>
<ExclamationTriangleIcon width={12} height={12} />
</Tooltip>
diff --git a/packages/design-system/src/components/combobox.tsx b/packages/design-system/src/components/combobox.tsx
index ae936d67d5..95123404b8 100644
--- a/packages/design-system/src/components/combobox.tsx
+++ b/packages/design-system/src/components/combobox.tsx
@@ -61,9 +61,15 @@ export const ListboxItemBase: ForwardRefRenderFunction<
);
};
+export const ComboboxListbox = Listbox;
+
export const ComboboxListboxItem = forwardRef(ListboxItemBase);
-export const ComboboxPopperContent = PopperContent;
+export const ComboboxPrimitive = Popper;
+
+export const ComboboxContent = PopperContent;
+
+export const ComboboxAnchor = PopperAnchor;
type useComboboxProps<Item> = {
items: ReadonlyArray<Item>;
@@ -84,7 +90,7 @@ export function useCombobox<Item>({
itemToString,
onItemSelect,
onItemHighlight,
- stateReducer,
+ stateReducer = (state, { changes }) => changes,
}: useComboboxProps<Item>) {
const [filteredItems, setFilteredItems] = useState(items);
const cachedItems = useRef(items);