Skip to content

Commit

Permalink
fix(#2336): Fields without description are losing focus when an error…
Browse files Browse the repository at this point in the history
… is shown for the first time.

🐛 Bug Report
If you have a TextField without a description, the focus on the field is lost as soon as an error is shown for the first time. This probably happens for all the fields that have Helper Text option.

🤔 Expected Behavior
Field should keep focus.

😯 Current Behavior
Field is losing the focus when the error is shown.

💁 Possible Solution
Probably use a useLayoutEffect to restore focus to the input if the input had focus previously.

🔦 Context
💻 Code Sample
https://codesandbox.io/s/lost-focus-44wtq

🌍 Your Environment
Software	Version(s)
react-spectrum	3.14.0
Browser	Chrome
Operating System	macOS 11.6
  • Loading branch information
majornista committed Oct 5, 2021
1 parent b4a3821 commit 1ae7d1a
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 24 deletions.
27 changes: 6 additions & 21 deletions packages/@react-spectrum/label/src/Field.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -76,27 +76,12 @@ function Field(props: SpectrumFieldProps, ref: RefObject<HTMLElement>) {
showErrorIcon={showErrorIcon} />
);

let renderChildren = () => {
if (hasHelpText) {
if (labelPosition === 'side') {
return (
<Flex direction="column" UNSAFE_className={classNames(labelStyles, 'spectrum-Field-wrapper')}>
{children}
{renderHelpText()}
</Flex>
);
}

return (
<>
{children}
{renderHelpText()}
</>
);
}

return children;
};
let renderChildren = () => (
<Flex direction="column" UNSAFE_className={classNames(labelStyles, 'spectrum-Field-wrapper')}>
{children}
{hasHelpText && renderHelpText()}
</Flex>
);

return (
<div
Expand Down
2 changes: 1 addition & 1 deletion packages/@react-spectrum/label/test/Field.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ describe('Field', function () {
let {getByRole} = renderField({ref});
let field = getByRole('textbox').closest('div');

expect(ref.current).toBe(field);
expect(ref.current).toBe(field.parentNode);
});
describe('help text', function () {
describe('description', function () {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,10 @@ describe('NumberField', function () {

let root = props.label ? container.firstChild.firstChild : undefined;
container = within(container).queryByRole('group');
let textField = container.firstChild;
let textField = within(container).queryByRole('textbox');
let buttons = within(container).queryAllByRole('button');
let incrementButton = buttons[0];
let decrementButton = buttons[1];
textField = textField.firstChild;
return {
root,
container,
Expand Down

0 comments on commit 1ae7d1a

Please sign in to comment.