From 2a4cd84e109d23bf05f09bee4c2ec05abedb6cf5 Mon Sep 17 00:00:00 2001 From: Andrey Okonetchnikov Date: Wed, 21 Sep 2022 11:49:49 +0200 Subject: [PATCH] Improve types and add more examples for refs --- .../src/components/text-field.stories.tsx | 28 +++++ .../src/components/text-field.tsx | 107 +++++++++--------- 2 files changed, 82 insertions(+), 53 deletions(-) diff --git a/packages/design-system/src/components/text-field.stories.tsx b/packages/design-system/src/components/text-field.stories.tsx index bf155ed07f..9ebbb0cd0a 100644 --- a/packages/design-system/src/components/text-field.stories.tsx +++ b/packages/design-system/src/components/text-field.stories.tsx @@ -1,6 +1,7 @@ import React from "react"; import { ComponentStory } from "@storybook/react"; import { BrushIcon, ChevronDownIcon } from "@webstudio-is/icons"; +import { Button } from "./button"; import { Flex } from "./flex"; import { IconButton } from "./icon-button"; import { TextField } from "./text-field"; @@ -94,3 +95,30 @@ export const PrefixSuffix: ComponentStory = () => { ); }; + +export const Interactive: ComponentStory = () => { + const [value, setValue] = React.useState(""); + const wrapperRef = React.useRef(null); + const inputRef = React.useRef(null); + + return ( + + + + + + ); +}; diff --git a/packages/design-system/src/components/text-field.tsx b/packages/design-system/src/components/text-field.tsx index ec4164a2cd..7afac6b069 100644 --- a/packages/design-system/src/components/text-field.tsx +++ b/packages/design-system/src/components/text-field.tsx @@ -146,67 +146,68 @@ const TextFieldBase = styled("div", { }, }); -type TextFieldProps = Omit< +type TextFieldProps = Pick< React.ComponentProps, - "onChange" | "onFocus" | "onBlur" | "prefix" + "size" | "variant" | "state" | "css" > & - React.ComponentProps<"input"> & { + Omit, "prefix" | "children"> & { inputRef?: React.Ref; prefix?: React.ReactNode; suffix?: React.ReactNode; }; -export const TextField = React.forwardRef< - React.ElementRef, - TextFieldProps ->((props, forwardedRef) => { - const { - prefix, - suffix, - css, - disabled, - inputRef, - size, - state, - variant, - ...textFieldProps - } = props; - return ( - - {prefix && ( - - {prefix} - - )} +export const TextField = React.forwardRef( + (props, forwardedRef) => { + const { + prefix, + suffix, + css, + disabled, + inputRef, + size, + state, + variant, + ...textFieldProps + } = props; - + return ( + + {/* We want input to be the first element in DOM so it receives the focus first */} + - {suffix && ( - - {suffix} - - )} - - ); -}); + {prefix && ( + + {prefix} + + )} + + {suffix && ( + + {suffix} + + )} + + ); + } +); TextField.displayName = "TextField";