Skip to content

Commit

Permalink
DynamicForm - Custom sorting (#1802)
Browse files Browse the repository at this point in the history
* field custom sorting

* sorting in mount/update + documentation

---------

Co-authored-by: matteo <>
  • Loading branch information
srpmtt authored Aug 16, 2024
1 parent 759d11b commit 34d9c48
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
1 change: 1 addition & 0 deletions docs/documentation/docs/controls/DynamicForm.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ The `DynamicForm` can be configured with the following properties:
| disabled | boolean | no | Allows form to be disabled. Default value is `false`|
| disabledFields | string[] | no | InternalName of fields that should be disabled. Default value is `false`|
| enableFileSelection | boolean | no | Specify if the form should support the creation of a new list item in a document library attaching a file to it. This option is only available for document libraries and works only when the contentTypeId is specified and has a base type of type Document. Default value is `false`|
| fieldOrder | string[] | no | List of fields internal names. Specifies fields custom sorting. |
| hiddenFields | string[] | no | InternalName of fields that should be hidden. Default value is `false`|
| onListItemLoaded | (listItemData: any) => Promise&lt;void&gt; | no | List item loaded handler. Allows to access list item information after it's loaded.|
| onBeforeSubmit | (listItemData: any) => Promise&lt;boolean&gt; | no | Before submit handler. Allows to modify the object to be submitted or cancel the submission. To cancel, return `true`.|
Expand Down
25 changes: 24 additions & 1 deletion src/controls/dynamicForm/DynamicForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,25 @@ export class DynamicForm extends React.Component<
);
}

private sortFields = (fields: IDynamicFieldProps[], customSort: string[]): IDynamicFieldProps[] => {
const fMap = new Map<string, IDynamicFieldProps>();

for (const field of fields) {
fMap.set(field.columnInternalName.toLowerCase(), field);
}

const sortedFields = customSort
.map((sortColumn) => sortColumn.toLowerCase())
.filter((normalizedSortColumn) => fMap.has(normalizedSortColumn))
.map((normalizedSortColumn) => fMap.get(normalizedSortColumn))
.filter((field) => field !== undefined);

const remainingFields = fields.filter((field) => !sortedFields.includes(field));
const uniqueRemainingFields = Array.from(new Set(remainingFields));

return [...sortedFields, ...uniqueRemainingFields];
}

private renderField = (field: IDynamicFieldProps): JSX.Element => {
const { fieldOverrides } = this.props;
const { hiddenByFormula, isSaving, validationErrors } = this.state;
Expand Down Expand Up @@ -996,6 +1015,10 @@ export class DynamicForm extends React.Component<
customIcons
);

const sortedFields = this.props.fieldOrder?.length > 0
? this.sortFields(tempFields, this.props.fieldOrder)
: tempFields;

// Get installed languages for Currency fields
let installedLanguages: IInstalledLanguageInfo[];
if (tempFields.filter(f => f.fieldType === "Currency").length > 0) {
Expand All @@ -1011,7 +1034,7 @@ export class DynamicForm extends React.Component<
footer: footerJSON
},
etag,
fieldCollection: tempFields,
fieldCollection: sortedFields,
installedLanguages,
validationFormulas
}, () => this.performValidation(true));
Expand Down
7 changes: 7 additions & 0 deletions src/controls/dynamicForm/IDynamicFormProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,4 +126,11 @@ export interface IDynamicFormProps {
* The key is the field internal name and the value is the Fluent UI icon name.
*/
customIcons?: { [columnInternalName: string]: string };

/**
* Specify fields custom sorting.
* The value is the field internal name.
*/
fieldOrder?: string[]

}

0 comments on commit 34d9c48

Please sign in to comment.