制作动画可折叠卡组件,显示或隐藏初始道具

时间:2017-08-01 03:03:31

标签: reactjs react-native react-animated

背景

使用React Native我能够制作可折叠的卡组件。在图标上单击该卡会向上滑动以隐藏其内容,或展开显示其内容。我认为设置默认值就像设置扩展为false或true一样简单,但我认为这里的问题是当切换时会触发动画,这会改变卡的高度。

示例

class CardCollapsible extends Component{
  constructor(props){
    super(props);

    this.state = {
      title: props.title,
      expanded: true,
      animation: new Animated.Value(),
      iconExpand: "keyboard-arrow-down",
    };
  }

  _setMaxHeight(event){
      this.setState({
          maxHeight   : event.nativeEvent.layout.height
      });
  }

  _setMinHeight(event){
      this.setState({
          minHeight   : event.nativeEvent.layout.height
      });

      this.toggle = this.toggle.bind(this);
  }

  toggle(){
    let initialValue    = this.state.expanded? this.state.maxHeight + this.state.minHeight : this.state.minHeight,
        finalValue      = this.state.expanded? this.state.minHeight : this.state.maxHeight + this.state.minHeight;

    this.setState({
      expanded : !this.state.expanded
    });

    if (this.state.iconExpand === "keyboard-arrow-up") {
      this.setState({
        iconExpand : "keyboard-arrow-down"
      })
    } else {
      this.setState({
        iconExpand : "keyboard-arrow-up"
      })
    }
    this.state.animation.setValue(initialValue);
    Animated.spring( this.state.animation, {
        toValue: finalValue
      }
    ).start();
  }

  render(){

    return (
      <Animated.View style={[styles.container,{height: this.state.animation}]}>
          <View style={styles.titleContainer} onLayout={this._setMinHeight.bind(this)}>
            <CardTitle>{this.state.title}</CardTitle>
            <TouchableHighlight
              style={styles.button}
              onPress={this.toggle}
              underlayColor="#f1f1f1">
              <Icon
                name={this.state.iconExpand}
                style={{ fontSize: 30 }}/>
            </TouchableHighlight>
          </View>
          <Separator />
          <View style={styles.card} onLayout={this._setMaxHeight.bind(this)}>
            {this.props.children}
          </View>
      </Animated.View>
    );
  }
}

var styles = StyleSheet.create({
  container: {
    backgroundColor: '#fff',
    margin:10,
    overflow:'hidden'
    },
  titleContainer: {
    flexDirection: 'row'
    },
  card: {
    padding: 10
  }
});

export { CardCollapsible };

打开

enter image description here

enter image description here

问题

我的目标是允许调用组件的人将组件的初始状态设置为展开或打开。但是当我尝试将展开状态更改为false时,它不会呈现关闭状态。

我如何允许用户调用组件来选择在初始组件渲染时是扩展还是关闭?

5 个答案:

答案 0 :(得分:4)

为您制作全新的产品。简单而且工作正常。

注意:此组件不需要state。状态越少,表现越好。

也许你可以修改自己的风格=)

class Card extends Component {
    anime = {
        height: new Animated.Value(),
        expanded: false,
        contentHeight: 0,
    }

    constructor(props) {
        super(props);

        this._initContentHeight = this._initContentHeight.bind(this);
        this.toggle = this.toggle.bind(this);

        this.anime.expanded = props.expanded;
    }

    _initContentHeight(evt) {
        if (this.anime.contentHeight>0) return;
        this.anime.contentHeight = evt.nativeEvent.layout.height;
        this.anime.height.setValue(this.anime.expanded ? this._getMaxValue() : this._getMinValue() );
    }

    _getMaxValue() { return this.anime.contentHeight };
    _getMinValue() { return 0 };

    toggle() {
        Animated.timing(this.anime.height, {
            toValue: this.anime.expanded ? this._getMinValue() : this._getMaxValue(),
            duration: 300,
        }).start();
        this.anime.expanded = !this.anime.expanded;
    }

