Skip to content
Snippets Groups Projects
Commit 0f566065 authored by Tom Robinson's avatar Tom Robinson
Browse files

Make sure pasting doesn't update input value

parent 6676826b
No related branches found
No related tags found
No related merge requests found
......@@ -340,11 +340,14 @@ export default class TokenField extends Component {
onInputPaste = (e: SyntheticClipboardEvent) => {
if (this.props.parseFreeformValue) {
e.preventDefault();
const string = e.clipboardData.getData("Text");
const values = string
.split(/\n|,/g)
.map(this.props.parseFreeformValue)
.filter(s => s);
const values = this.props.multi
? string
.split(/\n|,/g)
.map(this.props.parseFreeformValue)
.filter(s => s)
: [string];
if (values.length > 0) {
this.addValue(values);
}
......
......@@ -363,6 +363,27 @@ describe("TokenField", () => {
});
expect(preventDefault).toHaveBeenCalled();
});
it('should paste "1,2,3" as multiple values', () => {
const preventDefault = jest.fn();
component = mount(
<TokenFieldWithStateAndDefaults
// return null for empty string since it's not a valid
parseFreeformValue={value => value || null}
updateOnInputChange
multi
/>,
);
input = component.find("input");
input.simulate("paste", {
clipboardData: {
getData: () => "1,2,3",
},
preventDefault,
});
expect(values()).toEqual(["1", "2", "3"]);
// prevent pasting into <input>
expect(preventDefault).toHaveBeenCalled();
});
});
describe("with multi=false", () => {
it("should not prevent blurring on tab", () => {
......@@ -383,5 +404,25 @@ describe("TokenField", () => {
});
expect(preventDefault).not.toHaveBeenCalled();
});
it('should paste "1,2,3" as one value', () => {
const preventDefault = jest.fn();
component = mount(
<TokenFieldWithStateAndDefaults
// return null for empty string since it's not a valid
parseFreeformValue={value => value || null}
updateOnInputChange
/>,
);
input = component.find("input");
input.simulate("paste", {
clipboardData: {
getData: () => "1,2,3",
},
preventDefault,
});
expect(values()).toEqual(["1,2,3"]);
// prevent pasting into <input>
expect(preventDefault).toHaveBeenCalled();
});
});
});
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment