Skip to content

Commit 4412b53

Browse files
chore: refactor input-like components to avoid calling setValue and validation if the value hasn't changed
1 parent 2d1bd4f commit 4412b53

10 files changed

Lines changed: 132 additions & 39 deletions

File tree

src/components/entries/FEEL/Feel.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -656,18 +656,18 @@ export default function FeelEntry(props) {
656656

657657
const onInput = useStaticCallback((newValue) => {
658658
const value = getValue(element);
659-
let newValidationError = null;
660659

661-
if (isFunction(validate)) {
662-
newValidationError = validate(newValue) || null;
663-
}
664-
665-
// don't create multiple commandStack entries for the same value
660+
// don't create multiple commandStack entries and do validation for the same value
666661
if (newValue !== value) {
662+
let newValidationError = null;
663+
664+
if (isFunction(validate)) {
665+
newValidationError = validate(newValue) || null;
666+
}
667+
667668
setValue(newValue, newValidationError);
669+
setValidationError(newValidationError);
668670
}
669-
670-
setValidationError(newValidationError);
671671
});
672672

673673
const onError = useCallback(err => {

src/components/entries/NumberField.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -139,14 +139,16 @@ export default function NumberFieldEntry(props) {
139139
}, [ value, validate ]);
140140

141141
const onInput = (newValue) => {
142-
let newValidationError = null;
142+
if (newValue !== value) {
143+
let newValidationError = null;
143144

144-
if (isFunction(validate)) {
145-
newValidationError = validate(newValue) || null;
146-
}
145+
if (isFunction(validate)) {
146+
newValidationError = validate(newValue) || null;
147+
}
147148

148-
setValue(newValue, newValidationError);
149-
setLocalError(newValidationError);
149+
setValue(newValue, newValidationError);
150+
setLocalError(newValidationError);
151+
}
150152
};
151153

152154
const error = globalError || localError;

src/components/entries/Select.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -162,14 +162,16 @@ export default function SelectEntry(props) {
162162

163163

164164
const onChange = (newValue) => {
165-
let newValidationError = null;
165+
if (newValue !== value) {
166+
let newValidationError = null;
166167

167-
if (isFunction(validate)) {
168-
newValidationError = validate(newValue) || null;
169-
}
168+
if (isFunction(validate)) {
169+
newValidationError = validate(newValue) || null;
170+
}
170171

171-
setValue(newValue, newValidationError);
172-
setLocalError(newValidationError);
172+
setValue(newValue, newValidationError);
173+
setLocalError(newValidationError);
174+
}
173175
};
174176

175177
const error = globalError || localError;

src/components/entries/TextArea.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -223,17 +223,17 @@ export default function TextAreaEntry(props) {
223223

224224
const onInput = useStaticCallback((newValue) => {
225225
const value = getValue(element);
226-
let newValidationError = null;
227-
228-
if (isFunction(validate)) {
229-
newValidationError = validate(newValue) || null;
230-
}
231226

232227
if (newValue !== value) {
228+
let newValidationError = null;
229+
230+
if (isFunction(validate)) {
231+
newValidationError = validate(newValue) || null;
232+
}
233+
233234
setValue(newValue, newValidationError);
235+
setLocalError(newValidationError);
234236
}
235-
236-
setLocalError(newValidationError);
237237
});
238238

239239

src/components/entries/TextField.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -189,17 +189,17 @@ export default function TextfieldEntry(props) {
189189

190190
const onInput = useStaticCallback((newValue) => {
191191
const value = getValue(element);
192-
let newValidationError = null;
193-
194-
if (isFunction(validate)) {
195-
newValidationError = validate(newValue) || null;
196-
}
197192

198193
if (newValue !== value) {
194+
let newValidationError = null;
195+
196+
if (isFunction(validate)) {
197+
newValidationError = validate(newValue) || null;
198+
}
199+
199200
setValue(newValue, newValidationError);
201+
setLocalError(newValidationError);
200202
}
201-
202-
setLocalError(newValidationError);
203203
});
204204

205205

test/spec/components/JsonEditor.spec.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,34 @@ describe('<JsonEditor>', function() {
306306
});
307307

308308

309+
it('should not call setValue or validate if value is same', async function() {
310+
311+
// given
312+
const setValueSpy = sinonSpy();
313+
const validateSpy = sinonSpy();
314+
315+
const result = createJsonEditor({
316+
container,
317+
getValue: () => '{"existing": true}',
318+
setValue: setValueSpy,
319+
validate: validateSpy
320+
});
321+
322+
await waitFor(() => {
323+
expect(getEditorView(result.container)).to.exist;
324+
});
325+
326+
// when
327+
validateSpy.resetHistory();
328+
setEditorValue(result.container, '{"existing": true}');
329+
330+
// then
331+
await new Promise(resolve => setTimeout(resolve, 50));
332+
expect(setValueSpy).to.not.have.been.called;
333+
expect(validateSpy).to.not.have.been.called;
334+
});
335+
336+
309337
it('should pass validation error to setValue', async function() {
310338

311339
// given

test/spec/components/NumberField.spec.js

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ describe('<NumberField>', function() {
9191
// given
9292
const updateSpy = sinonSpy();
9393

94-
const result = createNumberField({ container, setValue: updateSpy });
94+
const result = createNumberField({ container, getValue: () => 42, setValue: updateSpy });
9595

9696
const input = domQuery('.bio-properties-panel-input', result.container);
9797

@@ -122,6 +122,31 @@ describe('<NumberField>', function() {
122122
});
123123

124124

125+
it('should not call setValue or validate if the value is same', function() {
126+
127+
// given
128+
const setValueSpy = sinonSpy();
129+
const validateSpy = sinonSpy();
130+
131+
const result = createNumberField({
132+
container,
133+
getValue: () => 20.5,
134+
setValue: setValueSpy,
135+
validate: validateSpy
136+
});
137+
138+
const input = domQuery('.bio-properties-panel-input', result.container);
139+
140+
// when
141+
validateSpy.resetHistory();
142+
changeInput(input, 20.5);
143+
144+
// then
145+
expect(setValueSpy).to.not.have.been.called;
146+
expect(validateSpy).to.not.have.been.called;
147+
});
148+
149+
125150
it('should use unique input element on element change', function() {
126151

127152
// given

test/spec/components/Select.spec.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,34 @@ describe('<Select>', function() {
147147
});
148148

149149

150+
it('should not call setValue or validate if the value is same', function() {
151+
152+
// given
153+
const setValueSpy = sinonSpy();
154+
const validateSpy = sinonSpy();
155+
156+
const getOptions = () => createOptions();
157+
158+
const result = createSelect({
159+
container,
160+
getValue: () => 'A',
161+
setValue: setValueSpy,
162+
validate: validateSpy,
163+
getOptions
164+
});
165+
166+
const select = domQuery('.bio-properties-panel-input', result.container);
167+
168+
// when
169+
validateSpy.resetHistory();
170+
changeInput(select, 'A');
171+
172+
// then
173+
expect(setValueSpy).to.not.have.been.called;
174+
expect(validateSpy).to.not.have.been.called;
175+
});
176+
177+
150178
it('should use unique input element on element change', function() {
151179

152180
// given

test/spec/components/TextArea.spec.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -336,26 +336,30 @@ describe('<TextArea>', function() {
336336
});
337337

338338

339-
it('should not call setValue if the value is same', async function() {
339+
it('should not call setValue or validate if the value is same', async function() {
340340

341341
// given
342342
const setValueSpy = sinonSpy();
343+
const validateSpy = sinonSpy();
343344

344345
const result = createTextArea({
345346
container,
346347
getValue: () => 'a value',
347-
setValue: setValueSpy
348+
setValue: setValueSpy,
349+
validate: validateSpy
348350
});
349351

350352
const input = domQuery('.bio-properties-panel-input', result.container);
351353

352354
// when
355+
validateSpy.resetHistory();
353356
input.focus();
354357
changeInput(input, 'a value');
355358
input.blur();
356359

357360
// then
358361
expect(setValueSpy).to.not.have.been.called;
362+
expect(validateSpy).to.not.have.been.called;
359363

360364
});
361365

test/spec/components/TextField.spec.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -294,26 +294,30 @@ describe('<TextField>', function() {
294294
});
295295

296296

297-
it('should not call setValue if the value is same', async function() {
297+
it('should not call setValue or validate if the value is same', async function() {
298298

299299
// given
300300
const setValueSpy = sinonSpy();
301+
const validateSpy = sinonSpy();
301302

302303
const result = createTextField({
303304
container,
304305
getValue: () => 'a value',
305-
setValue: setValueSpy
306+
setValue: setValueSpy,
307+
validate: validateSpy
306308
});
307309

308310
const input = domQuery('.bio-properties-panel-input', result.container);
309311

310312
// when
313+
validateSpy.resetHistory();
311314
input.focus();
312315
changeInput(input, 'a value');
313316
input.blur();
314317

315318
// then
316319
expect(setValueSpy).to.not.have.been.called;
320+
expect(validateSpy).to.not.have.been.called;
317321

318322
});
319323

0 commit comments

Comments
 (0)