Skip to content

Commit

Permalink
Merge pull request #990 from Shopify/985-textfield-focus
Browse files Browse the repository at this point in the history
[TextField] Fix `focused` prop so it triggers focus state
  • Loading branch information
amireynoso committed Feb 21, 2019
2 parents 9dd2ce4 + 0a8266e commit 53d003b
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 17 deletions.
1 change: 1 addition & 0 deletions UNRELEASED.md
Expand Up @@ -14,6 +14,7 @@ Use [the changelog guidelines](https://git.io/polaris-changelog-guidelines) to f

### Bug fixes

- Fixed the `focused` prop on `TextField` so it sets the focus state ([#990](https://github.com/Shopify/polaris-react/pull/990))
- Resolved an unsupported `React.Fragment` syntax ([#1080](https://github.com/Shopify/polaris-react/pull/1080))
- Constrained `DropZone` height based on inherited wrapper height [#908](https://github.com/Shopify/polaris-react/pull/908)

Expand Down
22 changes: 15 additions & 7 deletions src/components/TextField/TextField.tsx
Expand Up @@ -133,18 +133,26 @@ class TextField extends React.PureComponent<CombinedProps, State> {

this.state = {
height: null,
focus: false,
focus: props.focused || false,
id: props.id || getUniqueID(),
};
}

componentDidUpdate({focused}: CombinedProps) {
if (
this.input &&
focused !== this.props.focused &&
this.props.focused === true
) {
componentDidMount() {
if (!this.props.focused) {
return;
}

this.input.focus();
}

componentDidUpdate({focused: wasFocused}: CombinedProps) {
const {focused} = this.props;

if (!wasFocused && focused) {
this.input.focus();
} else if (wasFocused && !focused) {
this.input.blur();
}
}

Expand Down
46 changes: 36 additions & 10 deletions src/components/TextField/tests/TextField.test.tsx
Expand Up @@ -58,16 +58,6 @@ describe('<TextField />', () => {
expect(input.prop('prefix')).toBeUndefined();
});

it('focuses input and calls onFocus() when focused prop has been updated to true', () => {
const element = mountWithAppProvider(
<TextField label="TextField" onChange={noop} />,
);
element.setProps({focused: true});
expect(element.getDOMNode().querySelector('input')).toBe(
document.activeElement,
);
});

describe('onChange()', () => {
it('is called with the new value', () => {
const spy = jest.fn();
Expand Down Expand Up @@ -143,6 +133,42 @@ describe('<TextField />', () => {
});
});

describe('focused', () => {
it('input is in focus state if focused is true', () => {
const element = mountWithAppProvider(
<TextField label="TextField" onChange={noop} focused />,
);

expect(element.getDOMNode().querySelector('input')).toBe(
document.activeElement,
);
});

it('focuses input if focused is toggled', () => {
const element = mountWithAppProvider(
<TextField label="TextField" onChange={noop} />,
);

element.setProps({focused: true});

expect(element.getDOMNode().querySelector('input')).toBe(
document.activeElement,
);
});

it('blurs input if focused is toggled', () => {
const element = mountWithAppProvider(
<TextField label="TextField" onChange={noop} focused />,
);

element.setProps({focused: false});

expect(element.getDOMNode().querySelector('input')).not.toBe(
document.activeElement,
);
});
});

describe('autoComplete', () => {
it('defaults to no autoComplete attribute', () => {
const textField = shallowWithAppProvider(
Expand Down

0 comments on commit 53d003b

Please sign in to comment.