Skip to content

Commit 89dc988

Browse files
committed
Fix: Properly handle same inputs\outputs, handle cross-control changes
1 parent e6fb49d commit 89dc988

File tree

4 files changed

+58
-2
lines changed

4 files changed

+58
-2
lines changed

e2e/tests/playground/select-control.spec.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,4 +118,27 @@ test.describe.serial('FeSelectDirective', () => {
118118
await kit.$('#select-11').selectOption({label: 'VAL2'});
119119
await expect(kit.$('#select-11-touched')).toHaveText('TOUCHED: true');
120120
});
121+
122+
test('12 - cross-change, should properly re-set value received from input', async () => {
123+
await kit.$('#select-12_1').selectOption({label: 'VAL1'});
124+
await expect(kit.$('#select-12_1-input-value')).toHaveText('INPUT_VALUE: "1"');
125+
expect(await kit.isSelected('#select-12_1-opt-1')).toBeTruthy();
126+
expect(await kit.isSelected('#select-12_2-opt-1')).toBeTruthy();
127+
await kit.$('#select-12_2').selectOption({label: 'VAL2'});
128+
await expect(kit.$('#select-12_1-model-value')).toHaveText('MODEL_VALUE: "2"');
129+
await expect(kit.$('#select-12_1-input-value')).toHaveText('INPUT_VALUE: "2"');
130+
expect(await kit.isSelected('#select-12_1-opt-2')).toBeTruthy();
131+
await kit.$('#select-12_2').selectOption({label: 'VAL1'});
132+
await expect(kit.$('#select-12_1-model-value')).toHaveText('MODEL_VALUE: "1"');
133+
await expect(kit.$('#select-12_1-input-value')).toHaveText('INPUT_VALUE: "1"');
134+
expect(await kit.isSelected('#select-12_1-opt-1')).toBeTruthy();
135+
await kit.$('#select-12_2').selectOption({label: 'VAL2'});
136+
await expect(kit.$('#select-12_1-model-value')).toHaveText('MODEL_VALUE: "2"');
137+
await expect(kit.$('#select-12_1-input-value')).toHaveText('INPUT_VALUE: "2"');
138+
expect(await kit.isSelected('#select-12_1-opt-2')).toBeTruthy();
139+
await kit.$('#select-12_1').selectOption({label: 'VAL3'});
140+
expect(await kit.isSelected('#select-12_2-opt-3')).toBeTruthy();
141+
await kit.$('#select-12_1').selectOption({label: 'VAL2'});
142+
expect(await kit.isSelected('#select-12_2-opt-2')).toBeTruthy();
143+
});
121144
});

projects/ngfe/src/lib/core/fe-control.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,10 @@ export class FeControl<MODEL = any, INPUT = any> implements OnChanges, OnDestroy
9292
@Output() destroy = new EventEmitter<undefined>();
9393

9494
private modelValueOutput?: MODEL;
95+
/**
96+
* When we emit value from @output with "change" postfix - it will immediately return from @input.
97+
* This flag is `true` when we emit value, and should be reset after next input.
98+
*/
9599
private modelValueOutputFlag = false;
96100

97101
private readonly _vc$ = new BehaviorSubject<Vc<MODEL, INPUT>>({source: 'initial'});
@@ -190,8 +194,14 @@ export class FeControl<MODEL = any, INPUT = any> implements OnChanges, OnDestroy
190194
}
191195

192196
ngOnChanges(changes: SimpleChanges) {
193-
if ('feControl' in changes && (!this.modelValueOutputFlag || this.feControl !== this.modelValueOutput)) {
194-
this.update(this.feControl, 'model');
197+
if ('feControl' in changes) {
198+
if (!this.modelValueOutputFlag || this.feControl !== this.modelValueOutput) {
199+
this.update(this.feControl, 'model');
200+
}
201+
// Reset flag after input
202+
if (this.modelValueOutputFlag) {
203+
this.modelValueOutputFlag = false;
204+
}
195205
}
196206
if ('extraValidators' in changes) {
197207
const prev: FeValidator<MODEL>[] | undefined = changes.extraValidators.previousValue;

projects/playground/src/app/select-control-page/select-control-page.component.html

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,3 +153,25 @@ <h4>11 - Touch on change</h4>
153153
TOUCHED: {{ control11.touched | json }}
154154
</div>
155155
</div>
156+
157+
<div class="case">
158+
<h4>12 - Cross-change</h4>
159+
<div>
160+
<select [(feControl)]="value12" #control12_1="feControl" id="select-12_1" touchOnChange>
161+
<option value="1" id="select-12_1-opt-1">VAL1</option>
162+
<option value="2" id="select-12_1-opt-2">VAL2</option>
163+
<option value="3">VAL3</option>
164+
</select>
165+
<select [(feControl)]="value12" #control12_2="feControl" id="select-12_2" touchOnChange>
166+
<option value="1" id="select-12_2-opt-1">VAL1</option>
167+
<option value="2" id="select-12_2-opt-2">VAL2</option>
168+
<option value="3" id="select-12_2-opt-3">VAL3</option>
169+
</select>
170+
</div>
171+
<div id="select-12_1-input-value">
172+
INPUT_VALUE: {{ control12_1.inputValue | json }}
173+
</div>
174+
<div id="select-12_1-model-value">
175+
MODEL_VALUE: {{ control12_1.modelValue | json }}
176+
</div>
177+
</div>

projects/playground/src/app/select-control-page/select-control-page.component.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,5 @@ export class SelectControlPageComponent {
1919
value10 = '1';
2020
disabled10 = false;
2121
value11 = '';
22+
value12 = '';
2223
}

0 commit comments

Comments
 (0)