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.

Clicking anywhere inside the TextField should focus it

Andrey Okonetchnikovd3167ffadca4
diff --git a/packages/design-system/package.json b/packages/design-system/package.json
index cac926dd46..e42dd3a483 100644
--- a/packages/design-system/package.json
+++ b/packages/design-system/package.json
@@ -63,6 +63,7 @@
"@radix-ui/react-tooltip": "^1.0.0",
"@radix-ui/react-use-layout-effect": "^1.0.0",
"@react-aria/interactions": "^3.11.0",
+ "@react-aria/utils": "^3.13.3",
"@stitches/react": "^1.2.8",
"@webstudio-is/icons": "*",
"react-hot-toast": "^2.3.0"
diff --git a/packages/design-system/src/components/text-field.stories.tsx b/packages/design-system/src/components/text-field.stories.tsx
index 055ce3e114..28b2bef61e 100644
--- a/packages/design-system/src/components/text-field.stories.tsx
+++ b/packages/design-system/src/components/text-field.stories.tsx
@@ -214,3 +214,31 @@ FocusEvents.play = async ({ args, canvasElement }) => {
await waitFor(() => expect(args.onFocus).toHaveBeenCalledTimes(1));
await waitFor(() => expect(args.onBlur).toHaveBeenCalledTimes(1));
};
+
+export const ClickCapture: ComponentStory<typeof TextField> = (args) => {
+ return (
+ <TextField
+ name="click"
+ placeholder="Click on the icon to focus input"
+ prefix={
+ <Flex title="icon">
+ <BrushIcon />
+ </Flex>
+ }
+ suffix={
+ <IconButton>
+ <ChevronDownIcon />
+ </IconButton>
+ }
+ {...args}
+ />
+ );
+};
+ClickCapture.play = async ({ canvasElement }) => {
+ const canvas = within(canvasElement);
+ const input = canvas.getByPlaceholderText("Click on the icon to focus input");
+ await userEvent.click(canvas.getByTitle("icon"));
+ await waitFor(() => expect(input).toHaveFocus());
+ await userEvent.click(canvas.getByRole("button"));
+ await waitFor(() => expect(input).toHaveFocus());
+};
diff --git a/packages/design-system/src/components/text-field.tsx b/packages/design-system/src/components/text-field.tsx
index 68a1078cc6..c6e79b1ae8 100644
--- a/packages/design-system/src/components/text-field.tsx
+++ b/packages/design-system/src/components/text-field.tsx
@@ -1,3 +1,4 @@
+import { mergeRefs } from "@react-aria/utils";
import React from "react";
import { useFocusWithin } from "@react-aria/interactions";
import { styled } from "../stitches.config";
@@ -171,6 +172,12 @@ export const TextField = React.forwardRef<HTMLDivElement, TextFieldProps>(
...textFieldProps
} = props;
+ const internalInputRef = React.useRef<HTMLInputElement>(null);
+
+ const focusInnerInput = React.useCallback(() => {
+ internalInputRef.current?.focus();
+ }, [internalInputRef]);
+
const { focusWithinProps } = useFocusWithin({
isDisabled: disabled,
onFocusWithin: (event) => {
@@ -192,9 +199,14 @@ export const TextField = React.forwardRef<HTMLDivElement, TextFieldProps>(
variant={variant}
css={css}
{...focusWithinProps}
+ onClickCapture={focusInnerInput}
>
{/* We want input to be the first element in DOM so it receives the focus first */}
- <InputBase disabled={disabled} {...textFieldProps} ref={inputRef} />
+ <InputBase
+ disabled={disabled}
+ {...textFieldProps}
+ ref={mergeRefs(internalInputRef, inputRef ?? null)}
+ />
{prefix && (
<Flex