    render() {
        return (
            <View style={styles.titleContainer}>
                <View style={styles.title}>
                    <TouchableHighlight underlayColor="transparent" onPress={this.toggle}>
                        <Text>{this.props.title}</Text>
                    </TouchableHighlight>
                </View>

                <Animated.View style={[styles.content, { height: this.anime.height }]} onLayout={this._initContentHeight}>
                    {this.props.children}
                </Animated.View>
            </View>
        );
    }
}

用法:

<Card title='Customized Card 1' expanded={false}>
    <Text>Hello, this is first line.</Text>
    <Text>Hello, this is second line.</Text>
    <Text>Hello, this is third line.</Text>
</Card>

视觉效果:(只有第二张卡片以expanded={true}开头,其他卡片以expanded={false}开头)

enter image description here

答案 1 :(得分:0)

扩展值'true / false'应该从调用组件传递,您需要使用此值来展开/折叠组件。我看不到你在渲染方法中使用了'扩展'。你应该这样做:

 { this.props.expanded && <View style={styles.card} onLayout={this._setMaxHeight.bind(this)}>
            {this.props.children}
          </View> }

答案 2 :(得分:0)

我按照一些博客上的步骤然后我有相同的条件。所以我在这里的其他人的线索后做了一些改变。我的帖子也许是迟到了。但我希望能帮到别人。

import React from 'react';
import {
  Text,
  View,
  TouchableOpacity,
  Image,
  Animated,
  StyleSheet
} from 'react-native'
import styles from './styles' //put styles from another file

class PanelExpanding extends Component{
  constructor(props) {
    super(props)
    this.state = {
      expanded: false, //step 1 
      animation: new Animated.Value()
    }

    this.icons = {
      'up': require('../../..assets/images/up.png'),
      'down': require('../../../assets/images/down.png')
    }
  }

  toggle(){
    let finalValue    = this.state.expanded? this.state.maxHeight + this.state.minHeight : this.state.minHeight,
        initialValue  = this.state.expanded? this.state.minHeight : this.state.maxHeight + this.state.minHeight;
    //step 2, this needed, if we use !this.state.expanded i don't know how it wont changes at first
    if(this.state.expanded === true){
      return this.setState({ expanded : false })
    } else {
      return this.setState({ expanded : true })
    }
    this.state.animation.setValue(initialValue);
    Animated.spring(
        this.state.animation,
        { toValue: finalValue }
    ).start();
  }


  _setMinHeight(event){
    this.setState({
        minHeight : event.nativeEvent.layout.height
    });
  }

  _setMaxHeight(event){
    this.setState({
        maxHeight : event.nativeEvent.layout.height
    });
  }


  render(){
    let icon = this.icons['down'],
        textSwap = 'SHOWMORE'

    if(this.state.expanded){
      icon = this.icons['up'],
      textSwap = 'SHOWLESS'
    }

    return (
      <Animated.View
            style={[styles.containerPanel,{height: this.state.animation}]} >
        <View>
          <View style={styles.titleContainer} onLayout={this._setMinHeight.bind(this)}>
              <TouchableOpacity
                  style={styles.button}
                  onPress={this.toggle.bind(this)} >
                  <Text style={styles.textShow}>{textSwap}</Text>
                  <Image
                      style={styles.buttonImage}
                      source={icon} />
              </TouchableOpacity>
          </View>

          //step 3, add this to initial when first render is false
          {this.state.expanded && <View style={styles.body} onLayout={this._setMaxHeight.bind(this)}>
            <Text>
              {this.props.children}
            </Text>
          </View>}
        </View>
      </Animated.View>
    )
  }
}

export default PanelExpanding

答案 3 :(得分:0)

IdentityDbContext

答案 4 :(得分:0)

使用react-native-collapsible-view。 您可以传递它的expanded属性,使您可以完全控制其他组件的可折叠状态。
您还可以让他自控,但可以通过initExpanded来让他选择可折叠物体的初始状态。