Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable TouchableHighlight onPress?

How to disable groups of TouchableHighlight if one of them pressed ? Is it possible ? For example I have listview and each row has two TouchableHighlights. If one of them pressed, I want to disable onPress function of these two touchables.

like image 311
Melih Mucuk Avatar asked Nov 27 '22 05:11

Melih Mucuk


1 Answers

I solved this. Here is how:

First, thanks to James Ide for awesome button component with disabled props.

I wrote custom component --main.js-- for button group. And use disabled props for disable button group.

screenshot and logs

index.ios.js:

class PNTest extends React.Component{
  constructor(props){
    super(props)

    this.state ={
      clicked: []
    }
  }

  render(){
    return(
      <View style={styles.container}>
        <Main handleClick={this._handleClick.bind(this)} left={1} right={2} name={'group1'}/>
        <Main handleClick={this._handleClick.bind(this)} left={3} right={4} name={'group2'}/>
        <Text style={styles.welcome}>
          {this.state.clicked}
        </Text>
      </View>
    );
  }

  _handleClick(id, group){
    this.state.clicked.push(id);
    console.log(group + ' now inactive and clicked button id: ' + id);
    console.log('clicked button ids: ' + this.state.clicked);
  }
}

main.js:

class Main extends React.Component{
  constructor(props){
    super(props)

    this.state = {
      disabled: false,
      left: {},
      right: {}
    }
  }

  render(){
    return(
      <View style={styles.container}>
        <Button
          disabled={this.state.disabled}
          style={[styles.buttonContainer, this.state.left]}
          onPress={() => this._onPress(this.props.left)}>
          Left
        </Button>
        <Button
          disabled={this.state.disabled}
          style={[styles.buttonContainer,this.state.right]}
          onPress={() => this._onPress(this.props.right)}>
          Right
        </Button>
      </View>
    );
  }

  _onPress(id){
    var left = {};
    var right = {};

    if(id === this.props.left){
      left = styles.buttonSelected;
      right = styles.buttonNotSelected;
    }else if(id === this.props.right){
      right = styles.buttonSelected;
      left = styles.buttonNotSelected;
    }

    this.setState({
      disabled: true,
      left: left,
      right: right
    });

    this.props.handleClick(id, this.props.name);
  }
}
like image 197
Melih Mucuk Avatar answered Nov 29 '22 05:11

Melih Mucuk