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

[RFR] Fix AutocompleteInput by removing auto selection #2833

Merged
merged 2 commits into from
Jan 31, 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
3 changes: 2 additions & 1 deletion cypress/integration/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,10 @@ describe('Create Page', () => {
{
type: 'input',
name: 'authors[0].user_id',
value: 'Annamarie Mayer{enter}',
value: 'Annamarie Mayer',
},
]);
cy.contains('Annamarie Mayer').click();
cy.get(CreatePage.elements.input('authors[0].role')).should(
el => expect(el).to.exist
);
Expand Down
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