Comments (5)
Hi @hibado,
I think in your example above you meant to be using checkbox
not a radio
since the state is an array.
That said, numbers values in the form state are not currently support. Numeric values have to be strings.
This is because:
- the
value
attribute of a is and will always be a string (aDOMString
) - that makes it very tricky and not safe to assume that some values are going to be numbers.
- And even if they are numeric and I ended up parsing them to numbers, it might not be what users expect or want back. For example, a value may be an area code or a phone number "0011231234", or an id that starts with a 0.
To avoid confusion, this library, and many other form libraries, choose to work with strings. It's up to you if you want to cast such values to numbers.
Consider the following as a workaround to achieve what you want:
const Form = ({ onSubmit }) => {
const [{ values }, { checkbox }] = useFormState<{ status: string[] }>({
status: [0, 1, 2, 3].map(String),
});
const handleSubmit = e => {
e.preventDefault();
onSubmit({
...values,
status: values.status.map(Number),
});
};
return (
<form onSubmit={handleSubmit}>
{/*
as a convenience, you could still pass a number as a value (it'd be silly to
have to cast to a number for each input, so keep in mind these values will
be returned as strings in the form state
*/}
<input {...checkbox('status', 0)} />
<input {...checkbox('status', 1)} />
<input {...checkbox('status', 2)} />
<input {...checkbox('status', 3)} />
<button>Submit</button>
</form>
);
};
In future releases I might consider revisiting this issue and casting values to numbers only if an initial state is provided.
Thanks! Don't hesitate to reach out for any more questions you have.
from react-use-form-state.
I'm actually trying to use raw()
right now, and unfortunately it doesn't seem to work with TS for exactly this reason.
I'm specifically trying to use http://jquense.github.io/react-widgets/api/NumberPicker/ , which really does require a number
as its value, and its onChange
passes in just a number
.
That results in this TS error:
This is particularly annoying given that the source data field is a number, and I defined my TS form state type as having this field as a number.
from react-use-form-state.
Yeah, I just tried something along the same lines and got this to behave:
// components/react-widgets.tsx
import React from 'react';
import RWDateTimePicker from 'react-widgets/lib/DateTimePicker';
import RWNumberPicker from 'react-widgets/lib/NumberPicker';
type RWDTProps = React.ComponentProps<typeof RWDateTimePicker>;
type DTProps = Omit<RWDTProps, 'value'> & {
value?: Date | null;
};
export const DateTimePicker = ({ value, ...otherProps }: DTProps) => {
return <RWDateTimePicker value={value as any} {...otherProps} />;
};
type RWNPProps = React.ComponentProps<typeof RWNumberPicker>;
type NPProps = Omit<RWNPProps, 'value' | 'onChange'> & {
value?: number | string;
onChange?: (value: number) => any;
};
const noop = () => {};
export const NumberPicker = ({ value, onChange = noop, ...otherProps }: NPProps) => {
const innerOnChange = (value: number = 0) => {
onChange(value);
};
return <RWNumberPicker value={value as any} onChange={innerOnChange} {...otherProps} />;
};
// pages/myPage.tsx
import { DateTimePicker, NumberPicker } from 'components/react-widgets';
interface MyFormState {
a: number
}
const initialState : MyFormState = {
a: 0
}
const MyPage = () => {
const [formState, {raw}] = useFormState<MyFormState>(initialState);
return <NumberPicker {...raw('a')} />
}
That both compiles, and runs correctly, and I can see the actual numbers being stored.
from react-use-form-state.
Another alternative could be the raw input :-)
<Slider
{...raw('borderRadius')}
value={values.borderRadius || undefined}
aria-labelledby="border-radius-changer"
min={0}
max={100}
valueLabelDisplay="on"
/>
from react-use-form-state.
@markerikson Actually I had sort of the same thing! I just forgot to update the comment. What I did to overcome the issue was creating a custom component
interface FormSliderProps extends Omit<SliderProps, 'onChange'> {
onChange: (number: number) => any
}
function FormSlider({ onChange, ...rest }: FormSliderProps) {
const onInnerChange = (e: any, v: number | number[]) => {
onChange(Number(v) || 0)
}
return <Slider {...rest} onChange={onInnerChange} />
}
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.