React-native-gifted-chat: InputToolbar height dynamically

Created on 26 Apr 2017  路  3Comments  路  Source: FaridSafi/react-native-gifted-chat

Issue Description

Im trying to use a list of button options or default InputToolBar dynamycally.

I try to render options buttons with renderInputToolbar prop, but container height is always the same.

This is my code:

renderInputToolbar(props) {
    if(this.state.responseType == "text") {
      return <InputToolbar {...props} />
    } else if(this.state.responseType == "options") {
      return <AnswerMultipleOptions options={[1,2,3]} onPress={(opt) => console.log("opt: ", opt)} />
    }
  }

  render() {
    return (
        <View style={{flex: 1, backgroundColor: 'red'}}>
          <GiftedChat
            messages={this.state.messages}
            loadEarlier={false}
            onSend={this.onSend.bind(this)}
            renderInputToolbar={this.renderInputToolbar.bind(this)}

            user={{
              _id: 1,
            }}
          />
        </View>
    )
  }

Capture:
captura de pantalla 2017-04-26 a las 12 58 51

Somebody know how can i change the renderInputToolbar height dynamically?

Thanks

Most helpful comment

Finally i solved it with:

  calculateMinInputToolbarHeight(layout) {
    if(layout){
      this.setState({minInputToolbarHeight: layout.height})
    }
  }

and:

  componentDidUpdate(prevProps, prevState) {
    if(prevState.minInputToolbarHeight != this.state.minInputToolbarHeight){
      this.refs.chat.resetInputToolbar()
    }
  }

because the method setMinInputToolbarHeight is "to do"

All 3 comments

Ok, now i calculate the height of my AnswerMultipleOptions component and set to minInputToolbarHeight prop, it works perfect when show options, but when i come back to show defaultText input and change renderInputToolbar to default height, it doesnt work until i click on inputText.

Captures:
captura de pantalla 2017-04-26 a las 13 25 57

Here minInputToolbarHeight is 44.5, but dont render the changes
captura de pantalla 2017-04-26 a las 13 26 04

And when i click on textInput:
captura de pantalla 2017-04-26 a las 13 26 10

And code now:

  constructor(props) {
    super(props)
    this.state = {
      messages: [],
      responseType: 'text',
      minInputToolbarHeight: 44.5,
    }
  }

onSend(messages = []) {
    this.setState({responseType: 'options'})
  }

  renderInputToolbar(props) {
    if(this.state.responseType == "text") {
      return (
        <View onLayout={(e) => { this.calculateMinInputToolbarHeight(e.nativeEvent.layout) }}>
          <InputToolbar {...props} />
        </View>
      )
    } else if(this.state.responseType == "options") {
      return (
        <View onLayout={(e) => { this.calculateMinInputToolbarHeight(e.nativeEvent.layout) }}>
          <AnswerMultipleOptions options={[1,2,3]} onPress={(opt) => this.setState({responseType: 'text'})} />
        </View>
      )
    }
  }

  calculateMinInputToolbarHeight(layout) {
    if(layout && layout.height){
      this.setState({minInputToolbarHeight: layout.height})
    }
  }

  render() {
    console.log("this.state.minInputToolbarHeight: ", this.state.minInputToolbarHeight)
    return (
      <View style={{flex: 1, backgroundColor: 'red'}}>
          <GiftedChat
            messages={this.state.messages}
            loadEarlier={false}
            onSend={this.onSend.bind(this)}
            renderInputToolbar={this.renderInputToolbar.bind(this)}
            minInputToolbarHeight={this.state.minInputToolbarHeight}
            user={{
              _id: 1,
            }}
          />
      </View>
    )
  }

Im trying to add ref to chat (ref="chat") and when my renderInputToolbarheight change i call:
this.refs.chat.onInputSizeChanged(layout)

But it doesnt work xd Maybe some of this.refs.chat functions can help with this. @FaridSafi you know some about this?

Finally i solved it with:

  calculateMinInputToolbarHeight(layout) {
    if(layout){
      this.setState({minInputToolbarHeight: layout.height})
    }
  }

and:

  componentDidUpdate(prevProps, prevState) {
    if(prevState.minInputToolbarHeight != this.state.minInputToolbarHeight){
      this.refs.chat.resetInputToolbar()
    }
  }

because the method setMinInputToolbarHeight is "to do"

Was this page helpful?
0 / 5 - 0 ratings

Related issues

cerberusv2px picture cerberusv2px  路  3Comments

redwind picture redwind  路  3Comments

cassioseffrin picture cassioseffrin  路  3Comments

pentarex picture pentarex  路  3Comments

Hayko1994 picture Hayko1994  路  3Comments