简体   繁体   中英

Does [React Final Form] work with Material-UI 3.x?

I want to use a TextField from Material-UI with react-final-form ( https://github.com/final-form/react-final-form ).

The main question is "How to get values object?"

I cannot get it from TextField.

The result is:

在此处输入图片说明

I've already tried different examples but nothing works.

My code:

import React from 'react';
import TextField from '@material-ui/core/TextField';
import Button from '@material-ui/core/Button';
import Card from '@material-ui/core/Card';
import './loginForm.css';
import { Form, Field } from 'react-final-form';

const handleSubmit = (event, values) => {
  console.log('onsubmit event target ', event.target);
  event.preventDefault();
  console.log('onSubmit values: ', values);
};

const onChange = (event) => {
  console.log('on changed: ', event.target.value);
};

const LoginForm = () => (
  <Card className="card">
    <Form
      onSubmit={handleSubmit}
      onChange={onChange}
      render={({ values }) => (
        <form className="login-form" onSubmit={handleSubmit}>
          <Field
            name="pin"
            component={TextField}
            id="standard-name"
            label="PIN"
            value={values}
            onChange={onChange}
          />
          <Button
            type="submit"
            onClick={handleSubmit}
            variant="contained"
            color="primary"
            className="login-btn"
          >
            Sign in
          </Button>
        </form>
      )}
    />{' '}
  </Card>
);

export default LoginForm;

versions:

"@material-ui/core": "^3.9.2",
"react-final-form": "^4.0.2",

You can't directly use @material-ui/core/TextField, you need to wrap it first:

 import React from 'react' import TextField from "@material-ui/core/TextField"; export default ({ input: { name, onChange, value, ...restInput }, meta, ...rest }) => ( <TextField {...rest} name={name} helperText={meta.touched ? meta.error : undefined} error={meta.error && meta.touched} inputProps={restInput} onChange={onChange} value={value} /> )

Now you have the necessary props passed to @ui/TextField. If you are not doing validation you don't need to pass the meta stuff.

https://github.com/final-form/react-final-form#third-party-components

https://codesandbox.io/s/2z5y03y81r

Yes! The accepted answer is quite good, but it would be really nice to have this done for us for all of the form components. The reason is that each form component has subtle differences that makes passing properties kind of complicated to figure out.

There was one existing project that made a good start to this and gets a ton of downloads each month, but it seems abandoned and stops at MUIv2.

So, I've created my own take on making something modern and tested. It is drop in easy to use and very flexible...

The demo also shows off one of my favorite features of RFF, which is the ability to easily control form renders. This improves performance of large forms, quite a bit.

Hi I try to implement this solution and I don't know why this UI broke and cant perform validation on the Select component https://stackblitz.com/edit/react-jtv7me?file=demo.js

can anyone help me with this?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM