Skip to content

Commit 34d9c48

Browse files
authored
DynamicForm - Custom sorting (#1802)
* field custom sorting * sorting in mount/update + documentation --------- Co-authored-by: matteo <>
1 parent 759d11b commit 34d9c48

File tree

3 files changed

+32
-1
lines changed

3 files changed

+32
-1
lines changed

docs/documentation/docs/controls/DynamicForm.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ The `DynamicForm` can be configured with the following properties:
5050
| disabled | boolean | no | Allows form to be disabled. Default value is `false`|
5151
| disabledFields | string[] | no | InternalName of fields that should be disabled. Default value is `false`|
5252
| 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`|
53+
| fieldOrder | string[] | no | List of fields internal names. Specifies fields custom sorting. |
5354
| hiddenFields | string[] | no | InternalName of fields that should be hidden. Default value is `false`|
5455
| onListItemLoaded | (listItemData: any) => Promise&lt;void&gt; | no | List item loaded handler. Allows to access list item information after it's loaded.|
5556
| 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`.|

src/controls/dynamicForm/DynamicForm.tsx

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,25 @@ export class DynamicForm extends React.Component<
263263
);
264264
}
265265

266+
private sortFields = (fields: IDynamicFieldProps[], customSort: string[]): IDynamicFieldProps[] => {
267+
const fMap = new Map<string, IDynamicFieldProps>();
268+
269+
for (const field of fields) {
270+
fMap.set(field.columnInternalName.toLowerCase(), field);
271+
}
272+
273+
const sortedFields = customSort
274+
.map((sortColumn) => sortColumn.toLowerCase())
275+
.filter((normalizedSortColumn) => fMap.has(normalizedSortColumn))
276+
.map((normalizedSortColumn) => fMap.get(normalizedSortColumn))
277+
.filter((field) => field !== undefined);
278+
279+
const remainingFields = fields.filter((field) => !sortedFields.includes(field));
280+
const uniqueRemainingFields = Array.from(new Set(remainingFields));
281+
282+
return [...sortedFields, ...uniqueRemainingFields];
283+
}
284+
266285
private renderField = (field: IDynamicFieldProps): JSX.Element => {
267286
const { fieldOverrides } = this.props;
268287
const { hiddenByFormula, isSaving, validationErrors } = this.state;
@@ -996,6 +1015,10 @@ export class DynamicForm extends React.Component<
9961015
customIcons
9971016
);
9981017

1018+
const sortedFields = this.props.fieldOrder?.length > 0
1019+
? this.sortFields(tempFields, this.props.fieldOrder)
1020+
: tempFields;
1021+
9991022
// Get installed languages for Currency fields
10001023
let installedLanguages: IInstalledLanguageInfo[];
10011024
if (tempFields.filter(f => f.fieldType === "Currency").length > 0) {
@@ -1011,7 +1034,7 @@ export class DynamicForm extends React.Component<
10111034
footer: footerJSON
10121035
},
10131036
etag,
1014-
fieldCollection: tempFields,
1037+
fieldCollection: sortedFields,
10151038
installedLanguages,
10161039
validationFormulas
10171040
}, () => this.performValidation(true));

src/controls/dynamicForm/IDynamicFormProps.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,4 +126,11 @@ export interface IDynamicFormProps {
126126
* The key is the field internal name and the value is the Fluent UI icon name.
127127
*/
128128
customIcons?: { [columnInternalName: string]: string };
129+
130+
/**
131+
* Specify fields custom sorting.
132+
* The value is the field internal name.
133+
*/
134+
fieldOrder?: string[]
135+
129136
}

0 commit comments

Comments
 (0)