QueryField: Remove carriage return character from pasted text (#34076)

* Remove carriage returns

* Clean up
This commit is contained in:
Ivana Huckova
2021-05-17 13:40:08 +02:00
committed by GitHub
parent bc4ec61f67
commit 25d42dbedb
2 changed files with 19 additions and 2 deletions

View File

@@ -30,6 +30,17 @@ describe('<QueryField />', () => {
expect(onRun.mock.calls.length).toBe(1);
});
it('should run onChange with clean text', () => {
const onChange = jest.fn();
const wrapper = shallow(
<QueryField query={`my\r clean query`} onTypeahead={jest.fn()} onChange={onChange} portalOrigin="mock-origin" />
);
const field = wrapper.instance() as QueryField;
field.runOnChange();
expect(onChange.mock.calls.length).toBe(1);
expect(onChange.mock.calls[0][0]).toBe('my clean query');
});
it('should run custom on blur, but not necessarily execute query', () => {
const onBlur = jest.fn();
const onRun = jest.fn();

View File

@@ -148,9 +148,9 @@ export class QueryField extends React.PureComponent<QueryFieldProps, QueryFieldS
runOnChange = () => {
const { onChange } = this.props;
const value = Plain.serialize(this.state.value);
if (onChange) {
onChange(Plain.serialize(this.state.value));
onChange(this.cleanText(value));
}
};
@@ -190,6 +190,12 @@ export class QueryField extends React.PureComponent<QueryFieldProps, QueryFieldS
return next();
};
cleanText(text: string) {
// RegExp with invisible characters we want to remove - currently only carriage return (newlines are visible)
const newText = text.trim().replace(/[\r]/g, '');
return newText;
}
render() {
const { disabled } = this.props;
const wrapperClassName = classnames('slate-query-field__wrapper', {