【问题标题】:Material UI - Autocomplete form labels with 2 variablesMaterial UI - 带有 2 个变量的自动完成表单标签
【发布时间】:2020-07-17 23:29:26
【问题描述】:

如何在自动完成表单的标签中同时显示标题和年份?

这是标准的 Material UI 演示。当我尝试使用数组在表单标签中同时显示标题和年份时,我收到关于小写结果的错误。我认为这是因为该值不再是字符串 - 但我不知道如何解决这个问题。

<Autocomplete
      multiple
      id="checkboxes-tags-demo"
      options={top100Films}
      disableCloseOnSelect
      getOptionLabel={(option) => [{option.title}, {option.year}]}  // - this doesn't work
      renderOption={(option, { selected }) => (
        <React.Fragment>
          <Checkbox
            icon={icon}
            checkedIcon={checkedIcon}
            style={{ marginRight: 8 }}
            checked={selected}
          />
          //[{option.title}, {option.year}] - this doesn't work
        </React.Fragment>
      )}
      style={{ width: 500 }}
      renderInput={(params) => (
        <TextField {...params} variant="outlined" label="Checkboxes" placeholder="Favorites" />
      )}
    />
  );
}

// Top 100 films as rated by IMDb users. http://www.imdb.com/chart/top
const top100Films = [
  { title: 'The Shawshank Redemption', year: 1994 },
  { title: 'The Godfather', year: 1972 },

【问题讨论】:

    标签: reactjs material-ui


    【解决方案1】:

    所以 - 解决方案是单独保留选项标签(只是标题),然后在复选框标签中同时使用标题和年份,如下所示:

    {option.title} {option.year}

    我不明白为什么这有效,但确实有效。

    【讨论】:

      猜你喜欢
      • 2020-03-27
      • 2020-10-19
      • 1970-01-01
      • 1970-01-01
      • 2023-03-24
      • 2020-12-17
      • 2021-08-13
      • 1970-01-01
      • 2020-07-30
      相关资源
      最近更新 更多