-
Notifications
You must be signed in to change notification settings - Fork 91
fix(all): enable input type file #458
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
Conversation
Can't really say anything useful without an example usage piece of code 🤔 |
@skateman I added examples to the docs. Source starts from here: https://github.com/data-driven-forms/react-forms/pull/458/files#diff-63eddbe3b4330a752e2fa0ffad9dba2fR1 |
Codecov Report
@@ Coverage Diff @@
## master #458 +/- ##
=======================================
Coverage 89.70% 89.71%
=======================================
Files 138 138
Lines 2342 2354 +12
Branches 766 770 +4
=======================================
+ Hits 2101 2112 +11
- Misses 241 242 +1
Continue to review full report at Codecov.
|
|
||
# File input | ||
|
||
Files cannot be easilly uploaded in JSON payload. In order to upload files usigng the input type *file* you can follow these steps. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
easilly > easily
usigng > using
|
||
## File onChange payload | ||
|
||
In order to successfully store the file reference, you have either use an input of type file or use an object with the following shape in your on change function ([visit MDN docs for more info on file upload](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/file)). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe: 'use an input of "file" type'
|
||
## Getting file from state. | ||
|
||
When submitting, you will have to construct the binary via FormData or encode the file to Base64, depending on your use case. Be aware that FormData cannot be sent in the JSON payload. Binaries are destroyed when serializing JSON. There will be a list of field names with the type file avaiable in the submit function arguments. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
avaiable > available
|
||
When submitting, you will have to construct the binary via FormData or encode the file to Base64, depending on your use case. Be aware that FormData cannot be sent in the JSON payload. Binaries are destroyed when serializing JSON. There will be a list of field names with the type file avaiable in the submit function arguments. | ||
|
||
The `formApi.fileInputs` is an array of field names with `type: file`. Be aware that if your filed name is in nested, you have to use the lodash like method of getting the value from state. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
filed > field
@@ -76,6 +76,9 @@ const useFieldApi = ({ name, initializeOnMount, component, render, validate, ... | |||
}; | |||
|
|||
const fieldProps = useField(name, enhancedProps); | |||
if (fieldProps.input.type === 'file' && typeof fieldProps.input.value === 'object') { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this modification should be moved to the return statement of this hook.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
With this the input object is basically being modified at two different places. I would keep it at one place - it would be more consistent and readable.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
its not going to be pretty. But ok
@@ -170,6 +177,8 @@ const useFieldApi = ({ name, initializeOnMount, component, render, validate, ... | |||
...(arrayValidator ? { arrayValidator } : {}), | |||
input: { | |||
...fieldProps.input, | |||
value: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about?
...(fieldProps.input.type === 'file' && typeof fieldProps.input.value === 'object' && {value: fieldProps.input.value.inputValue})
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
that's how I originally wrote it but linter wanted it this way
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🤦♂️
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me 👍
I was trying to make my own component for file inputs but couldn't go past the enhancedOnChange at the moment so I'm glad this PR exists, thank you ! |
@davellx The validation of input files is a bit tricky compared to other components. I think adding an example with a custom validator is a good idea. 👍 |
🎉 This PR is included in version 2.2.0 🎉 The release is available on Demo can be found here! |
closes #307
this will have to be backported to V1
To Do