微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何将对象数组绑定到再次包装另一个组件的列表组件?

如何解决如何将对象数组绑定到再次包装另一个组件的列表组件?

我正在尝试使用 mobx 绑定一组对象。

我正在用一个 TagFilter 组件包装一个材质 ui 芯片,该组件保持我的自定义 isSeleced 状态。

显示数据。

问题是当我选择芯片/标签“test 1”然后我选择芯片/标签“test 2”时,“test 1”没有被取消选择?

为什么?

为什么我在执行 this.props 时会在 TagFilters.onSelected 函数中看到子 onClick 函数属性

MainStore.ts

class MyStore implements IDtpStore {

  @observable public myFilters: ITagFilterProps[] =
    [
      { value: 'test1',label: 'test 1',isSelected: false },{ value: 'test2',label: 'test 2',];

}

MainComponent.tsx

private loader;
  private myStore;
  @observable private currentTechnology: string | undefined;
  @observable private currentLeadPurpose: string | undefined;

  constructor(p,c) {
    super(p,c);
    this.loader = Loader.create();
    this.myStore = new MyStore();
  }

 public componentDidMount() {    
      this.loader.load(this.myStore.loadFlow(...));
   
  }


render(){

      return (
      <>
          <TagFilters
          onChangeCurrentTag={this.onChangeCurrentTechnology}
          items={this.myStore.myFilters}
          backcolor={'#27A3CC'}
        />
      </>
    );

}

TagFilters.tsx

export interface ISelectedTagProps {
  onSelected(currentTag: string): void;
}

export interface IChangeCurrentTagProps {
  onChangeCurrentTag(currentTag: string): void;
}

class TagFilters extends React.Component<{ items: ITagFilterProps[] } & { backcolor: string }
  & IChangeCurrentTagProps & IWithDtpStoreProps & { intl: IntlShape },{}> {

  // private items: ITagFilterProps[];
  constructor(p,c);
    // this.items = this.props.items;
  }

  private onSelected = (currentTag: string) => {
    const { onChangeCurrentTag } = this.props;

    for (const item of this.props.items) {
      if (item.value !== currentTag) {
        // set all other chips/tags to false except the current tag/chip
        item.isSelected = false;
      }
    }
    onChangeCurrentTag(currentTag);
  }

  public render() {

    if (!this.props.items) {
      return null;
    }

    return (
      <>{
        this.props.items.map((element,index) => {
          return (
            <TagFilter
              backcolor={this.props.backcolor}
              onSelected={this.onSelected}
              key={index}
              label={element.label}
              value={element.value}
              isSelected={element.isSelected}
            />
          );
        })}
      </>
    );
  }
}

TagFilter.tsx

class TagFilter extends React.Component<ITagFilterProps & ISelectedTagProps & { backcolor: string } &
  IWithDtpStoreProps & { intl: IntlShape },{}> {
  @observable private isSelected = this.props.isSelected;


  private onClick = () => {
    this.isSelected = !this.isSelected;
    this.props.onSelected(this.props.value);
  }

  public render() {

    return (
      <>
        <Chip
          size={this.isSelected ? 'medium' : 'small'}
          style={{ background: this.props.backcolor,fontSize: '1.1em' }}
          label={this.props.label}
          id={this.props.value}
          clickable
          onClick={this.onClick}
        />
      </>
    );
  }
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。