From f4e027b9b30a9875581b262cee4547e80ba2e791 Mon Sep 17 00:00:00 2001 From: Derek Burgman Date: Thu, 24 Nov 2022 23:45:07 -0600 Subject: [PATCH] feat: added duplicate button to repeatArrayField --- .../modules/form/container/value.component.ts | 8 ++- .../value/array/array.field.component.ts | 54 ++++++++++++++++++- .../formly/field/value/array/array.field.ts | 7 ++- packages/dbx-web/src/lib/layout/text/index.ts | 1 + 4 files changed, 64 insertions(+), 6 deletions(-) diff --git a/apps/demo/src/app/modules/doc/modules/form/container/value.component.ts b/apps/demo/src/app/modules/doc/modules/form/container/value.component.ts index a55c72a51..04df2c653 100644 --- a/apps/demo/src/app/modules/doc/modules/form/container/value.component.ts +++ b/apps/demo/src/app/modules/doc/modules/form/container/value.component.ts @@ -96,7 +96,9 @@ export class DocFormValueComponent { }), repeatArrayField({ key: 'test2', - label: 'Field With Add and Remove Diabled', + label: 'Field With Add and Remove Diabled Via Field', + description: 'Shows the remove button being disabled when a value is a certain value, and shows the duplicate button.', + duplicateText: 'Make Copy', repeatFieldGroup: [ nameField(), toggleField({ @@ -106,8 +108,10 @@ export class DocFormValueComponent { ], disableRearrange: true, allowAdd: true, + allowDuplicate: true, allowRemove: ({ i, value }) => !(value as { disable: boolean }).disable, - labelForField: ({ value }) => (value as { name: string }).name + labelForField: ({ value }) => (value as { name: string }).name, + addDuplicateToEnd: true }) ]; diff --git a/packages/dbx-form/src/lib/formly/field/value/array/array.field.component.ts b/packages/dbx-form/src/lib/formly/field/value/array/array.field.component.ts index d26be92e9..91ba1fad2 100644 --- a/packages/dbx-form/src/lib/formly/field/value/array/array.field.component.ts +++ b/packages/dbx-form/src/lib/formly/field/value/array/array.field.component.ts @@ -1,5 +1,5 @@ import { CdkDragDrop } from '@angular/cdk/drag-drop'; -import { Component, OnInit } from '@angular/core'; +import { Component, OnInit, TrackByFunction } from '@angular/core'; import { asDecisionFunction, asGetter, cachedGetter, DecisionFunction, FactoryWithInput, FactoryWithRequiredInput, Getter, getValueFromGetter, IndexRef, makeGetter, Maybe } from '@dereekb/util'; import { FieldArrayTypeConfig, FieldArrayType, FormlyFieldConfig, FormlyFieldProps } from '@ngx-formly/core'; @@ -17,6 +17,10 @@ export interface DbxFormRepeatArrayConfig extends Pick extends Pick>; /** * Whether or not to allow removing items. Can optionally pass a decision function that decides whether or not a specific item can be removed. */ allowRemove?: boolean | DecisionFunction>; + /** + * Adds the duplicate to the end of the values + */ + addDuplicateToEnd?: boolean; } @Component({ @@ -45,7 +57,7 @@ export interface DbxFormRepeatArrayConfig extends Pick
-
+
@@ -55,6 +67,7 @@ export interface DbxFormRepeatArrayConfig extends Pick{{ labelForItem(field, i) }} + @@ -83,6 +96,10 @@ export class DbxFormRepeatArrayTypeComponent extends FieldArrayType return asDecisionFunction(this.field.props.allowRemove, true); }); + private _allowDuplicate: Getter>> = cachedGetter(() => { + return asDecisionFunction(this.field.props.allowDuplicate || false, false); + }); + get repeatArrayField(): DbxFormRepeatArrayConfig { return this.field.props; } @@ -95,6 +112,10 @@ export class DbxFormRepeatArrayTypeComponent extends FieldArrayType return this.field.props.description; } + get duplicateText(): string { + return this.repeatArrayField.addText ?? 'Duplicate'; + } + get addText(): string { return this.repeatArrayField.addText ?? 'Add'; } @@ -119,6 +140,10 @@ export class DbxFormRepeatArrayTypeComponent extends FieldArrayType return this.field.props.allowAdd ?? true; } + get addDuplicateToEnd(): boolean { + return this.field.props.addDuplicateToEnd ?? false; + } + allowRemove(i: number) { const array: unknown[] = this.model; const value = array[i] as T; @@ -128,6 +153,15 @@ export class DbxFormRepeatArrayTypeComponent extends FieldArrayType }); } + allowDuplicate(i: number) { + const array: unknown[] = this.model; + const value = array[i] as T; + return this._allowDuplicate()({ + i, + value + }); + } + get addItemDisabled() { return !this.canAddItem; } @@ -142,6 +176,10 @@ export class DbxFormRepeatArrayTypeComponent extends FieldArrayType } } + readonly trackByFunction: TrackByFunction = (i, x) => { + return x.key; + }; + /** * Moves the target index up one value. * @@ -159,6 +197,18 @@ export class DbxFormRepeatArrayTypeComponent extends FieldArrayType this.swapIndexes(index, index + 1); } + duplicate(index: number) { + const array: unknown[] = this.model; + const targetValue = array[index]; + + if (!targetValue) { + return; + } + + const targetIndex = this.addDuplicateToEnd ? array.length : index; + this.add(targetIndex, targetValue, { markAsDirty: true }); + } + swapIndexes(currentIndex: number, targetIndex: number) { const array: unknown[] = this.model; const targetValue = array[currentIndex]; diff --git a/packages/dbx-form/src/lib/formly/field/value/array/array.field.ts b/packages/dbx-form/src/lib/formly/field/value/array/array.field.ts index 383f6bad3..def3f5ea6 100644 --- a/packages/dbx-form/src/lib/formly/field/value/array/array.field.ts +++ b/packages/dbx-form/src/lib/formly/field/value/array/array.field.ts @@ -8,7 +8,7 @@ export interface RepeatArrayFieldConfig extends DbxFormRepeatArrayC } export function repeatArrayField(config: RepeatArrayFieldConfig) { - const { key, label, description, repeatFieldGroup, maxLength, addText, removeText, labelForField, disableRearrange, allowAdd, allowRemove } = config; + const { key, label, description, repeatFieldGroup, maxLength, addText, removeText, duplicateText, labelForField, disableRearrange, allowAdd, allowRemove, allowDuplicate, addDuplicateToEnd } = config; return formlyField({ key, @@ -20,9 +20,12 @@ export function repeatArrayField(config: RepeatArrayFieldConfig) labelForField, addText, removeText, + duplicateText, disableRearrange, allowAdd, - allowRemove + allowRemove, + allowDuplicate, + addDuplicateToEnd }), fieldArray: { fieldGroup: asArray(repeatFieldGroup) diff --git a/packages/dbx-web/src/lib/layout/text/index.ts b/packages/dbx-web/src/lib/layout/text/index.ts index 5eed8d053..92399eebd 100644 --- a/packages/dbx-web/src/lib/layout/text/index.ts +++ b/packages/dbx-web/src/lib/layout/text/index.ts @@ -1,3 +1,4 @@ +export * from './form.description.component'; export * from './hint.component'; export * from './icon.spacer.component'; export * from './label.component';