Giter Club home page Giter Club logo

Comments (5)

wsmd avatar wsmd commented on May 30, 2024 1

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 (a DOMString)
  • 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.

markerikson avatar markerikson commented on May 30, 2024 1

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:

image

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.

markerikson avatar markerikson commented on May 30, 2024 1

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.

RichardLindhout avatar RichardLindhout commented on May 30, 2024

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.

RichardLindhout avatar RichardLindhout commented on May 30, 2024

@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)

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.