react 下拉框内容回显

需要实现效果如下


目前效果如下

思路 : 将下拉框选项的value和label一起存储到state中 , 初始化表单数据时 , 将faqType对应的label查找出来并设置到Form.Item中 , 最后修改useEffect
旧代码

javascript 复制代码
//可以拿到faqType为0 但是却没有回显出下拉框的内容 我需要faqType为0 回显出下拉框内容为对应的label 
  <Form
          form={form2}
          initialValues={{
            question: currentRecord.question || '',
            faqType: currentRecord.faqType || '',
          }}
        >   
            <Form.Item
                  label='问题类型'
                  name='faqType'
                  colon={false}
                  rules={[{ required: true, message: '请输入问题类型' }]}
                >
                  <Select
                    onChange={value => {
                      setSelectedCon1(value)
                      form2.setFieldsValue({ faqType: value })
                    }}
                    allowClear
                    showSearch
                    placeholder='请输入问题类型'
                    style={{ width: 300, height: 40 }}
                    options={[
                      { value: 0, label: '如何使用' },
                      { value: 1, label: '常见情况' },
                      { value: 2, label: '日常维护' },
                      { value: 3, label: '如何更换' }
                    ]}
                  />
                </Form.Item>



// 弹窗内部数据回显
useEffect(() => {
if (currentRecord) {
form2.setFieldsValue({
question: currentRecord.question || '',
faultInformationId: currentRecord.faultInformationId || '',
faqType: currentRecord.faqType || '',
answer: currentRecord.answer || ''
})
}
}, [currentRecord, form2])

解决问题的代码

javascript 复制代码
const [faqTypes, setFaqTypes] = useState([
  { value: 0, label: '如何使用' },
  { value: 1, label: '常见情况' },
  { value: 2, label: '日常维护' },
  { value: 3, label: '如何更换' }
]);


<Form.Item
  label='问题类型'
  name='faqType'
  colon={false}
  rules={[{ required: true, message: '请输入问题类型' }]}
>
  <Select
    onChange={value => {
      setSelectedCon1(value)
      form2.setFieldsValue({ faqType: value })
    }}
    allowClear
    showSearch
    placeholder='请输入问题类型'
    style={{ width: 300, height: 40 }}
    options={faqTypes.map(type => ({ value: type.value, label: type.label }))}
  />
</Form.Item>


useEffect(() => {
  if (currentRecord) {
    const selectedFaqType = faqTypes.find(type => type.value === currentRecord.faqType);
    form2.setFieldsValue({
      question: currentRecord.question || '',
      faultInformationId: currentRecord.faultInformationId || '',
      faqType: selectedFaqType ? selectedFaqType.label : '',
      answer: currentRecord.answer || ''
    })
  }
}, [currentRecord, form2, faqTypes])
相关推荐
GalaxyPokemon1 分钟前
PlayerFeedback 插件开发日志
java·服务器·前端
爱加班的猫1 分钟前
深入理解防抖与节流
前端·javascript
自由日记16 分钟前
学习中小牢骚1
前端·javascript·css
泽泽爱旅行20 分钟前
业务场景-opener.focus() 不聚焦解决
前端
VOLUN24 分钟前
Vue3 选择弹窗工厂函数:高效构建可复用数据选择组件
前端·javascript·vue.js
ErMao25 分钟前
深入理解let、const和var
前端
IT_陈寒28 分钟前
SpringBoot 3.2新特性实战:这5个隐藏功能让开发效率翻倍🚀
前端·人工智能·后端
涛哥AI编程29 分钟前
【AI编程干货】Token成为硬通货后,我的7000字Claude Code精算准则
前端·ai编程
IT_陈寒35 分钟前
Vue3性能优化实战:这5个技巧让我的应用加载速度提升70% 🚀
前端·人工智能·后端
golang学习记37 分钟前
从0死磕全栈之深入理解 Next.js 中的 NextResponse:API 详解与实战示例
前端