Skip to content
This repository was archived by the owner on Mar 2, 2026. It is now read-only.

Commit c7b49c8

Browse files
authored
Fix controlling options values (#404)
1 parent b62413b commit c7b49c8

2 files changed

Lines changed: 23 additions & 22 deletions

File tree

src/core/__tests__/props-updating.test.tsx

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ interface IControlledComponentProps {
1313
defaultControlledOption?: string;
1414
controlledOption?: string;
1515
onControlledOptionChanged?: () => void;
16-
everyOption?: number;
16+
everyOption?: number | boolean | null;
1717
anotherOption?: string;
1818
complexOption?: object;
1919
}
@@ -211,31 +211,33 @@ describe("option control", () => {
211211
});
212212
});
213213

214-
it("rolls back controlled simple option", () => {
215-
shallow(
216-
<ControlledComponent everyOption={123} />
217-
);
214+
[123, false, 0].map((value) => {
215+
it("rolls back controlled simple option", () => {
216+
shallow(
217+
<ControlledComponent everyOption={value} />
218+
);
218219

219-
fireOptionChange("everyOption", 234);
220-
jest.runAllTimers();
221-
expect(Widget.option.mock.calls.length).toBe(1);
222-
expect(Widget.option.mock.calls[0]).toEqual(["everyOption", 123]);
220+
fireOptionChange("everyOption", 234);
221+
jest.runAllTimers();
222+
expect(Widget.option.mock.calls.length).toBe(1);
223+
expect(Widget.option.mock.calls[0]).toEqual(["everyOption", value]);
224+
});
223225
});
224226

225227
it("rolls back controlled complex option", () => {
226228
shallow(
227-
<ControlledComponent complexOption={{a: 123, b: 234}} />
229+
<ControlledComponent complexOption={{ a: 123, b: 234 }} />
228230
);
229231

230232
fireOptionChange("complexOption", {});
231233
jest.runAllTimers();
232234
expect(Widget.option.mock.calls.length).toBe(1);
233-
expect(Widget.option.mock.calls[0]).toEqual(["complexOption", {a: 123, b: 234}]);
235+
expect(Widget.option.mock.calls[0]).toEqual(["complexOption", { a: 123, b: 234 }]);
234236
});
235237

236238
it("rolls back complex option controlled field", () => {
237239
shallow(
238-
<ControlledComponent complexOption={{a: 123}} />
240+
<ControlledComponent complexOption={{ a: 123 }} />
239241
);
240242

241243
fireOptionChange("complexOption.a", 234);
@@ -271,14 +273,14 @@ describe("option control", () => {
271273

272274
it("applies complex option change", () => {
273275
const component = shallow(
274-
<ControlledComponent complexOption={{a: 123}} />
276+
<ControlledComponent complexOption={{ a: 123 }} />
275277
);
276278

277279
fireOptionChange("complexOption.b", 234);
278-
component.setProps({ complexOption: {a: 123, b: 234} });
280+
component.setProps({ complexOption: { a: 123, b: 234 } });
279281
jest.runAllTimers();
280282
expect(Widget.option.mock.calls.length).toBe(1);
281-
expect(Widget.option.mock.calls[0]).toEqual(["complexOption", {a: 123, b: 234} ]);
283+
expect(Widget.option.mock.calls[0]).toEqual(["complexOption", { a: 123, b: 234 }]);
282284
});
283285

284286
it("does not roll back not controlled simple option", () => {
@@ -293,7 +295,7 @@ describe("option control", () => {
293295

294296
it("does not roll back controlled complex option not controlled field", () => {
295297
shallow(
296-
<ControlledComponent complexOption={{a: 123}} />
298+
<ControlledComponent complexOption={{ a: 123 }} />
297299
);
298300

299301
fireOptionChange("complexOption.b", 234);
@@ -372,7 +374,7 @@ describe("cfg-component option control", () => {
372374
fireOptionChange("items", []);
373375
jest.runAllTimers();
374376
expect(Widget.option.mock.calls.length).toBe(1);
375-
expect(Widget.option.mock.calls[0]).toEqual(["items", [{a: 1}, {a: 2}]]);
377+
expect(Widget.option.mock.calls[0]).toEqual(["items", [{ a: 1 }, { a: 2 }]]);
376378
});
377379

378380
it("rolls nested collection item value back", () => {
@@ -383,7 +385,7 @@ describe("cfg-component option control", () => {
383385
</TestComponentWithExpectation>
384386
);
385387

386-
fireOptionChange("items[0]", {a: 3});
388+
fireOptionChange("items[0]", { a: 3 });
387389
jest.runAllTimers();
388390
expect(Widget.option.mock.calls.length).toBe(1);
389391
expect(Widget.option.mock.calls[0]).toEqual(["items[0].a", 1]);

src/core/configuration/tree.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -123,16 +123,15 @@ function findValue(node: IConfigNode, path: string[]): undefined | IValueDescrip
123123
};
124124
}
125125

126-
const value = node.options[optionInfo.name];
127-
if (value) {
128-
return findValueInObject(value, path);
126+
if (optionInfo.name in node.options) {
127+
return findValueInObject(node.options[optionInfo.name], path);
129128
}
130129

131130
return;
132131
}
133132

134133
function findValueInObject(obj: any, path: string[]): undefined | IValueDescriptor {
135-
if (!obj) {
134+
if (obj === null || obj === undefined) {
136135
return;
137136
}
138137

0 commit comments

Comments
 (0)