reactjs - 根据所选选项填充 Antd 输入值
问题描述
我想根据之前选择的名称填充一个输入值。例如,如果我选择“FRANCILIENNE CONSEIL”,我希望关联的正确 IBAN 作为输入值。
我尝试了几件事但没有成功。
这是我的代码的堆栈闪电战:https ://stackblitz.com/edit/react-rc3me7
解决方案
你可以创建一些states
来处理select option
&input
的初学者。然后通过您的handleChangeBeneficiary
函数更新它们。
- in
Option
, 如果您使用unique
variable
likebeneficiaryId
而不是 , 将是理想的,name
除非您name
也在这里unique
。 - 你可以在这里看到工作代码:https ://stackblitz.com/edit/react-rc3me7-gr3fmu
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>
);
};
推荐阅读
- python-3.x - 网页抓取 - 非 href
- python - 如何更改 python flask API 的 localhost url
- c++ - 为什么在这段代码中 S 必须是 const ?
- javascript - 无法从异步函数中检索返回的对象
- android - 谷歌标签管理器android事件没有真正回来
- excel - 获取日期范围 VBA 之间的所有月份和年份
- machine-learning - 计算成本低廉的图像分类器
- javascript - 如何打印多个订单的运输标签
- google-cloud-platform - 如何处理 Google Cloud Run 中的长请求?
- javascript - 无法在纸质列表框聚合物 3 中获取多选值