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(design-system): Introduce `prefix` and `suffix` props to TextField.

Andrey Okonetchnikovebe6adc781b7
diff --git a/packages/design-system/src/components/combobox.tsx b/packages/design-system/src/components/combobox.tsx
index 11eadbde54..90691c8bab 100644
--- a/packages/design-system/src/components/combobox.tsx
+++ b/packages/design-system/src/components/combobox.tsx
@@ -32,19 +32,15 @@ const ComboboxTextFieldBase: ForwardRefRenderFunction<
ComboboxTextFieldProps<BaseItem>
> = ({ inputProps, toggleProps }, ref) => {
return (
- <Box ref={ref} css={{ position: "relative" }}>
- <TextField css={{ paddingRight: "$4" }} {...inputProps} />
- <IconButton
- {...toggleProps}
- css={{
- position: "absolute",
- transform: "translateX(-100%)",
- width: "$4",
- }}
- >
- <ChevronDownIcon />
- </IconButton>
- </Box>
+ <TextField
+ ref={ref}
+ suffix={
+ <IconButton {...toggleProps}>
+ <ChevronDownIcon />
+ </IconButton>
+ }
+ {...inputProps}
+ />
);
};
@@ -198,7 +194,7 @@ export const Combobox = <Item extends BaseItem>({
return (
<Popper>
<Box {...comboboxProps}>
- <PopperAnchor asChild>
+ <PopperAnchor>
{renderTextField({ inputProps, toggleProps, highlightedItem })}
</PopperAnchor>
{renderPopperContent({
diff --git a/packages/design-system/src/components/text-field.stories.tsx b/packages/design-system/src/components/text-field.stories.tsx
index 9b2a57695f..452f2fd3fd 100644
--- a/packages/design-system/src/components/text-field.stories.tsx
+++ b/packages/design-system/src/components/text-field.stories.tsx
@@ -1,7 +1,10 @@
-import React from "react";
import { ComponentStory } from "@storybook/react";
-import { TextField } from "./text-field";
+import { BrushIcon, ChevronDownIcon } from "@webstudio-is/icons";
+import React from "react";
+import { Box } from "./box";
import { Flex } from "./flex";
+import { IconButton } from "./icon-button";
+import { TextField } from "./text-field";
export default {
component: TextField,
@@ -11,8 +14,16 @@ export const Default: ComponentStory<typeof TextField> = () => {
return <TextField />;
};
-export const Type: ComponentStory<typeof TextField> = () => {
- return <TextField type="number" />;
+export const NativeProps: ComponentStory<typeof TextField> = () => {
+ return (
+ <Flex direction="column" gap={3}>
+ <TextField placeholder="This is a placeholder" />
+ <TextField disabled placeholder="This is a disabled placeholder" />
+ <TextField type="number" value={25} />
+ <TextField readOnly value="Read-only" />
+ <TextField disabled value="Disabled" />
+ </Flex>
+ );
};
export const Sizes: ComponentStory<typeof TextField> = () => {
@@ -43,11 +54,44 @@ export const State: ComponentStory<typeof TextField> = () => {
);
};
-export const Cursor: ComponentStory<typeof TextField> = () => {
+export const PrefixSuffix: ComponentStory<typeof TextField> = () => {
return (
<Flex direction="column" gap={3}>
- <TextField cursor="default" />
- <TextField cursor="text" />
+ <TextField
+ prefix={<BrushIcon />}
+ suffix={
+ <IconButton size={1}>
+ <ChevronDownIcon />
+ </IconButton>
+ }
+ />
+ <TextField
+ state="invalid"
+ prefix={<BrushIcon />}
+ suffix={
+ <IconButton>
+ <ChevronDownIcon />
+ </IconButton>
+ }
+ />
+ <TextField
+ size={2}
+ prefix={<BrushIcon />}
+ suffix={
+ <IconButton size={2}>
+ <ChevronDownIcon />
+ </IconButton>
+ }
+ />
+ <TextField
+ disabled
+ prefix={<BrushIcon />}
+ suffix={
+ <IconButton>
+ <ChevronDownIcon />
+ </IconButton>
+ }
+ />
</Flex>
);
};
diff --git a/packages/design-system/src/components/text-field.tsx b/packages/design-system/src/components/text-field.tsx
index 440e4ffb2f..1eff37b98c 100644
--- a/packages/design-system/src/components/text-field.tsx
+++ b/packages/design-system/src/components/text-field.tsx
@@ -1,29 +1,32 @@
+import React from "react";
import { styled } from "../stitches.config";
+import { Flex } from "./flex";
-export const TextField = styled("input", {
+const InputBase = styled("input", {
// Reset
appearance: "none",
borderWidth: "0",
+ backgroundColor: "transparent",
boxSizing: "border-box",
fontFamily: "inherit",
+ fontSize: "inherit",
margin: "0",
- outline: "none",
padding: "0",
- width: "100%",
+ flexGrow: 1,
+ flexShrink: 1,
+ outline: "none",
WebkitTapHighlightColor: "rgba(0,0,0,0)",
+
+ // Focus should start on the input element then move to prefix and suffix elements.
+ // DOM order reflects focus path and visually we use order to put them into the correct visual order.
+ order: 1,
+
"&::before": {
boxSizing: "border-box",
},
"&::after": {
boxSizing: "border-box",
},
-
- // Custom
- backgroundColor: "$loContrast",
- boxShadow: "inset 0 0 0 1px $colors$slate7",
- color: "$hiContrast",
- fontVariantNumeric: "tabular-nums",
-
"&:-webkit-autofill": {
boxShadow: "inset 0 0 0 1px $colors$blue6, inset 0 0 0 100px $colors$blue3",
},
@@ -32,8 +35,30 @@ export const TextField = styled("input", {
fontFamily: "$untitled",
color: "$hiContrast",
},
+ "&::placeholder": {
+ color: "$slate9",
+ },
+
+ "&:disabled": {
+ color: "$slate8",
+ // @todo: This is not working now bue to cursor variant. Is it needed?
+ cursor: "not-allowed",
+ "&::placeholder": {
+ color: "$slate7",
+ },
+ },
+});
- "&:focus": {
+const TextFieldBase = styled("div", {
+ // Custom
+ display: "flex",
+ gap: "$1",
+ backgroundColor: "$loContrast",
+ boxShadow: "inset 0 0 0 1px $colors$slate7",
+ color: "$hiContrast",
+ fontVariantNumeric: "tabular-nums",
+
+ "&:focus-within": {
boxShadow:
"inset 0px 0px 0px 1px $colors$blue8, 0px 0px 0px 1px $colors$blue8",
"&:-webkit-autofill": {
@@ -41,19 +66,12 @@ export const TextField = styled("input", {
"inset 0px 0px 0px 1px $colors$blue8, 0px 0px 0px 1px $colors$blue8, inset 0 0 0 100px $colors$blue3",
},
},
- "&::placeholder": {
- color: "$slate9",
- },
- "&:disabled": {
+
+ "&[aria-disabled=true]": {
pointerEvents: "none",
backgroundColor: "$slate2",
- color: "$slate8",
- cursor: "not-allowed",
- "&::placeholder": {
- color: "$slate7",
- },
},
- "&:read-only": {
+ "&:has(input:read-only)": {
backgroundColor: "$slate2",
"&:focus": {
boxShadow: "inset 0px 0px 0px 1px $colors$slate7",
@@ -71,24 +89,18 @@ export const TextField = styled("input", {
variants: {
size: {
"1": {
+ px: "$1",
borderRadius: "$1",
- height: "$5",
fontSize: "$1",
- px: "$1",
+ height: "$5",
lineHeight: "$sizes$5",
- "&:-webkit-autofill::first-line": {
- fontSize: "$1",
- },
},
"2": {
+ px: "$2",
borderRadius: "$2",
- height: "$6",
fontSize: "$3",
- px: "$2",
+ height: "$6",
lineHeight: "$sizes$6",
- "&:-webkit-autofill::first-line": {
- fontSize: "$3",
- },
},
},
variant: {
@@ -129,19 +141,72 @@ export const TextField = styled("input", {
},
},
},
- cursor: {
- default: {
- cursor: "default",
- "&:focus": {
- cursor: "text",
- },
- },
- text: {
- cursor: "text",
- },
- },
},
defaultVariants: {
size: "1",
},
});
+
+type TextFieldProps = Omit<
+ React.ComponentProps<typeof TextFieldBase> & React.ComponentProps<"input">,
+ "prefix"
+> & {
+ inputRef?: React.Ref<HTMLInputElement>;
+ prefix?: React.ReactNode;
+ suffix?: React.ReactNode;
+};
+
+export const TextField = React.forwardRef<
+ React.ElementRef<typeof TextFieldBase>,
+ TextFieldProps
+>((props, forwardedRef) => {
+ const {
+ prefix,
+ suffix,
+ css,
+ disabled,
+ inputRef,
+ size,
+ state,
+ variant,
+ ...textFieldProps
+ } = props;
+ return (
+ <TextFieldBase
+ aria-disabled={disabled}
+ ref={forwardedRef}
+ size={size}
+ state={state}
+ variant={variant}
+ css={css}
+ >
+ {prefix && (
+ <Flex
+ css={{
+ alignItems: "center",
+ flexShrink: 0,
+ order: 0,
+ }}
+ >
+ {prefix}
+ </Flex>
+ )}
+
+ <InputBase ref={inputRef} disabled={disabled} {...textFieldProps} />
+
+ {suffix && (
+ <Flex
+ css={{
+ alignItems: "center",
+ flexShrink: 0,
+ order: 2,
+ }}
+ >
+ {suffix}
+ </Flex>
+ )}
+ </TextFieldBase>
+ );
+});
+
+TextField.displayName = "TextField";