Search code examples
reactjsformik

how to use react number format with formik


My formik form is like this

const NewProduct = ({}) => {
    const validate = (values) => {
        const errors = {}
        if (!values.title) {
            errors.title = 'Required'
        }
        return errors
    }
    const formik = useFormik({
        initialValues: {
            title: '',
            price: '',
        },
        validate,
        onSubmit: (values) => {
            alert(JSON.stringify(values, null, 2))
        },
    })

    return (
        <div className="newProductComponent">
            <form onSubmit={formik.handleSubmit}>
                <label>title</label>
                <input
                    type="text"
                    id="title"
                    name="title"
                    onChange={formik.handleChange}
                    value={formik.values.title}
                />
                {formik.errors.title ? (
                    <div className="error">{formik.errors.title}</div>
                ) : null}

                <NumberFormat
                    value={formik.values.price}
                    thousandSeparator={true}
                    onValueChange={(values) => {
                        const { formattedValue, value } = values
                    }}
                />
            </form>
        </div>
    )
}

How can I get number format component in that form to work with formik?


Solution

  • useFormik - the hook you're already using - returns a function setFieldValue that can be used to manually set a value.

    First arg is field name price and second is the value. You also must set attribute name="price" on <NumberFormat>.

    const App = () => {
      const validate = (values) => {
          const errors = {}
          if (!values.title) {
              errors.title = 'Required'
          }
          return errors
      }
      const formik = useFormik({
          initialValues: {
              title: '',
              price: '',
          },
          validate,
          onSubmit: (values) => {
              alert(JSON.stringify(values, null, 2))
          },
      })
    
      return (
          <div className="newProductComponent">
              <form onSubmit={formik.handleSubmit}>
                  <label>title</label>
                  <input
                      type="text"
                      id="title"
                      name="title"
                      onChange={formik.handleChange}
                      value={formik.values.title}
                  />
                  {formik.errors.title ? (
                      <div className="error">{formik.errors.title}</div>
                  ) : null}
                  <br />
                  <label>Price</label>
                  <NumberFormat
                      name="price"
                      value={formik.values.price}
                      thousandSeparator={true}
                      onValueChange={(values) => {
                          const {  value } = values;
                          formik.setFieldValue('price', value);
                      }}
                  />
                  <br />
                  <button type="submit">Submit</button>
              </form>
          </div>
      )
    };
    

    Live Demo