Skip to content

Commit

Permalink
fix: fix loss of field focus after each character for search field (#262
Browse files Browse the repository at this point in the history
)

* fix: fix loss of field focus after each character for search field

* fix: update package-lock file
  • Loading branch information
ihor-romaniuk authored Dec 5, 2022
1 parent 2536b93 commit 75eeb45
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 10 deletions.
21 changes: 16 additions & 5 deletions src/components/GradesView/SearchControls.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,27 +18,38 @@ import messages from './SearchControls.messages';
export class SearchControls extends React.Component {
constructor(props) {
super(props);
this.onChange = this.onChange.bind(this);
this.onBlur = this.onBlur.bind(this);
this.onClear = this.onClear.bind(this);
this.onSubmit = this.onSubmit.bind(this);
}

/** Changing the search value stores the key in Gradebook. Currently unused */
onChange(searchValue) {
this.props.setSearchValue(searchValue);
onBlur(e) {
const { value } = e.target;
if (this.props.searchValue !== value) {
this.props.setSearchValue(value);
}
}

onClear() {
this.props.setSearchValue('');
this.props.fetchGrades();
}

onSubmit(value) {
if (this.props.searchValue !== value) {
this.props.setSearchValue(value);
}
this.props.fetchGrades();
}

render() {
return (
<div>
<SearchField
onSubmit={this.props.fetchGrades}
onSubmit={this.onSubmit}
inputLabel={<FormattedMessage {...messages.label} />}
onChange={this.onChange}
onBlur={this.onBlur}
onClear={this.onClear}
value={this.props.searchValue}
/>
Expand Down
29 changes: 26 additions & 3 deletions src/components/GradesView/SearchControls.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,18 +52,25 @@ describe('SearchControls', () => {
describe('Snapshots', () => {
test('basic snapshot', () => {
const wrapper = searchControls();
wrapper.instance().onChange = jest.fn().mockName('onChange');
wrapper.instance().onBlur = jest.fn().mockName('onBlur');
wrapper.instance().onClear = jest.fn().mockName('onClear');
wrapper.instance().onSubmit = jest.fn().mockName('onSubmit');
expect(wrapper.instance().render()).toMatchSnapshot();
});
});

describe('onChange', () => {
describe('onBlur', () => {
it('saves the changed search value to Gradebook state', () => {
const wrapper = searchControls();
wrapper.instance().onChange('bob');
wrapper.instance().onBlur({ target: { value: 'bob' } });
expect(props.setSearchValue).toHaveBeenCalledWith('bob');
});

it('doesnt save the same as previous value to Gradebook state', () => {
const wrapper = searchControls({ searchValue: 'bob' });
wrapper.instance().onBlur({ target: { value: 'bob' } });
expect(props.setSearchValue).not.toHaveBeenCalled();
});
});

describe('onChange', () => {
Expand All @@ -75,6 +82,22 @@ describe('SearchControls', () => {
});
});

describe('onSubmit', () => {
it('saves the changed search value to Gradebook state and calls fetchGrades', () => {
const wrapper = searchControls();
wrapper.instance().onSubmit('bob');
expect(props.setSearchValue).toHaveBeenCalledWith('bob');
expect(props.fetchGrades).toHaveBeenCalled();
});

it('doesnt save the same as previous value to Gradebook state and calls fetchGrades', () => {
const wrapper = searchControls({ searchValue: 'bob' });
wrapper.instance().onSubmit('bob');
expect(props.setSearchValue).not.toHaveBeenCalled();
expect(props.fetchGrades).toHaveBeenCalled();
});
});

describe('mapStateToProps', () => {
const testState = { never: 'gonna', give: 'you up' };
test('searchValue from app.searchValue', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ exports[`SearchControls Component Snapshots basic snapshot 1`] = `
id="gradebook.GradesView.search.label"
/>
}
onChange={[MockFunction onChange]}
onBlur={[MockFunction onBlur]}
onClear={[MockFunction onClear]}
onSubmit={[MockFunction fetchGrades]}
onSubmit={[MockFunction onSubmit]}
value="alice"
/>
<small
Expand Down

0 comments on commit 75eeb45

Please sign in to comment.