twisty / formsy-react-components

Bootstrap components for a formsy-react form.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Select with numerical values

SachaG opened this issue · comments

I'm getting a warning because my values are numbers, not strings:

https://github.com/twisty/formsy-react-components/blob/master/src/components/select.js#L64

Warning: Failed prop type: Invalid prop `options[1].value` of type `number` supplied to `SelectControl`, expected `string`.

Should I be converting my number to strings before passing them to the Select? Or should this be fixed so that selects can also handle numerical values?

For now, just convert the numbers to strings. The selected attribute is added by React, and I'm not sure how it would deal with two values like 1 (int) and "1" (string).

Just accepting strings makes it easier to reason about. An option might be to use formsy-react's mapping option to manipulate the string value back to another data type on submit.

I notice react-select does a string conversion, so I may explore this avenue in the future.

const stringifyValue = value =>
	typeof value === 'string'
		? value
		: (value !== null && JSON.stringify(value)) || '';