Comments (4)
I’d cast numbers to strings. I’d even cast objects to strings as well, but that doesn’t mean that the object should be serialize into a JSON strong. Maybe a simple .toString method call or new String(value) would suffice. All I know is that a lot of time was wasted trying to figure out why my code wasn’t working simply because I was using a number from my database as the value for the checkbox. The value was an ID of a record. It’s common to use numbers as values even HTML, however HTML will type cast to a string. This package should do the same IMO.
from react-use-form-state.
Hello again, @samholmes!
Is there a reason for this? Could it be possible to support other values like numbers, objects, or arrays?
Good question! I went back and forth on this; that is whether to serialize provided values or not. I ended up sticking to the DOM spec.
The value of the value
attribute of a <input type="checkbox">
is expected to be a string (a DOMString
).
React also treats values the same exact way: if you pass a value
other than strings to a checkbox, the value you get back will be a string. I believe it's for the same reason above.
<input
type="checkbox"
value={123} // non-string (also try objects, booleans, and arrays)
onChange={e =>
console.log({
value: e.target.value, // value will be casted to a string
type: typeof e.target.value // type will always be a string
})
}
/>
// logs: { value: "123", type: "string" }
Therefore, I don't believe it's possible to support non-primitive values such as objects and arrays (most underlying functionality depends on strict equality checks).
My goal with react-use-form-state is to follow the same expectation.
If not, then maybe it should be documented that value must be a string and to recommend serializing values into a string before hand.
This is documented in two places:
A) In the README.md under the "Input Types" section:
https://github.com/wsmd/react-use-form-state#input-types
B) It's also implicitly documented in the type declaration file (if you use typescript or vscode)
I believe this answers your questions above. If it doesn't, please let me know! I'm closing this issue for now, but that doesn't mean we have to stop talking about it! 😄
I might take casting non-string values provided to input.checkbox
to strings into consideration in order to support the following (for convenience):
<input {...input.checkbox('permissions', 4)} />
<input {...input.checkbox('permissions', 2)} />
<input {...input.checkbox('permissions', 1)} />
// Values in the result form state, however, would still have to be of type string!
{
values: {
permissions: ["4", "1", "2"]
}
}
Can you provide a code example of what you are trying to achieve in your code? It'll help understand this better.
from react-use-form-state.
Release 0.6.2
added ability to use non-string values such numbers, booleans, objects, arrays to checkboxes and radio buttons as own values. These vales will be casted to strings.
<input {...input.checkbox('options', 'value')} />;
<input {...input.checkbox('options', true)} />;
<input {...input.checkbox('options', 123)} />;
<input {...input.checkbox('options', ['a', 'b'])} />;
{
"values": {
"options": [
"value",
"123",
"a,b",
"true"
]
}
}
from react-use-form-state.
Interesting solution. The only problem I have with this from a use-case point of view is that false becomes a truthy value ("false"). Not good.
I'd suggest using JSON.stringify
on the value argument and put that in the element's value. Then when extracting the value out of the element, you could use JSON.parse
. The only caveat is that the argument must be JSON "stringify-able", but this could be documented.
from react-use-form-state.
Related Issues (20)
- Nested objects support HOT 1
- validate that fields match HOT 1
- Async validation HOT 2
- setField should not be packaged with formState HOT 2
- Published version of types lacking pristine/isPristine? HOT 2
- BUG: `validate` function is called on blur when `validateOnBlur` is not specified/false HOT 2
- react update - Cannot update a component from inside the function body of a different component. HOT 10
- Auto-saving forms HOT 1
- Reference Nested Values with Name HOT 1
- Question: how would one handle "dynamically added fields" ? HOT 2
- isPristine feature is not present in dist package HOT 5
- Password confirmation use case questions HOT 2
- Raw inputs are not given a `name` prop HOT 1
- Question - trigger validation on submit HOT 1
- Request: reset current values to initial values HOT 1
- Function to reset pristine HOT 2
- Request: Input type "number" with BaseInputProps - value is "number" type
- Maintained? HOT 4
- Unable to install with react's latest version react ^17.0.2 HOT 1
- React warning: Cannot update a component while rendering a different component when using `...text` in child component
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from react-use-form-state.