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

Dynamic Form: add children to override field rendering #1257

Closed
bjdekker opened this issue Jul 7, 2022 · 10 comments
Closed

Dynamic Form: add children to override field rendering #1257

bjdekker opened this issue Jul 7, 2022 · 10 comments
Labels
status:fixed-next-drop Issue will be fixed in upcoming release. type:enhancement New feature or enhancement of existing capability
Milestone

Comments

@bjdekker
Copy link

bjdekker commented Jul 7, 2022

Category

[x] Enhancement
[ ] Bug
[ ] Question

Version

Please specify what version of the library you are using: [3.8.1]

Expected / Desired Behavior / Question

Currently, the DynamicForm does not allow for overriding individual fields. So you get the form as is. If you want one field to behave differently, you have to write the entire form yourself. I would like to suggest that you can add children to the DynamicForm for the fields that you want to render yourself. Let's say you have a list with three fields: Title, SomeTextField and SomeOtherField and you want to do custom rendering for SomeOtherField, the you could put in a DynamicForm like this:

<DynamicForm 
          context={this.props.context} 
          listId={"3071c058-549f-461d-9d73-8b9a52049a80"}  
          listItemId={1}
          onCancelled={() => { console.log('Cancelled') }}
          onBeforeSubmit={async (listItem) => { return true; }}
          onSubmitError={(listItem, error) => { alert(error.message); }}
          onSubmitted={async (listItemData) => { console.log(listItemData); }}>
          <SomeCustomFieldComponent internalFieldName="SomeOtherField" /> //Other Properties can be added as well
</DynamicForm>

In the render function of the DynamicForm, you can check in the map whether the children of the component contains a child with the specified internalFieldName, if so, that child (in this case SomeCustomFieldComponent) is rendered, if not, nothing changes and the DynamicField is rendered.
This need to be worked out in more detail, but I'd be happy to check if this can be implemented properly and to submit a pull request for this.

@ghost
Copy link

ghost commented Jul 7, 2022

Thank you for reporting this issue. We will be triaging your incoming issue as soon as possible.

@ghost ghost added the Needs: Triage 🔍 label Jul 7, 2022
@github-actions
Copy link

github-actions bot commented Jul 7, 2022

Thank you for submitting your first issue to this project.

@AJIXuMuK
Copy link
Collaborator

AJIXuMuK commented Jul 7, 2022

Thank you @bjdekker for the suggestion!
Would you be able to create a PR for that new feature?

@bjdekker
Copy link
Author

bjdekker commented Jul 8, 2022

Thank you @AJIXuMuK, I try to find some time the coming days to implement this and will create a pul request then

@IRRDC
Copy link
Contributor

IRRDC commented Jul 12, 2022

This looks like it could be used to hide disabled fields. Looking forward to see it in the next version :-)

@AJIXuMuK AJIXuMuK added status:fixed-next-drop Issue will be fixed in upcoming release. type:enhancement New feature or enhancement of existing capability and removed Needs: Triage 🔍 labels Aug 3, 2022
@AJIXuMuK AJIXuMuK added this to the 3.10.0 milestone Aug 3, 2022
@IRRDC
Copy link
Contributor

IRRDC commented Aug 11, 2022

@bjdekker I gave it a test and used it to completely hide some fields or alternatively to display them as disabled with a default value that I than add to the data in onBeforeSubmit. Which works fine (had to have a look at the code to see that I have to set newValue to get required fields to work and skip setting values in onBeforeSubmit). Could do with a nice docu and examples ;-)

@martinlingstuyl
Copy link
Contributor

Great idea @bjdekker, can't wait to try this out somewhere.

@bjdekker
Copy link
Author

@IRRDC thanks for checking it out! I will take a look at providing some kind of documentation!

@rakeshrelan118
Copy link

@AJIXuMuK, Can you please share example for the usage

@martinlingstuyl
Copy link
Contributor

Hi @rakeshrelan118 ,
@bjdekker has planned to create an example blog on this.

Basically it can be set up as follows:

const fieldOverrides = {
  "FieldInternalName1": (fieldProperties: IDynamicFieldProps) => <>some-ui</>,
  "FieldInternalName2": (fieldProperties: IDynamicFieldProps) => <>some-ui</>
};

<DynamicForm 
  listId={someListId}
  webAbsoluteUrl={someSiteUrl} 
  context={spfxContext} 
  fieldOverrides={fieldOverrides}
/>

I believe you can use the fieldProperties to update the value of the field that should be submitted.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status:fixed-next-drop Issue will be fixed in upcoming release. type:enhancement New feature or enhancement of existing capability
Projects
None yet
Development

No branches or pull requests

5 participants