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

Fix cursor position when initial value is set #20

Merged
merged 1 commit into from
Mar 21, 2019
Merged

Fix cursor position when initial value is set #20

merged 1 commit into from
Mar 21, 2019

Conversation

thomas-lebeau
Copy link
Contributor

When setting an initial value, the cursor is still at the beginning of the line.

This PR sets the initial cursor offset to the value length, so the cursor is at the end of the input, ready to continue typing.

src/index.js Outdated
@@ -34,7 +34,7 @@ class TextInput extends PureComponent {
};

state = {
cursorOffset: 0
cursorOffset: this.props.value.length
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a value: '' to defaultProps, so that it doesn't crash when this.props.value is missing?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

value is declared as required per the propTypes, setting a default value makes the linter complain.

I could change it so that it's not required, but I preferred to use a ternary operator to initialize cursorOffset.

Though, if you think it should be done differently, I'd be happy to change it.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. In that case I'd use (this.props.value || '').length instead.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 updated!
thanks

@vadimdemedes vadimdemedes changed the title fix initial cursor position Fix initial cursor position Mar 18, 2019
@vadimdemedes vadimdemedes changed the title Fix initial cursor position Fix cursor position when initial value is set Mar 18, 2019
@vadimdemedes
Copy link
Owner

Great fix, thanks!

@vadimdemedes vadimdemedes merged commit 720f479 into vadimdemedes:master Mar 21, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants