Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OnContentSizeChange for TextInput doesn't update width #19696

Closed
5 tasks done
Craigson opened this issue Jun 13, 2018 · 3 comments
Closed
5 tasks done

OnContentSizeChange for TextInput doesn't update width #19696

Craigson opened this issue Jun 13, 2018 · 3 comments
Labels
Component: TextInput Related to the TextInput component. Stale There has been a lack of activity on this issue and it may be closed soon.

Comments

@Craigson
Copy link

Environment

Environment:
  OS: macOS High Sierra 10.13.2
  Node: 10.3.0
  Yarn: Not Found
  npm: 6.1.0
  Watchman: 4.9.0
  Xcode: Xcode 9.2 Build version 9C40b
  Android Studio: 3.0 AI-171.4443003

Packages: (wanted => installed)
  react: 16.3.1 => 16.3.1
  react-native: 0.55.4 => 0.55.4

Description

I'm trying to create a laterally expanding text field - I have tried simply not setting the width of the TextInput component and let it auto expand, but it visually clips the text.

When using onContentSizeChange property of the TextInput component, the event.nativeEvent.contentSize.width does not update correctly. In fact, it only fires twice after input of first two characters then doesn't fire again.

This issue has been raised before, though only with height. Solutions on StackOverflow say to instead use onChange instead, but the event has no contentSize property that I can use.

Steps to Reproduce

class ExpandingInput extends Component {

  constructor(props) {
    super(props)

    this.state = {
      width: 50
    }

  }

  render() {

    console.log(this.state.width)
    let inputStyle = {
        color: '#fff',
        width: this.state.width,
        fontSize: 60,
        lineHeight: 60,
        fontFamily: 'GT-Walsheim-Bold',
        flexWrap: 'wrap'
    }

      return (
        <View style={styles.containerStyle}>
          <Text style={styles.labelStyle}>{this.props.label}</Text>
          <TextInput
            secureTextEntry={this.props.secureTextEntry}
            placeholder={this.props.placeholder}
            autoCorrect={false}
            style={inputStyle}
            value={this.props.value}
            onChangeText={this.props.onChangeText}
            autoFocus={true}
            autoCapitalize={this.props.capitalize}
            onContentSizeChange={ e=> {
              console.log(e.nativeEvent.contentSize.width)
              this.setState({
                width: e.nativeEvent.contentSize.width
              })
            }}
          />
        </View>
      );
  }
}

Expected Behavior

With the addition of every character, the onContentSizeChange handler should be called and the event passed as an argument to the callback and the e.nativeEvent.contentSize.width should update and represent the width of the TextInput's text (value).

Actual Behavior

Callback fires only twice and returns a width of 524296, presumably because it's never correctly initialized

{height: 101, width: 524296}
Input.js:24 524296
Input.js:24 524296
Input.js:24 524296
Input.js:47 {height: 81, width: 524296}
@react-native-bot react-native-bot added the Component: TextInput Related to the TextInput component. label Jun 13, 2018
@alexandru1208
Copy link

I have the exact same problem, and it is just on Android, on iOS works fine

@stale
Copy link

stale bot commented Dec 21, 2018

Hey there, it looks like there has been no activity on this issue recently. Has the issue been fixed, or does it still require the community's attention? This issue may be closed if no further activity occurs. You may also label this issue as "For Discussion" or "Good first issue" and I will leave it open. Thank you for your contributions.

@stale stale bot added the Stale There has been a lack of activity on this issue and it may be closed soon. label Dec 21, 2018
@stale
Copy link

stale bot commented Dec 28, 2018

Closing this issue after a prolonged period of inactivity. If this issue is still present in the latest release, please feel free to create a new issue with up-to-date information.

@stale stale bot closed this as completed Dec 28, 2018
@facebook facebook locked as resolved and limited conversation to collaborators Dec 29, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Component: TextInput Related to the TextInput component. Stale There has been a lack of activity on this issue and it may be closed soon.
Projects
None yet
Development

No branches or pull requests

4 participants