How do I render these options correctly in a React form?
P粉463840170
P粉463840170 2023-09-13 00:03:43
0
1
432

I'm trying to get the options under APPRAISAL_TYPE to show up on my React form

class Appraisal(BaseModel):
    class APPRAISAL_TYPE(models.IntegerChoices):
        self_appraisal = 1
        line_manager = 2
        coo = 3
    name = models.CharField(max_length=200, blank=True)
    category = models.IntegerField(choices=APPRAISAL_TYPE.choices, default=APPRAISAL_TYPE.self_appraisal)
    description = models.TextField(max_length=200, blank=True)
    appraisal_for = models.ForeignKey(settings.AUTH_USER_MODEL, null=True, on_delete=models.SET_NULL, related_name="re_appraisal_for")
    appraised_by = models.CharField(max_length=200, blank=True)
<InputGroup>
              <InputLeftAddon children="Category" borderRadius="16px" />
              <Select
                name="category"
                value={APPRAISAL_TYPE[appraisalDetails.status]}
                options={categoryOptions}
                onChange={(option) => handleChange(option, "category")}
              />
            </InputGroup>

However, since category is an integer field, it should be concatenated with APPRAISAL_TYPE, so I get an error expecting an integer.

P粉463840170
P粉463840170

reply all(1)
P粉086993788

You can try this

// 如果这是一个昂贵的操作,可以使用useMemo
let categoryOptions = Object.entries(APPRAISAL_TYPE).map(([key, value]) => {
  //console.log("item", key, value);
  // 修改属性以适应组件所需的选项
  return {
    label: value.label,
    value: parseInt(value.value)
  };
});

// React JSX
 <InputGroup>
      <InputLeftAddon children="类别" borderRadius="16px" />
      <Select
          name="category"
          value={APPRAISAL_TYPE[appraisalDetails.status]}
          options={categoryOptions}
          onChange={(option) => handleChange(option, "category")}
       />
 </InputGroup>

What type of Select component is used, the properties of categoryOptions need to be changed accordingly

Hope this can help you solve your problem.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!