Giter Club home page Giter Club logo

Comments (4)

samholmes avatar samholmes commented on May 30, 2024 1

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.

wsmd avatar wsmd commented on May 30, 2024

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)

screen shot 2019-01-31 at 1 06 52 am

screen shot 2019-01-31 at 1 19 10 am


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.

wsmd avatar wsmd commented on May 30, 2024

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.

samholmes avatar samholmes commented on May 30, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.