fix(ui): don't crash when removing last matcher value

Removing last value sets the value that's expected to be a list to null, which breaks some logic, ensure we always have a list there

Fixes #827
This commit is contained in:
Łukasz Mierzwa
2019-07-14 22:15:41 +01:00
parent 21b62c2550
commit b78c47cb93
2 changed files with 21 additions and 3 deletions

View File

@@ -57,12 +57,15 @@ const LabelValueInput = observer(
onChange = action((newValue, actionMeta) => {
const { matcher } = this.props;
matcher.values = newValue;
// we might get null if there's nothing selected
const value = newValue || [];
matcher.values = value;
// force regex if we have multiple values
if (newValue.length > 1 && matcher.isRegex === false) {
if (value.length > 1 && matcher.isRegex === false) {
matcher.isRegex = true;
} else if (newValue.length === 1 && matcher.isRegex === true) {
} else if (value.length === 1 && matcher.isRegex === true) {
matcher.isRegex = false;
}
});

View File

@@ -125,4 +125,19 @@ describe("<LabelValueInput />", () => {
options.at(1).simulate("click");
expect(matcher.isRegex).toBe(true);
});
it("removing last value sets matcher.values to []", () => {
matcher.values = [MatcherValueToObject("foo"), MatcherValueToObject("bar")];
const tree = MountedLabelValueInput(true);
tree
.find(".react-select__multi-value__remove")
.at(0)
.simulate("click");
expect(matcher.values).toHaveLength(1);
tree.find(".react-select__multi-value__remove").simulate("click");
expect(matcher.values).toHaveLength(0);
expect(matcher.values).toEqual([]);
});
});