React Native - 当onPress一个项目时如何隐藏数组中的其他项目?

时间:2018-03-11 00:34:07

标签: javascript reactjs react-native

我在视图中显示了一系列项目(Circle组件), enter image description here

但是当我点击一个时,我不确定如何隐藏所有其他内容(onPress设置为放大并提供有关我点击的那个圆圈的更多信息卡。) < / p>

enter image description here

这是我的RN代码..任何想法?

class Circle extends Component {
  constructor(props) {
    super(props);
    this.state = { opened: false};
    this.onPress = this.onPress.bind(this);
  }

  onPress() {
    this.props.onPress();
    if (this.props.noAnimation) {
      return;
    }

    if (this.state.opened) {
      this.refs.nextEpisode.fadeOutLeft();
      this.refs.description.fadeOutDownBig();
      return  this.setState({opened: false, windowPos: null});
    }
    this.refs.container.measureInWindow((x, y) => {
      this.refs.nextEpisode.slideInLeft();
      setTimeout(() => this.refs.description.fadeInUpBig(), 400);
      this.setState({
        opened: true,
        windowPos: { x, y },
      });
    });
  }

  render() {
    const data = this.props.data;
    const { opened } = this.state;
    const styles2 = getStyles(opened, (this.props.index % 2 === 0));

    const containerPos = this.state.windowPos ? {
      top: - this.state.windowPos.y + 64
    } : {};

    return (
      <TouchableWithoutFeedback onPress={this.onPress}>
        <View style={[styles2.container, containerPos]} ref="container" >
          <TvShowImage tvShow={data} style={styles2.image} noShadow={opened} openStatus={opened}/>
          <View style={styles2.title}>
            <Title
              tvShow={data}
              onPress={this.onPress}
              noShadow={opened}
            />
            <Animatable.View
              style={styles2.animatableNextEpisode}
              duration={800}
              ref="nextEpisode"
            >
              <NextEpisode tvShow={data}/>
            </Animatable.View>
          </View>
          <Animatable.View
            style={styles2.description}
            duration={800}
            ref="description"
            delay={400}
          >
            <Text style={styles2.descriptionText}>{data.item_description}</Text>
          </Animatable.View>
        </View>
      </TouchableWithoutFeedback>
    );
  }
}
Circle.defaultProps = {
  index: 0,
  onPress: () => {}
};

(请注意,当照片与食物有关时,某些变量名称与电视节目相关,但还没有时间修复)。

FYI映射Circle组件数组的父组件如下所示:

class Favorites extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      circleCount: 0
    };
    this.addCircle = this.addCircle.bind(this);
  }
  componentDidMount() {
    for(let i = 0; i < this.props.screenProps.appstate.length; i++) {
      setTimeout(() => {
        this.addCircle();
      }, (i*100));
    }
  }
  addCircle = () => {
    this.setState((prevState) => ({circleCount: prevState.circleCount + 1}));
  }

  render() {
    var favoritesList = this.props.screenProps.appstate;

    var circles = [];
    for (let i = 0; i < this.state.circleCount; i++) {

      circles.push(
          <Circle key={favoritesList[i].url} style={styles.testcontainer} data={favoritesList[i]}>

          </Circle>
      );
    }

    return (
        <ScrollView style={{backgroundColor: '#fff9f9'}}>
          <View style={styles.favoritesMainView}>
            <View style={styles.circleContainer}>
              {circles}
            </View>
          </View>
        </ScrollView>
    );
  }
}

1 个答案:

答案 0 :(得分:2)

脱离我的头脑(如果我理解你想要的东西)可能会有很少的解决方案。

您可能需要跟踪“选定”圆圈并根据该样式设置组件的样式。例如,如果您仍然需要元素的高度,则可以使用{height: 0}{opacity: 0}设置未选择的样式。

要跟踪我会尝试以下内容:

在收藏夹状态:

this.state = {
  circleCount: 0,
  selected: -1
};

并将3个新值传递给circle,第三个是更改“selected”状态的函数:

<Circle
  key={favoritesList[i].url}
  style={styles.testcontainer}
  data={favoritesList[i]}
  index={i}
  selected={this.state.selected}
  onClick={(index) => {
    this.setState(prevState => {
      return { ...prevState, selected: index }
    });
  }}
/>

在Circle中,我们使用我们传递的方法将已按下的索引传递给收藏夹:

onPress() {
  this.props.onClick(this.props.index);
  ...

在Circle的渲染方法中,我们创建一个不透明样式来隐藏当前未选中的任何元素(但仅当选择了一个元素时 - 意味着如果它是-1则不选择任何元素,并且不应将任何不透明度应用于任何圆:< / p>

render() {
  const { selected, index } = this.props;
  let opacityStyle = {};
  if(selected !== -1) {
    if(selected !== index) {
      opacityStyle = { opacity: 0 }
    }
  }

最后一步是应用样式(空对象或不透明度:0):

<TouchableWithoutFeedback onPress={this.onPress}>
    <View style={[styles2.container, containerPos, opacityStyle]} ref="container" >

我不确定你要关闭或缩小的位置。当您关闭或缩小圆圈时,只需拨打this.props.onClick(-1)即可有效取消选择圆圈。这意味着没有其他圆圈会应用不透明度。

要确保“已选中”未被删除,您可能需要做的一件事是更改收藏夹中的setState()方法:

addCircle = () => {
this.setState(prevState => {
    return { ...prevState, circleCount: prevState.circleCount + 1 };
  }
)}

在这个版本中,我们只更改circleCount并保留prevState所具有的任何其他属性 - 在这种情况下“selected”保持不变。

相关问题