reactjs Antd选择未设置初始值

pnwntuvh  于 2022-12-29  发布在  React
关注(0)|答案(2)|浏览(139)

在antd Select中设置初始值时遇到问题

const options=[{name:'john',id:1},{name:'jack',id:2},{name:'jill',id:3}]
   <Form initialValues={{user:3}}>
    <Form.Item name="user" label="Select user">
        <Select value="3">
        {options.map((user,index)=><Option key={index} value={user.id}>{user.name}</Option>)}
        </Select>
    </Form.Item>
   </Form>

它不是选择初始值。这里面有什么想法吗。这是正确的实现方式吗,请帮助

dffbzjpn

dffbzjpn1#

下面是在Antd表单中选择组件的一个简单演示我想知道选项是来自商店还是其他组件?因此表单在挂载后不会获得初始值。如果它来自其他源,则需要订阅初始值,以便在更改后重置Antd表单,如

import React from 'react';
import ReactDOM from 'react-dom';
import 'antd/dist/antd.css';
import './index.css';
import { Form, Input, Button, Checkbox, Select } from 'antd';
const layout = {
  labelCol: {
    span: 8,
  },
  wrapperCol: {
    span: 16,
  },
};
const tailLayout = {
  wrapperCol: {
    offset: 8,
    span: 16,
  },
};
// useEffect(() => {
  //   form.resetFields()
  //   form.setFieldsValue({
  //     productName: productName
  //   })
  // }, [initialValues]);
const userNameOptions = [{name:'john',id:1},{name:'jack',id:2},{name:'jill',id:3}]
const Demo = () => {
  const onFinish = (values) => {
    console.log('Success:', values);
  };

  const onFinishFailed = (errorInfo) => {
    console.log('Failed:', errorInfo);
  };

  return (
    <Form
      {...layout}
      name="basic"
      initialValues={{
        remember: true,
        username: 3
      }}
      onFinish={onFinish}
      onFinishFailed={onFinishFailed}
    >
      <Form.Item
        label="Username"
        name="username"
        rules={[
          {
            required: true,
            message: 'Please input your username!',
          },
        ]}
      >
        <Select value='3'>
          {userNameOptions.map((item, index) => (
            <Select.Option key={index} value={item.id}>
              {item.name}
            </Select.Option>
          ))}
        </Select>
      </Form.Item>

      <Form.Item
        label="Password"
        name="password"
        rules={[
          {
            required: true,
            message: 'Please input your password!',
          },
        ]}
      >
        <Input.Password />
      </Form.Item>

      <Form.Item {...tailLayout} name="remember" valuePropName="checked">
        <Checkbox>Remember me</Checkbox>
      </Form.Item>

      <Form.Item {...tailLayout}>
        <Button type="primary" htmlType="submit">
          Submit
        </Button>
      </Form.Item>
    </Form>
  );
};

ReactDOM.render(<Demo />, document.getElementById('container'));
igsr9ssn

igsr9ssn2#

我在ant design 4中使用了这个方法。如果只需要设置一次值,请使用defaultValue并传递一个字符串prop

defaultValue={!!props.DefaultString && claimId}

但是,您可以使用useEffect中的form. setFields Value设置当前值和选项的值。
例如:

useEffect(() => {
        form.setFieldsValue({
            SelectValue: !!props.selectItemValue && props.props.claimId,
            SelectableOptions: (!!props.selectableOptions? props.selectableOptions : undefined)
        );
    }
}, [props,form]);

<Form.Item name="SelectValue" >
    <Select >
        {!!props.selectableOptions && props.selectableOptions.map((opt) => <Option key={opt.id} value={opt.id}>{opt.name}</Option>)} 
    </Select>
 </Form.Item>

注意:直接使用props而不是fieldValue作为选项

相关问题