Skip to content

Commit b4cd72b

Browse files
authored
Merge pull request #1480 from martinlingstuyl/dynamicform-onlistitemloaded
Adds onListItemLoaded handler to DynamicForm. Closes #1472
2 parents 2333be3 + e1c9917 commit b4cd72b

File tree

5 files changed

+20
-7
lines changed

5 files changed

+20
-7
lines changed

docs/documentation/docs/controls/DynamicForm.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ The `DynamicForm` can be configured with the following properties:
3838
| contentTypeId | string | no | content type ID |
3939
| disabled | boolean | no | Allows form to be disabled. Default value is `false`|
4040
| disabledFields | string[] | no | InternalName of fields that should be disabled. Default value is `false`|
41+
| onListItemLoaded | (listItemData: any) => Promise<void> | no | List item loaded handler. Allows to access list item information after it's loaded.|
4142
| 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`.|
4243
| onSubmitted | (listItemData: any, listItem?: IItem) => void | no | Method that returns listItem data JSON object and PnPJS list item instance (`IItem`). |
4344
| onSubmitError | (listItemData: any, error: Error) => void | no | Handler of submission error. |

src/controls/dynamicForm/DynamicForm.tsx

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ export class DynamicForm extends React.Component<IDynamicFormProps, IDynamicForm
197197
}
198198
else if (fieldType === 'Thumbnail') {
199199
if (additionalData) {
200-
const uploadedImage = await this.uplaodImage(additionalData);
200+
const uploadedImage = await this.uploadImage(additionalData);
201201
objects[columnInternalName] = JSON.stringify({
202202
type: 'thumbnail',
203203
fileName: uploadedImage.Name,
@@ -354,7 +354,7 @@ export class DynamicForm extends React.Component<IDynamicFormProps, IDynamicForm
354354

355355
//getting all the fields information as part of get ready process
356356
private getFieldInformations = async (): Promise<void> => {
357-
const { listId, listItemId, disabledFields, respectETag } = this.props;
357+
const { listId, listItemId, disabledFields, respectETag, onListItemLoaded } = this.props;
358358
let contentTypeId = this.props.contentTypeId;
359359
try {
360360
const spList = await sp.web.lists.getById(listId);
@@ -363,6 +363,10 @@ export class DynamicForm extends React.Component<IDynamicFormProps, IDynamicForm
363363
if (listItemId !== undefined && listItemId !== null && listItemId !== 0) {
364364
item = await spList.items.getById(listItemId).get();
365365

366+
if (onListItemLoaded) {
367+
await onListItemLoaded(item);
368+
}
369+
366370
if (respectETag !== false) {
367371
etag = item['odata.etag'];
368372
}
@@ -453,7 +457,7 @@ export class DynamicForm extends React.Component<IDynamicFormProps, IDynamicForm
453457
termSetId = field.TermSetId;
454458
anchorId = field.AnchorId;
455459
if (item !== null) {
456-
const response = await this._spService.getSingleManagedMtadataLabel(listId, listItemId, field.InternalName);
460+
const response = await this._spService.getSingleManagedMetadataLabel(listId, listItemId, field.InternalName);
457461
if (response) {
458462
selectedTags.push({ key: response.TermID, name: response.Label });
459463
defaultValue = selectedTags;
@@ -545,7 +549,7 @@ export class DynamicForm extends React.Component<IDynamicFormProps, IDynamicForm
545549
}
546550
}
547551

548-
private uplaodImage = async (file: IFilePickerResult): Promise<IUploadImageResult> => {
552+
private uploadImage = async (file: IFilePickerResult): Promise<IUploadImageResult> => {
549553
const {
550554
listId,
551555
listItemId

src/controls/dynamicForm/IDynamicFormProps.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ export interface IDynamicFormProps {
1616
* List id
1717
*/
1818
listId: string;
19+
/**
20+
* List item loaded handler.
21+
* Allows to access list item information after it's loaded.
22+
*/
23+
onListItemLoaded?: (listItemData: any) => Promise<void>; // eslint-disable-line @typescript-eslint/no-explicit-any
1924
/**
2025
* Before submit handler.
2126
* Allows to modify the object to be submitted or cancel the submission.

src/extensions/testForm/components/TestForm.tsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ interface ITestFormState { }
1717
const LOG_SOURCE: string = 'TestForm';
1818

1919
export default class TestForm extends React.Component<ITestFormProps, ITestFormState> {
20-
20+
2121
constructor(props: ITestFormProps) {
2222
super(props);
2323

@@ -40,7 +40,10 @@ export default class TestForm extends React.Component<ITestFormProps, ITestFormS
4040
<DynamicForm
4141
context={this.props.context}
4242
listId={this.props.context.list.guid.toString()}
43-
listItemId={this.props.context.itemId} />
43+
listItemId={this.props.context.itemId}
44+
onListItemLoaded={async (listItemData: any) => {
45+
console.log(listItemData);
46+
}} />
4447
</EnhancedThemeProvider>);
4548
}
4649
}

src/services/SPService.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -645,7 +645,7 @@ export default class SPService implements ISPService {
645645
}
646646
}
647647

648-
public async getSingleManagedMtadataLabel(listId: string, listItemId: number, fieldName: string): Promise<any> { // eslint-disable-line @typescript-eslint/no-explicit-any
648+
public async getSingleManagedMetadataLabel(listId: string, listItemId: number, fieldName: string): Promise<any> { // eslint-disable-line @typescript-eslint/no-explicit-any
649649
try {
650650
const webAbsoluteUrl = this._context.pageContext.web.absoluteUrl;
651651
const apiUrl = `${webAbsoluteUrl}/_api/web/lists(@listId)/RenderListDataAsStream?@listId=guid'${encodeURIComponent(listId)}'`;

0 commit comments

Comments
 (0)