@3kles/kles-material-dynamicforms 19.5.0 → 19.5.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,5 +1,5 @@
1
1
  import * as i0 from '@angular/core';
2
- import { signal, InjectionToken, inject, Component, Injector, Input, Directive, Injectable, computed, EventEmitter, Output, NgModule, ViewContainerRef, HostBinding, Pipe, ChangeDetectorRef, ViewChildren, ViewChild, forwardRef, CUSTOM_ELEMENTS_SCHEMA, NO_ERRORS_SCHEMA } from '@angular/core';
2
+ import { signal, InjectionToken, inject, Component, Injector, Input, Directive, Injectable, EventEmitter, Output, NgModule, computed, ViewContainerRef, HostBinding, Pipe, ChangeDetectorRef, ViewChildren, ViewChild, forwardRef, CUSTOM_ELEMENTS_SCHEMA, NO_ERRORS_SCHEMA } from '@angular/core';
3
3
  import * as i1 from '@angular/common';
4
4
  import { CommonModule } from '@angular/common';
5
5
  import * as i2 from '@angular/forms';
@@ -134,6 +134,8 @@ class KlesFormControl extends KlesAbstractFormControl {
134
134
  class AbstractUiState {
135
135
  constructor() {
136
136
  this._value = signal(undefined);
137
+ this._parent = null;
138
+ this._parentKey = null;
137
139
  }
138
140
  get(path) {
139
141
  let currPath = path;
@@ -151,6 +153,18 @@ class AbstractUiState {
151
153
  get value() {
152
154
  return this._value.asReadonly();
153
155
  }
156
+ setParent(parent, key) {
157
+ this._parent = parent;
158
+ this._parentKey = key;
159
+ }
160
+ notifyParent() {
161
+ if (this._parent && this._parentKey != null) {
162
+ this._parent.childValueChanged(this._parentKey, this._value());
163
+ }
164
+ }
165
+ childValueChanged(key, value) {
166
+ // par défaut, rien
167
+ }
154
168
  }
155
169
 
156
170
  class ControlUiState extends AbstractUiState {
@@ -160,8 +174,12 @@ class ControlUiState extends AbstractUiState {
160
174
  this._value.set(value);
161
175
  }
162
176
  }
177
+ get value() {
178
+ return this._value.asReadonly();
179
+ }
163
180
  setValue(value) {
164
181
  this._value.set(value);
182
+ this.notifyParent();
165
183
  }
166
184
  patchValue(value) {
167
185
  if (value == null) {
@@ -173,6 +191,7 @@ class ControlUiState extends AbstractUiState {
173
191
  return;
174
192
  }
175
193
  this._value.set({ ...curr, ...value });
194
+ this.notifyParent();
176
195
  }
177
196
  }
178
197
 
@@ -328,11 +347,23 @@ class KlesDynamicFieldDirective {
328
347
  this.buildComponent();
329
348
  }
330
349
  ngOnChanges(changes) {
331
- if (changes.group && !changes.group.isFirstChange()) {
332
- this.group = changes.group.currentValue;
350
+ const groupChanged = !!changes['group'] && !changes['group'].isFirstChange();
351
+ const fieldChanged = !!changes['field'] && !changes['field'].isFirstChange();
352
+ const uiChanged = !!changes['ui'] && !changes['ui'].isFirstChange();
353
+ const siblingFieldsChanged = !!changes['siblingFields'] && !changes['siblingFields'].isFirstChange();
354
+ if (groupChanged) {
355
+ this.group = changes['group'].currentValue;
333
356
  }
334
- if (changes.field && !changes.field?.isFirstChange()) {
335
- this.field = changes.field.currentValue;
357
+ if (fieldChanged) {
358
+ this.field = changes['field'].currentValue;
359
+ }
360
+ if (uiChanged) {
361
+ this.ui = changes['ui'].currentValue;
362
+ }
363
+ if (siblingFieldsChanged) {
364
+ this.siblingFields = changes['siblingFields'].currentValue;
365
+ }
366
+ if (groupChanged || fieldChanged || uiChanged || siblingFieldsChanged) {
336
367
  this.buildComponent();
337
368
  }
338
369
  }
@@ -531,31 +562,48 @@ class GroupUiState extends AbstractUiState {
531
562
  constructor(states) {
532
563
  super();
533
564
  this.states = {};
534
- this._computedValue = computed(() => {
535
- return this._snapshot();
536
- });
537
565
  this.states = (states ?? {});
538
- this._value.set(this._snapshot());
566
+ for (const key of Object.keys(this.states)) {
567
+ const state = this.states[key];
568
+ state?.setParent(this, key);
569
+ }
570
+ this._value.set(this.buildInitialValue());
539
571
  }
540
572
  setValue(value) {
541
573
  if (value == null)
542
574
  return;
543
- Object.keys(value).forEach((name) => {
544
- this.states[name]?.setValue(value[name]);
545
- });
575
+ for (const name of Object.keys(value)) {
576
+ const state = this.states[name];
577
+ if (state) {
578
+ state.setValue(value[name]);
579
+ }
580
+ }
546
581
  this._value.set(value);
582
+ this.notifyParent();
547
583
  }
548
584
  patchValue(value) {
549
585
  if (value == null)
550
586
  return;
551
- Object.keys(value).forEach((name) => {
587
+ for (const name of Object.keys(value)) {
552
588
  const state = this.states[name];
553
589
  if (state) {
554
590
  state.patchValue(value[name]);
555
591
  }
592
+ }
593
+ const curr = this._value() ?? {};
594
+ this._value.set({
595
+ ...curr,
596
+ ...value,
556
597
  });
598
+ this.notifyParent();
599
+ }
600
+ childValueChanged(key, value) {
557
601
  const curr = this._value() ?? {};
558
- this._value.set({ ...curr, ...value });
602
+ this._value.set({
603
+ ...curr,
604
+ [key]: value,
605
+ });
606
+ this.notifyParent();
559
607
  }
560
608
  _find(name) {
561
609
  const key = String(name);
@@ -564,13 +612,15 @@ class GroupUiState extends AbstractUiState {
564
612
  }
565
613
  addUiState(name, uiState) {
566
614
  this.states[name] = uiState;
615
+ uiState.setParent(this, name);
567
616
  const curr = this._value() ?? {};
568
- this._value.set({ ...curr, [name]: uiState.value() });
569
- }
570
- get value() {
571
- return this._computedValue;
617
+ this._value.set({
618
+ ...curr,
619
+ [name]: uiState.value(),
620
+ });
621
+ this.notifyParent();
572
622
  }
573
- _snapshot() {
623
+ buildInitialValue() {
574
624
  const out = {};
575
625
  for (const key of Object.keys(this.states)) {
576
626
  out[key] = this.states[key]?.value();
@@ -4327,14 +4377,11 @@ class ArrayUiState extends AbstractUiState {
4327
4377
  constructor(states) {
4328
4378
  super();
4329
4379
  this.states = [];
4330
- this._computedValue = computed(() => {
4331
- return this.states.map((s) => s.value());
4332
- });
4333
4380
  this.states = states ?? [];
4334
- this._value.set(this.states.map((s) => s.value()));
4335
- }
4336
- get value() {
4337
- return this._computedValue;
4381
+ this.states.forEach((state, index) => {
4382
+ state.setParent(this, index);
4383
+ });
4384
+ this._value.set(this.states.map((state) => state.value()));
4338
4385
  }
4339
4386
  setValue(value) {
4340
4387
  if (!Array.isArray(value)) {
@@ -4344,6 +4391,7 @@ class ArrayUiState extends AbstractUiState {
4344
4391
  this.at(index)?.setValue(newValue);
4345
4392
  });
4346
4393
  this._value.set(value);
4394
+ this.notifyParent();
4347
4395
  }
4348
4396
  patchValue(value) {
4349
4397
  if (value == null) {
@@ -4357,8 +4405,24 @@ class ArrayUiState extends AbstractUiState {
4357
4405
  });
4358
4406
  const curr = this._value() ?? [];
4359
4407
  const next = [...curr];
4360
- value.forEach((v, i) => (next[i] = v));
4408
+ value.forEach((newValue, index) => {
4409
+ next[index] = {
4410
+ ...(next[index] ?? {}),
4411
+ ...newValue,
4412
+ };
4413
+ });
4414
+ this._value.set(next);
4415
+ this.notifyParent();
4416
+ }
4417
+ childValueChanged(key, value) {
4418
+ const index = typeof key === 'number' ? key : Number(key);
4419
+ if (!Number.isInteger(index) || index < 0)
4420
+ return;
4421
+ const curr = this._value() ?? [];
4422
+ const next = [...curr];
4423
+ next[index] = value;
4361
4424
  this._value.set(next);
4425
+ this.notifyParent();
4362
4426
  }
4363
4427
  at(index) {
4364
4428
  return this.states?.[index] ?? null;
@@ -4368,15 +4432,51 @@ class ArrayUiState extends AbstractUiState {
4368
4432
  return Number.isFinite(idx) ? this.at(idx) : null;
4369
4433
  }
4370
4434
  push(control) {
4371
- if (Array.isArray(control)) {
4372
- control.forEach((ctrl) => {
4373
- this.states.push(ctrl);
4374
- });
4435
+ const items = Array.isArray(control) ? control : [control];
4436
+ if (items.length === 0) {
4437
+ return;
4375
4438
  }
4376
- else {
4377
- this.states.push(control);
4439
+ for (const item of items) {
4440
+ const index = this.states.length;
4441
+ this.states.push(item);
4442
+ item.setParent(this, index);
4378
4443
  }
4379
4444
  this._value.set(this.states.map((s) => s.value()));
4445
+ this.notifyParent();
4446
+ }
4447
+ insert(index, state) {
4448
+ const items = Array.isArray(state) ? state : [state];
4449
+ if (items.length === 0) {
4450
+ return;
4451
+ }
4452
+ const safeIndex = Math.max(0, Math.min(index, this.states.length));
4453
+ this.states.splice(safeIndex, 0, ...items);
4454
+ for (let i = safeIndex; i < this.states.length; i++) {
4455
+ this.states[i]?.setParent(this, i);
4456
+ }
4457
+ const curr = this._value();
4458
+ const next = [...curr];
4459
+ next.splice(safeIndex, 0, ...items.map((item) => item.value()));
4460
+ this._value.set(next);
4461
+ this.notifyParent();
4462
+ }
4463
+ removeAt(index) {
4464
+ if (index < 0 || index >= this.states.length)
4465
+ return;
4466
+ this.states.splice(index, 1);
4467
+ this.states.forEach((state, i) => {
4468
+ state.setParent(this, i);
4469
+ });
4470
+ const curr = this._value() ?? [];
4471
+ const next = [...curr];
4472
+ next.splice(index, 1);
4473
+ this._value.set(next);
4474
+ this.notifyParent();
4475
+ }
4476
+ clear() {
4477
+ this.states.length = 0;
4478
+ this._value.set([]);
4479
+ this.notifyParent();
4380
4480
  }
4381
4481
  }
4382
4482