Skip to content

Commit 5ec8179

Browse files
Form Smart Paste: call onFieldDataChanged on item value update (#30953)
1 parent 8e0f691 commit 5ec8179

File tree

2 files changed

+65
-3
lines changed

2 files changed

+65
-3
lines changed

packages/devextreme/js/__internal/ui/form/form.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1855,15 +1855,20 @@ class Form extends Widget<FormProperties> {
18551855
this._abort?.();
18561856
this._abort = undefined;
18571857
this._currentAICommand = undefined;
1858-
this._hideLoadPanel();
18591858
}
18601859

18611860
private _processAIIntegrationUpdate(): void {
18621861
if (this._currentAICommand) {
18631862
const { command, params, callbacks } = this._currentAICommand;
1863+
const { aiIntegration } = this.option();
18641864

18651865
this._processCommandCompletion();
1866-
this._executeAICommand(command, params, callbacks);
1866+
1867+
if (!aiIntegration) {
1868+
this._hideLoadPanel();
1869+
} else {
1870+
this._executeAICommand(command, params, callbacks);
1871+
}
18671872
}
18681873
}
18691874

@@ -1896,6 +1901,7 @@ class Form extends Widget<FormProperties> {
18961901
invokeConditionally(
18971902
smartPastingArgs.cancel,
18981903
(): void => {
1904+
this._hideLoadPanel();
18991905
this.beginUpdate();
19001906
fieldsData.forEach(({ name, value }: SmartPasteCommandResult[number]) => {
19011907
this._updateFieldValue(name, value);

packages/devextreme/testing/tests/DevExpress.ui.widgets.form/form.aiIntegration.tests.js

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -464,7 +464,7 @@ QUnit.module('SmartPaste', () => {
464464
});
465465
});
466466

467-
QUnit.test('LoadPanel is hidden when smartPaste operation is aborted', function(assert) {
467+
QUnit.test('LoadPanel not hidden on aiIntegration update during command execution', function(assert) {
468468
const done = assert.async();
469469
let abortCallback;
470470

@@ -485,6 +485,38 @@ QUnit.module('SmartPaste', () => {
485485

486486
form.option('aiIntegration', { smartPaste: sinon.stub().callsFake(() => () => {}) });
487487

488+
setTimeout(() => {
489+
assert.strictEqual(abortCallback.calledOnce, true, 'Previous operation was aborted');
490+
assert.strictEqual(form._loadPanel.option('visible'), true, 'LoadPanel is hidden after abort');
491+
assert.strictEqual(form.option('disabled'), true, 'Form is not disabled after abort');
492+
493+
done();
494+
}, 10);
495+
}, 10);
496+
});
497+
});
498+
499+
QUnit.test('LoadPanel is hidden on aiIntegration remove during command execution', function(assert) {
500+
const done = assert.async();
501+
let abortCallback;
502+
503+
const smartPaste = sinon.stub().callsFake((params, callbacks) => {
504+
abortCallback = sinon.spy();
505+
return abortCallback;
506+
});
507+
const aiIntegration = { smartPaste: smartPaste };
508+
const form = setupFormWithAi({ aiIntegration });
509+
510+
this.clipboardStub = this.createClipboardStub('test text');
511+
512+
form.smartPaste().then(() => {
513+
setTimeout(() => {
514+
assert.strictEqual(form.$element().find(`.${FORM_LOAD_PANEL_CLASS}`).length, 1, 'LoadPanel is shown during operation');
515+
assert.strictEqual(form.option('disabled'), true, 'Form is disabled during operation');
516+
assert.ok(abortCallback, 'Abort callback was created');
517+
518+
form.option('aiIntegration', undefined);
519+
488520
setTimeout(() => {
489521
assert.strictEqual(abortCallback.calledOnce, true, 'Previous operation was aborted');
490522
assert.strictEqual(form.$element().find(`.${FORM_LOAD_PANEL_CLASS}`).length, 1, 'LoadPanel is still present but hidden');
@@ -765,6 +797,30 @@ QUnit.module('SmartPaste', () => {
765797
assert.strictEqual(onSmartPasted.calledOnce, true, 'onSmartPasted event has been invoked after its change at runtime');
766798
});
767799
});
800+
801+
QUnit.test('should invoke onDataFieldChanged for each updated field after smartPaste', function(assert) {
802+
const aiResult = [
803+
{ name: 'field1', value: 'value1' },
804+
{ name: 'field2', value: 'value2' },
805+
];
806+
const smartPaste = (_, callbacks) => {
807+
callbacks.onComplete(aiResult);
808+
};
809+
const onFieldDataChanged = sinon.spy();
810+
811+
const form = setupFormWithAi({
812+
aiIntegration: { smartPaste },
813+
onFieldDataChanged
814+
});
815+
816+
form.smartPaste('text');
817+
818+
const onFieldDataChangedCalls = onFieldDataChanged.getCalls();
819+
820+
assert.strictEqual(onFieldDataChangedCalls.length, 2, 'onFieldDataChanged called twice');
821+
assert.propContains(onFieldDataChangedCalls[0].args[0], { dataField: 'field1', value: 'value1' }, 'onFieldDataChanged called for 1st field with correct args');
822+
assert.propContains(onFieldDataChangedCalls[1].args[0], { dataField: 'field2', value: 'value2' }, 'onFieldDataChanged called for 2nd field with correct args');
823+
});
768824
});
769825

770826
QUnit.module('aiOptions', {

0 commit comments

Comments
 (0)