Skip to content

Commit 8631d18

Browse files
authored
Merge pull request JedWatson#1858 from not-an-aardvark/no-mutate-input-value
Avoid mutating user input when ignoring case/accents (fixes JedWatson#1352)
2 parents 87a82b3 + e1bb7b9 commit 8631d18

2 files changed

Lines changed: 17 additions & 6 deletions

File tree

src/Async.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -131,26 +131,28 @@ export default class Async extends Component {
131131
isLoading: true
132132
});
133133
}
134-
135-
return inputValue;
136134
}
137135

138136
_onInputChange (inputValue) {
139137
const { ignoreAccents, ignoreCase, onInputChange } = this.props;
138+
let transformedInputValue = inputValue;
140139

141140
if (ignoreAccents) {
142-
inputValue = stripDiacritics(inputValue);
141+
transformedInputValue = stripDiacritics(transformedInputValue);
143142
}
144143

145144
if (ignoreCase) {
146-
inputValue = inputValue.toLowerCase();
145+
transformedInputValue = transformedInputValue.toLowerCase();
147146
}
148147

149148
if (onInputChange) {
150-
onInputChange(inputValue);
149+
onInputChange(transformedInputValue);
151150
}
152151

153-
return this.loadOptions(inputValue);
152+
this.loadOptions(transformedInputValue);
153+
154+
// Return the original input value to avoid modifying the user's view of the input while typing.
155+
return inputValue;
154156
}
155157

156158
inputValue() {

test/Async-test.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,15 @@ describe('Async', () => {
319319
typeSearchText('WÄRE');
320320
expect(loadOptions, 'was called with', 'WÄRE');
321321
});
322+
323+
it('does not mutate the user input', () => {
324+
createControl({
325+
ignoreAccents: false,
326+
ignoreCase: true
327+
});
328+
typeSearchText('A');
329+
expect(asyncNode.textContent, 'to begin with', 'A');
330+
});
322331
});
323332

324333
describe('with ignore case and ignore accents', () => {

0 commit comments

Comments
 (0)