Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DynamicForm - Custom sorting #1802

Merged
merged 2 commits into from
Aug 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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<void> | no | List item loaded handler. Allows to access list item information after it's loaded.|
| onBeforeSubmit | (listItemData: any) => Promise<boolean> | no | Before submit handler. Allows to modify the object to be submitted or cancel the submission. To cancel, return `true`.|
Expand Down
27 changes: 25 additions & 2 deletions 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 @@ -418,7 +437,7 @@ export class DynamicForm extends React.Component<
if (field.newValue !== null && field.newValue !== undefined) {

let value = field.newValue;

if (["Lookup", "LookupMulti", "User", "UserMulti", "TaxonomyFieldTypeMulti"].indexOf(fieldType) < 0) {
objects[columnInternalName] = value;
}
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 @@ -122,4 +122,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[]
michaelmaillot marked this conversation as resolved.
Show resolved Hide resolved

}