search

Home  >  Q&A  >  body text

How do I render these options correctly in a React form?

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粉463840170P粉463840170446 days ago455

reply all(1)I'll reply

  • P粉086993788

    P粉0869937882023-09-13 14:29:45

    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.

    reply
    0
  • Cancelreply