Skip to content

Commit

Permalink
feat(autocomplete): added isActive to state. (#1190)
Browse files Browse the repository at this point in the history
  • Loading branch information
RicardoNeves authored Jul 2, 2021
1 parent dca0515 commit b51252d
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 4 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "nukleus",
"version": "15.1.6",
"version": "15.1.7",
"description": "Shared components repo for kununu projects",
"main": "dist/components/index.js",
"repository": {
Expand Down
5 changes: 3 additions & 2 deletions src/components/Autocomplete/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ export default class Autocomplete extends React.Component {

state = {
hasInitialized: false,
isActive: this.props.autoFocus || false, // eslint-disable-line react/destructuring-assignment, react/no-unused-state
showError: false,
showNoSuggestionsText: false,
suggestions: this.props.data.items || [], // eslint-disable-line react/destructuring-assignment
Expand Down Expand Up @@ -134,7 +135,7 @@ export default class Autocomplete extends React.Component {
onFocus = (ev) => {
const {onFocus} = this.props;

this.setState({showNoSuggestionsText: true});
this.setState({isActive: true, showNoSuggestionsText: true}); // eslint-disable-line react/no-unused-state

// Prevents autoscroll if element is not
// in the DOM
Expand All @@ -149,7 +150,7 @@ export default class Autocomplete extends React.Component {
const {onBlur} = this.props;

this.hideNoSuggestionsText();
this.setState({hasInitialized: false});
this.setState({hasInitialized: false, isActive: false}); // eslint-disable-line react/no-unused-state
onBlur(ev);
}

Expand Down
2 changes: 1 addition & 1 deletion src/components/Autocomplete/story.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ storiesOf('Autocomplete', module)
</h3>

<Autocomplete
autofocus
autoFocus
data={staticData}
id="autocompletes"
label={text('label', 'Autocomplete')}
Expand Down
22 changes: 22 additions & 0 deletions tests/Autocomplete.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,28 @@ test('Bluring an Autocomplete calls the onBlur Event', () => {
expect(spyFunc.mock.calls.length).toBe(1);
});

test('Focus/Blur an Autocomplete sets isActive properly', () => {
const component = mount(staticAutocomplete);

expect(component.state().isActive).toEqual(false);
component.find('input').simulate('focus');
expect(component.state().isActive).toEqual(true);
component.find('input').simulate('blur');
expect(component.state().isActive).toEqual(false);
});

test('Passing autofocus initializes isActive with true', () => {
const component = mount(<Autocomplete
autoFocus
data={{items: []}}
id="autocompletes"
label="Autocomplete"
name="autocomplete"
/>);

expect(component.state().isActive).toEqual(true);
});

test('Changing an Autocomplete calls the onChange Event', () => {
const spyFunc = jest.fn();
const component = mount(<Autocomplete
Expand Down

0 comments on commit b51252d

Please sign in to comment.