Skip to content

Commit bee90f7

Browse files
authored
UIEXT-3044: Set value on focusout in NumberControl when value is not a number (#95)
UIEXT-3044 (Prevent number inputs to set the value to null in the dialog)
1 parent 937eed6 commit bee90f7

File tree

3 files changed

+34
-7
lines changed

3 files changed

+34
-7
lines changed

.changeset/ready-dolls-cross.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@knime/jsonforms": patch
3+
---
4+
5+
Set a value on focusout when the number provided by the number input is not a number in the NumberControl.

packages/jsonforms/src/uiComponents/NumberControlBase.vue

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,22 +53,32 @@ const minParams = computed(() => validationParams.value.min);
5353
const maxParams = computed(() => validationParams.value.max);
5454
5555
const onFocusOut = () => {
56-
const num = props.control.data;
57-
if (minParams.value && !respectsMin(minParams.value)(num)) {
56+
const currentDataValue = props.control.data;
57+
const isNotANumber =
58+
typeof currentDataValue !== "number" || isNaN(currentDataValue);
59+
let updatedValue = isNotANumber ? 0 : null;
60+
const comparisonValue = isNotANumber ? 0 : currentDataValue;
61+
if (minParams.value && !respectsMin(minParams.value)(comparisonValue)) {
5862
const { min, isExclusive } = minParams.value;
5963
if (isExclusive) {
60-
props.changeValue(min + stepSize);
64+
updatedValue = min + stepSize;
6165
} else {
62-
props.changeValue(min);
66+
updatedValue = min;
6367
}
64-
} else if (maxParams.value && !respectsMax(maxParams.value)(num)) {
68+
} else if (
69+
maxParams.value &&
70+
!respectsMax(maxParams.value)(comparisonValue)
71+
) {
6572
const { max, isExclusive } = maxParams.value;
6673
if (isExclusive) {
67-
props.changeValue(max - stepSize);
74+
updatedValue = max - stepSize;
6875
} else {
69-
props.changeValue(max);
76+
updatedValue = max;
7077
}
7178
}
79+
if (updatedValue !== null) {
80+
props.changeValue(updatedValue);
81+
}
7282
};
7383
</script>
7484

packages/jsonforms/src/uiComponents/__tests__/NumberControl.test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,18 @@ describe("NumberControl", () => {
127127
expect(changeValue).toHaveBeenCalledWith(result);
128128
});
129129

130+
it("sets 0 if value is not a number on focusout", async () => {
131+
props.control.data = NaN;
132+
const { wrapper, changeValue } = mountJsonFormsControlLabelContent(
133+
NumberControl,
134+
{
135+
props,
136+
},
137+
);
138+
await wrapper.findComponent(NumberControl).trigger("focusout");
139+
expect(changeValue).toHaveBeenCalledWith(0);
140+
});
141+
130142
it("sets the minimum via state provider", async () => {
131143
props.control.uischema.providedOptions = ["validation.min"];
132144
let provideMin: (params: {

0 commit comments

Comments
 (0)