首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >react-select :选项`isSelected`始终为true

react-select :选项`isSelected`始终为true
EN

Stack Overflow用户
提问于 2021-04-21 06:11:45
回答 1查看 310关注 0票数 1

当未选择任何内容时

当没有选择任何东西时,它工作得非常好。

选择一个选项时(问题)

正如您在上面看到的,即使我选择了一个选项(Ash),它也会显示所有内容都被选中。我做了console.log(isSelected),它说一切都被选中了true?..

应该是这样的。

react-select selected props

我复制了很多代码,只修改了几行。

这是我的代码

提前感谢您对我的帮助!

如果你有任何建议,请联系我!

代码语言:javascript
运行
复制
const dot = (color = '#ccc') => ({
  display: 'flex',
  alignItems: 'center',

  ':before': {
    backgroundColor: color,
    borderRadius: 10,
    content: '" "',
    display: 'block',
    marginRight: 10,
    height: 15,
    width: 15,
  },
});

const colourStyles = {
  control: (styles, { selectProps: { width } }) => ({
    ...styles,
    width: width,
  }),
  option: (styles, { data, isSelected, isFocused }) => {
    const color = chroma(data.color);
    return {
      ...styles,
      backgroundColor: isSelected
        ? data.color
        : isFocused
        ? color.alpha(0.1).css()
        : null,
      color: isSelected
        ? chroma.contrast(color, 'white') > 2
          ? 'white'
          : 'black'
        : data.color,
    };
  },
  input: (styles) => ({ ...styles, ...dot() }),
  placeholder: (styles) => ({ ...styles, ...dot() }),
  singleValue: (styles, { data }) => ({ ...styles, ...dot(data.color) }),
};


const SelectModule = observer(({ label, value }) => {
  const [selectedOption, setSelectedOption] = useState('');

  const options = colorChipListStore.colorList.map((item) => ({
    color: item.hexId,
    label: item.label,
  }));


  const setColors = (color) => {
    setSelectedOption(color);
  };

  function customTheme(theme) {
    return {
      ...theme,
      colors: {
        ...theme.colors,
        primary: '#AD9EE5',
      },
    };
  }


return (
    <Container>
      {label && <Label>{label}</Label>}
      <OptionWrapper>
          <Select
            width='100%'
            options={options}
            styles={colourStyles}
            theme={customTheme}
            isSearchable
            onChange={setColors}
            defaultValue={selectedOption}
          />
      </OptionWrapper>
    </Container>
  );
});
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-11-05 15:05:47

您的options对象没有值键。我也得到了这个,但当我将我的选项更改为以下格式: options = {label:'',value:''}时,我能够选择正确的选项。默认行为是使用键“value”进行所选选项检查。您可以通过使用'isOptionSelected‘并将选项重置为您想要用作检查的任何密钥来更改此行为。您还可以向对象添加值。

请参阅此GitHub issue

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67186934

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档