@@ -18,19 +18,13 @@ import { MessageBar, MessageBarType } from "@fluentui/react/lib/MessageBar";
1818import { ProgressIndicator } from "@fluentui/react/lib/ProgressIndicator" ;
1919import { IStackTokens , Stack } from "@fluentui/react/lib/Stack" ;
2020import { Icon } from "@fluentui/react/lib/components/Icon/Icon" ;
21- import * as React from "react" ;
22- import { IUploadImageResult } from "../../common/SPEntities" ;
23- import SPservice from "../../services/SPService" ;
24- import { IFilePickerResult } from "../filePicker" ;
2521import { DynamicField } from "./dynamicField" ;
2622import {
2723 DateFormat ,
2824 FieldChangeAdditionalData ,
2925 IDynamicFieldProps ,
3026} from "./dynamicField/IDynamicFieldProps" ;
31- import { IFilePickerResult } from "../filePicker" ;
32- import { IDynamicFormProps } from "./IDynamicFormProps" ;
33- import { IDynamicFormState } from "./IDynamicFormState" ;
27+ import { FilePicker , IFilePickerResult } from "../filePicker" ;
3428
3529// pnp/sp, helpers / utils
3630import { sp } from "@pnp/sp" ;
@@ -48,6 +42,10 @@ import { FormulaEvaluation } from "../../common/utilities/FormulaEvaluation";
4842import { Context } from "../../common/utilities/FormulaEvaluation.types" ;
4943import CustomFormattingHelper from "../../common/utilities/CustomFormatting" ;
5044
45+ // Dynamic Form Props / State
46+ import { IDynamicFormProps } from "./IDynamicFormProps" ;
47+ import { IDynamicFormState } from "./IDynamicFormState" ;
48+
5149const stackTokens : IStackTokens = { childrenGap : 20 } ;
5250
5351/**
@@ -179,6 +177,10 @@ export class DynamicForm extends React.Component<
179177 footerContent = this . _customFormatter . renderCustomFormatContent ( customFormatting . footer , this . getFormValuesForValidation ( ) , true ) as JSX . Element ;
180178 }
181179
180+ // Content Type
181+ let contentTypeId = this . props . contentTypeId ;
182+ if ( this . state . contentTypeId !== undefined ) contentTypeId = this . state . contentTypeId ;
183+
182184 return (
183185 < div >
184186 { infoErrorMessages . map ( ( ie , i ) => (
@@ -196,8 +198,8 @@ export class DynamicForm extends React.Component<
196198 { headerContent }
197199 { this . props . enableFileSelection === true &&
198200 this . props . listItemId === undefined &&
199- this . props . contentTypeId !== undefined &&
200- this . props . contentTypeId . startsWith ( "0x0101" ) &&
201+ contentTypeId !== undefined &&
202+ contentTypeId . startsWith ( "0x0101" ) &&
201203 this . renderFileSelectionControl ( ) }
202204 { ( bodySections . length > 0 && ! customFormattingDisabled ) && bodySections
203205 . filter ( bs => bs . fields . filter ( bsf => hiddenByFormula . indexOf ( bsf ) < 0 ) . length > 0 )
@@ -311,14 +313,18 @@ export class DynamicForm extends React.Component<
311313 const {
312314 listId,
313315 listItemId,
314- contentTypeId,
315316 onSubmitted,
316317 onBeforeSubmit,
317318 onSubmitError,
318319 enableFileSelection,
319320 validationErrorDialogProps,
320321 returnListItemInstanceOnSubmit
321322 } = this . props ;
323+
324+ let contentTypeId = this . props . contentTypeId ;
325+ if ( this . state . contentTypeId !== undefined ) contentTypeId = this . state . contentTypeId ;
326+
327+ const fileSelectRendered = ! listItemId && contentTypeId . startsWith ( "0x0101" ) && enableFileSelection === true ;
322328
323329 try {
324330
@@ -351,7 +357,7 @@ export class DynamicForm extends React.Component<
351357 }
352358
353359 // Check min and max values for number fields
354- if ( field . fieldType === "Number" && field . newValue !== undefined ) {
360+ if ( field . fieldType === "Number" && field . newValue !== undefined && field . newValue . trim ( ) !== "" ) {
355361 if ( ( field . newValue < field . minimumValue ) || ( field . newValue > field . maximumValue ) ) {
356362 shouldBeReturnBack = true ;
357363 }
@@ -380,18 +386,7 @@ export class DynamicForm extends React.Component<
380386 return ;
381387 }
382388
383- if ( enableFileSelection === true && this . state . selectedFile === undefined && this . props . listItemId === undefined ) {
384- this . setState ( {
385- missingSelectedFile : true ,
386- isValidationErrorDialogOpen :
387- validationErrorDialogProps
388- ?. showDialogOnValidationError === true ,
389- validationErrors
390- } ) ;
391- return ;
392- }
393-
394- if ( enableFileSelection === true && this . state . selectedFile === undefined && this . props . listItemId === undefined ) {
389+ if ( fileSelectRendered === true && this . state . selectedFile === undefined && this . props . listItemId === undefined ) {
395390 this . setState ( {
396391 missingSelectedFile : true ,
397392 isValidationErrorDialogOpen :
@@ -546,7 +541,7 @@ export class DynamicForm extends React.Component<
546541 ( ! contentTypeId . startsWith ( "0x0120" ) &&
547542 contentTypeId . startsWith ( "0x01" ) )
548543 ) {
549- if ( contentTypeId === undefined || enableFileSelection === true ) {
544+ if ( fileSelectRendered === true ) {
550545 await this . addFileToLibrary ( objects ) ;
551546 }
552547 else {
@@ -620,10 +615,7 @@ export class DynamicForm extends React.Component<
620615 }
621616 console . log ( "Error" , error ) ;
622617 }
623- } else if ( contentTypeId . startsWith ( "0x01" ) && enableFileSelection === true ) {
624- // We are adding a folder or a Document Set
625- await this . addFileToLibrary ( objects ) ;
626- }
618+ }
627619
628620 this . setState ( {
629621 isSaving : false ,
@@ -638,6 +630,9 @@ export class DynamicForm extends React.Component<
638630 }
639631 } ;
640632
633+ /**
634+ * Adds selected file to the library
635+ */
641636 private addFileToLibrary = async ( objects : { } ) : Promise < void > => {
642637 const {
643638 selectedFile
@@ -651,49 +646,52 @@ export class DynamicForm extends React.Component<
651646 returnListItemInstanceOnSubmit
652647 } = this . props ;
653648
654- try {
655- const idField = "ID" ;
656- const contentTypeIdField = "ContentTypeId" ;
657-
658- const library = await sp . web . lists . getById ( listId ) ;
659- const itemTitle =
660- selectedFile !== undefined && selectedFile . fileName !== undefined && selectedFile . fileName !== ""
661- ? ( selectedFile . fileName as string ) . replace (
662- / [ " | * | : | < | > | ? | / | \\ | | ] / g,
663- "_"
664- ) // Replace not allowed chars in folder name
665- : "" ; // Empty string will be replaced by SPO with Folder Item ID
666-
667- const fileCreatedResult = await library . rootFolder . files . addChunked ( encodeURI ( itemTitle ) , await selectedFile . downloadFileContent ( ) ) ;
668- const fields = await fileCreatedResult . file . listItemAllFields ( ) ;
669-
670- if ( fields [ idField ] ) {
671- // Read the ID of the just created folder or Document Set
672- const folderId = fields [ idField ] ;
673-
674- // Set the content type ID for the target item
675- objects [ contentTypeIdField ] = contentTypeId ;
676- // Update the just created folder or Document Set
677- const iur = await library . items . getById ( folderId ) . update ( objects ) ;
678- if ( onSubmitted ) {
679- onSubmitted (
680- iur . data ,
681- returnListItemInstanceOnSubmit !== false
682- ? iur . item
683- : undefined
684- ) ;
649+
650+ if ( selectedFile !== undefined ) {
651+ try {
652+ const idField = "ID" ;
653+ const contentTypeIdField = "ContentTypeId" ;
654+
655+ const library = await sp . web . lists . getById ( listId ) ;
656+ const itemTitle =
657+ selectedFile !== undefined && selectedFile . fileName !== undefined && selectedFile . fileName !== ""
658+ ? ( selectedFile . fileName as string ) . replace (
659+ / [ " | * | : | < | > | ? | / | \\ | | ] / g,
660+ "_"
661+ ) // Replace not allowed chars in folder name
662+ : "" ; // Empty string will be replaced by SPO with Folder Item ID
663+
664+ const fileCreatedResult = await library . rootFolder . files . addChunked ( encodeURI ( itemTitle ) , await selectedFile . downloadFileContent ( ) ) ;
665+ const fields = await fileCreatedResult . file . listItemAllFields ( ) ;
666+
667+ if ( fields [ idField ] ) {
668+ // Read the ID of the just created folder or Document Set
669+ const folderId = fields [ idField ] ;
670+
671+ // Set the content type ID for the target item
672+ objects [ contentTypeIdField ] = contentTypeId ;
673+ // Update the just created folder or Document Set
674+ const iur = await library . items . getById ( folderId ) . update ( objects ) ;
675+ if ( onSubmitted ) {
676+ onSubmitted (
677+ iur . data ,
678+ returnListItemInstanceOnSubmit !== false
679+ ? iur . item
680+ : undefined
681+ ) ;
682+ }
683+ } else {
684+ throw new Error (
685+ "Unable to read the ID of the just created folder or Document Set"
686+ ) ;
687+ }
688+ } catch ( error ) {
689+ if ( onSubmitError ) {
690+ onSubmitError ( objects , error ) ;
691+ }
692+ console . log ( "Error" , error ) ;
685693 }
686- } else {
687- throw new Error (
688- "Unable to read the ID of the just created folder or Document Set"
689- ) ;
690694 }
691- } catch ( error ) {
692- if ( onSubmitError ) {
693- onSubmitError ( objects , error ) ;
694- }
695- console . log ( "Error" , error ) ;
696- }
697695 }
698696
699697 /**
@@ -999,6 +997,7 @@ export class DynamicForm extends React.Component<
999997 }
1000998
1001999 this . setState ( {
1000+ contentTypeId,
10021001 clientValidationFormulas,
10031002 customFormatting : {
10041003 header : headerJSON ,
@@ -1435,5 +1434,4 @@ export class DynamicForm extends React.Component<
14351434 }
14361435 }
14371436
1438-
14391437}
0 commit comments