Skip to content

Commit

Permalink
[RFR] Fix AutocompleteInput by removing auto selection altogether
Browse files Browse the repository at this point in the history
  • Loading branch information
djhi committed Jan 30, 2019
1 parent 5a18708 commit caf03e9
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 65 deletions.
48 changes: 5 additions & 43 deletions packages/ra-ui-materialui/src/input/AutocompleteInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -211,43 +211,11 @@ export class AutocompleteInput extends React.Component {
};

handleMatchSuggestionOrFilter = inputValue => {
const { choices, inputValueMatcher, input } = this.props;

const matches =
inputValue &&
choices.filter(it =>
inputValueMatcher(inputValue, it, this.getSuggestionText)
);

if (matches.length === 1) {
const match = matches[0];
const nextId = this.getSuggestionValue(match);
const suggestionText = this.getSuggestionText(match);

if (this.state.inputValue !== nextId) {
this.setState(
{
inputValue: nextId,
searchText: suggestionText, // The searchText could be whatever the inputvalue matcher likes, so sanitize it
selectedItem: match,
suggestions: [match],
},
() => input.onChange(nextId)
);
} else {
this.setState({
dirty: false,
suggestions: [match],
searchText: suggestionText,
});
}
} else {
this.setState({
dirty: true,
searchText: inputValue,
});
this.updateFilter(inputValue);
}
this.setState({
dirty: true,
searchText: inputValue,
});
this.updateFilter(inputValue);
};

handleChange = (event, { newValue, method }) => {
Expand Down Expand Up @@ -533,7 +501,6 @@ AutocompleteInput.propTypes = {
suggestionComponent: PropTypes.func,
translate: PropTypes.func.isRequired,
translateChoice: PropTypes.bool.isRequired,
inputValueMatcher: PropTypes.func,
};

AutocompleteInput.defaultProps = {
Expand All @@ -543,11 +510,6 @@ AutocompleteInput.defaultProps = {
optionValue: 'id',
limitChoicesToValue: false,
translateChoice: true,
inputValueMatcher: (input, suggestion, getOptionText) =>
getOptionText(suggestion)
.toLowerCase()
.trim()
.includes(input.toLowerCase().trim()),
};

export default compose(
Expand Down
23 changes: 1 addition & 22 deletions packages/ra-ui-materialui/src/input/AutocompleteInput.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -363,26 +363,6 @@ describe('<AutocompleteInput />', () => {
expect(wrapper.find('ListItem')).toHaveLength(2);
});

it('should resolve value from input value', () => {
const onChange = jest.fn();
const wrapper = mount(
<AutocompleteInput
{...defaultProps}
input={{ value: '', onChange }}
/>,
{ context, childContextTypes }
);
wrapper.setProps({
choices: [{ id: 'M', name: 'Male' }],
});
wrapper
.find('input')
.simulate('change', { target: { value: 'male' } });
expect(wrapper.state('searchText')).toBe('Male');
expect(onChange).toHaveBeenCalledTimes(1);
expect(onChange).toHaveBeenCalledWith('M');
});

it('should reset filter when input value changed', () => {
const setFilter = jest.fn();
const wrapper = mount(
Expand Down Expand Up @@ -530,7 +510,7 @@ describe('<AutocompleteInput />', () => {
expect(wrapper.state('suggestions')).toHaveLength(2);
});

it('automatically selects a matched choice if there is only one', () => {
it('does not automatically select a matched choice if there is only one', () => {
const onChange = jest.fn();

const wrapper = mount(
Expand All @@ -548,7 +528,6 @@ describe('<AutocompleteInput />', () => {
wrapper.find('input').simulate('focus');
wrapper.find('input').simulate('change', { target: { value: 'abc' } });
expect(wrapper.state('suggestions')).toHaveLength(1);
expect(onChange).toHaveBeenCalledWith(2);
});

it('passes options.suggestionsContainerProps to the suggestions container', () => {
Expand Down

0 comments on commit caf03e9

Please sign in to comment.