Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clear post autocomplete text upon selection #263

Merged
merged 1 commit into from
Jul 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/Components/AutoSuggest/AutoSuggest.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,13 @@ export default class AutoSuggest extends Component {

// when a suggestion is actually selected
onSuggestionSelected(event, { suggestion }) {
const { shouldClearOnSelect } = this.props;
this.props.onSuggestionSelected(
this.props.queryProperty.length ? suggestion[this.props.queryProperty] : suggestion,
);
if (shouldClearOnSelect) {
this.setState({ value: '' });
}
}

// Use your imagination to render suggestions.
Expand Down Expand Up @@ -150,6 +154,9 @@ AutoSuggest.propTypes = {

// props to pass to template
templateProps: PropTypes.shape({}),

// should the input be cleared upon selection
shouldClearOnSelect: PropTypes.bool,
};

AutoSuggest.defaultProps = {
Expand All @@ -168,4 +175,5 @@ AutoSuggest.defaultProps = {
className: undefined,
autoSuggestProps: {},
templateProps: {},
shouldClearOnSelect: false,
};
32 changes: 32 additions & 0 deletions src/Components/AutoSuggest/AutoSuggest.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -185,4 +185,36 @@ describe('AutoSuggestComponent', () => {
};
f();
});

it('clears the input onSuggestionSelected if shouldClearOnSelect === true', () => {
const wrapper = shallow(
<AutoSuggest
suggestions={suggestions}
getSuggestions={() => {}}
onSuggestionSelected={() => {}}
inputId="input"
label="label"
shouldClearOnSelect
/>,
);
wrapper.instance().setState({ value: 'text' });
wrapper.instance().onSuggestionSelected(null, { suggestion: suggestions[0] });
expect(wrapper.instance().state.value).toBe('');
});

it('does not clear the input onSuggestionSelected if shouldClearOnSelect === false', () => {
const wrapper = shallow(
<AutoSuggest
suggestions={suggestions}
getSuggestions={() => {}}
onSuggestionSelected={() => {}}
inputId="input"
label="label"
shouldClearOnSelect={false}
/>,
);
wrapper.instance().setState({ value: 'text' });
wrapper.instance().onSuggestionSelected(null, { suggestion: suggestions[0] });
expect(wrapper.instance().state.value).not.toBe('');
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ exports[`CDOAutoSuggest matches snapshot 1`] = `
onSuggestionsClearRequested={[Function]}
placeholder="Start typing CDO name"
queryProperty=""
shouldClearOnSelect={false}
suggestionTemplate={[Function]}
suggestions={
Array [
Expand Down Expand Up @@ -209,6 +210,7 @@ exports[`CDOAutoSuggest matches snapshot after toggling 1`] = `
onSuggestionsClearRequested={[Function]}
placeholder="Start typing CDO name"
queryProperty=""
shouldClearOnSelect={false}
suggestionTemplate={[Function]}
suggestions={
Array [
Expand Down
1 change: 1 addition & 0 deletions src/Components/SearchFilters/PostFilter/PostFilter.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ class PostFilter extends Component {
customInputProps={{
disabled: postSelectionDisabled,
}}
shouldClearOnSelect
/>
<div className="usa-grid-full tm-nested-accordions">
<Accordion>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ exports[`PostFilterComponent matches snapshot 1`] = `
onSuggestionsClearRequested={[Function]}
placeholder="Remove regional filters"
queryProperty="id"
shouldClearOnSelect={true}
suggestionTemplate={[Function]}
suggestions={Array []}
templateProps={Object {}}
Expand Down