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.

feat(Select): Implement better Select API

Andrey Okonetchnikov72f359347609
diff --git a/app/shared/design-system/components/select.stories.tsx b/app/shared/design-system/components/select.stories.tsx
index 00793bb33b..387a375f9d 100644
--- a/app/shared/design-system/components/select.stories.tsx
+++ b/app/shared/design-system/components/select.stories.tsx
@@ -1,4 +1,4 @@
-import { action } from "@storybook/addon-actions";
+import React from "react";
import { ComponentStory } from "@storybook/react";
import { Select } from "~/shared/design-system";
@@ -6,15 +6,17 @@ export default {
component: Select,
};
-const values = ["Apple", "Banana", "Orange", "Pear", "Grape"];
+const values = [
+ { value: "Apple", label: "Apple" },
+ { value: "Banana", label: "Banana" },
+ { value: "Orange", label: "Orange" },
+ { value: "Pear", label: "Pear" },
+ { value: "Grape", label: "Grape" },
+];
-export const Default: ComponentStory<typeof Select> = (args) => (
- <Select {...args} />
-);
-
-Default.args = {
- name: "fruit",
- onChange: action("onChange"),
- value: "Apple",
- options: values,
+export const Default: ComponentStory<typeof Select> = () => {
+ const [value, setValue] = React.useState();
+ return (
+ <Select name="fruit" options={values} value={value} onChange={setValue} />
+ );
};
diff --git a/app/shared/design-system/components/select.tsx b/app/shared/design-system/components/select.tsx
index 058dfbb19e..814c1be9c3 100644
--- a/app/shared/design-system/components/select.tsx
+++ b/app/shared/design-system/components/select.tsx
@@ -1,4 +1,4 @@
-import React from "react";
+import React, { Ref } from "react";
import { CSS, styled } from "../stitches.config";
import { CaretSortIcon } from "~/shared/icons";
@@ -61,17 +61,19 @@ const StyledCaretSortIcon = styled(CaretSortIcon, {
export type Option = { value: string; label: string };
-export type SelectProps<T> = React.ComponentProps<typeof StyledSelect> & {
+export type SelectProps<T = Option> = React.ComponentProps<
+ typeof StyledSelect
+> & {
options: T[];
value?: T;
onChange?: (value: T) => void;
css?: CSS;
};
-export const Select = React.forwardRef(function SelectBase<T extends Option>(
- { css, options, ...props }: SelectProps<T>,
- forwardedRef: FocusableRef<HTMLElement>
-) {
+const SelectBase = (
+ { css, options, ...props }: SelectProps,
+ forwardedRef: Ref<HTMLSelectElement>
+) => {
return (
<SelectWrapper css={css}>
<StyledSelect ref={forwardedRef} {...props}>
@@ -84,7 +86,8 @@ export const Select = React.forwardRef(function SelectBase<T extends Option>(
<StyledCaretSortIcon />
</SelectWrapper>
);
-});
+};
+export const Select = React.forwardRef(SelectBase);
Select.displayName = "Select";
Select.toString = () => `.${SelectWrapper.className}`;