【问题标题】:Populate Antd Input value depending on selected option根据所选选项填充 Antd 输入值
【发布时间】:2021-11-02 13:50:04
【问题描述】:

我想根据之前选择的名称填充一个输入值。 例如,如果我选择“FRANCILIENNE CONSEIL”,我希望关联的正确 IBAN 作为输入值。

我尝试了好几件事都没有成功。

这是我的代码的堆栈闪电:https://stackblitz.com/edit/react-rc3me7

【问题讨论】:

    标签: reactjs antd


    【解决方案1】:

    您可以创建一些states 来处理select optioninput 作为初学者。然后通过您的 handleChangeBeneficiary 函数更新它们。

    import React, { useState } from 'react'
    
    const Demo = () => {
      const [beneficiary, setbeneficiary] = useState()
      const [iban, setiban] = useState()
    
      const handleChangeBeneficiary = (value) => {
        console.log(`selected ${value}`);
        setbeneficiary(value)
    
        // get selected iban
        const selected = beneficiaries?.find(item => item?.beneficiaryId == value)
        setiban(selected?.iban)
      };
    
      const onFinish = (values) => {
        console.log('Success:', values);
      };
    
      const onFinishFailed = (errorInfo) => {
        console.log('Failed:', errorInfo);
      };
    
      const beneficiaries = [
        {
          iban: 'FR76167LQSDKJLKSQJ86538089',
          name: 'FRANCILIENNE CONSEIL',
          bic: 'TRZOFR21XXX',
          beneficiaryId: '60c38ddf-63f9-4589-888b-27b7e1a50e53',
        },
        {
          iban: 'FR291001DSKLFJSLKJ8633Z17',
          name: 'MR NAMLA EMAD',
          bic: 'PSSTFRPPCNE',
          beneficiaryId: '60a11891-81ba-4ab2-9b92-ce4f461c2d50',
        },
      ];
      return (
        <Form
          {...layout}
          name="test"
          onFinish={onFinish}
          onFinishFailed={onFinishFailed}
          autoComplete="off"
        >
          <Form.Item label="Nom du bénéficiare" name="benef">
            <Select
              // defaultValue=""
              value={beneficiary}
              style={{ width: 300, marginBottom: 20 }}
              onChange={handleChangeBeneficiary}
            >
              {beneficiaries.map((nom) => (
                <Option value={nom.beneficiaryId}> {nom.name} </Option>
              ))}
            </Select>
          </Form.Item>
          <Form.Item label="IBAN" name="iban">
            <Input 
              // autoComplete="off" 
              style={{ marginBottom: 20 }}
              placeholder={iban} 
            disabled/>
          </Form.Item>
          <Form.Item wrapperCol={{ ...layout.wrapperCol, offset: 8 }}>
            <Button type="primary" htmlType="submit">
              Submit
            </Button>
          </Form.Item>
        </Form>
      );
    };
    

    【讨论】:

    • 感谢您的回答!不能设置 value={iban} 而不是 placeholder={iban} ?
    • 这取决于你想怎么做。因为通过选择beneficiary 将自动为我们set iban,那么使用placeholder 将比value 更有意义。仅当我们期望用户更改它们的操作时,我们才使用value。所以如果我们不打算使用onChange,那么我们不妨直接使用placeholder
    • 好的,我知道了,非常感谢!我问这个是因为 onSubmit,iban 是未定义的
    • 哦。忘了你在form 上使用onSubmit。然后你应该添加value={iban}
    【解决方案2】:

    您也可以更新 IBAN 的值。请尝试以下代码。

    import React, {useState} from 'react';
    import ReactDOM from 'react-dom';
    import 'antd/dist/antd.css';
    import './index.css';
    import { Form, Input, InputNumber, Button, Select } from 'antd';
    const layout = {
      labelCol: {
        span: 8,
      },
      wrapperCol: {
        span: 16,
      },
    };
    /* eslint-disable no-template-curly-in-string */
    
    const { Option } = Select;
    
    /* eslint-enable no-template-curly-in-string */
    
    const Demo = () => {
    
      const [iban,setValue] =useState('')
      const handleChangeBeneficiary = (value) => {
        console.log(`selected ${value}`);
        const ben= beneficiaries.filter((b)=>b.name===value)
        setValue(value)
       
      };
    
    
      
    
      const onFinish = (values) => {
        console.log('Success:', values);
      
      };
    
      const onFinishFailed = (errorInfo) => {
        console.log('Failed:', errorInfo);
      };
    
      const beneficiaries = [
        {
          iban: 'FR76167LQSDKJLKSQJ86538089',
          name: 'FRANCILIENNE CONSEIL',
          bic: 'TRZOFR21XXX',
          beneficiaryId: '60c38ddf-63f9-4589-888b-27b7e1a50e53',
        },
        {
          iban: 'FR291001DSKLFJSLKJ8633Z17',
          name: 'MR NAMLA EMAD',
          bic: 'PSSTFRPPCNE',
          beneficiaryId: '60a11891-81ba-4ab2-9b92-ce4f461c2d50',
        },
      ];
      return (
        <Form
          {...layout}
          name="test"
          onFinish={onFinish}
          onFinishFailed={onFinishFailed}
          autoComplete="off"
        >
          <Form.Item label="Nom du bénéficiare" name="benef">
            <Select
              defaultValue=""
              style={{ width: 300, marginBottom: 20 }}
              onChange={handleChangeBeneficiary}
            >
              {beneficiaries.map((nom) => (
                <Option value={nom.name}> {nom.name} </Option>
              ))}
            </Select>
          </Form.Item>
          <Form.Item label="IBAN">
            <Input autoComplete="off" style={{ marginBottom: 20 }} value={iban}/>
          </Form.Item>
          <Form.Item wrapperCol={{ ...layout.wrapperCol, offset: 8 }}>
            <Button type="primary" htmlType="submit">
              Submit
            </Button>
          </Form.Item>
        </Form>
      );
    };
    
    ReactDOM.render(<Demo />, document.getElementById('container'));
    
    
    
    
    
    
    

    【讨论】:

      猜你喜欢
      • 2017-02-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-01
      • 1970-01-01
      • 2021-07-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多