Skip to content

Commit

Permalink
Merge pull request #42 from ML-Leonardo/fix/BCP_1980498907-focus-issue
Browse files Browse the repository at this point in the history
Set the focus to the input field when the Chat window is opened.
  • Loading branch information
JWandrocke authored and GitHub Enterprise committed Apr 6, 2020
2 parents 6bdb1ab + 6662171 commit de25be7
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/components/Input/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,18 @@ class Input extends Component {
historyValues: [],
indexHistory: 0,
menuOpened: false,
isOpen: false,
hasFocus: false,
menuIndexes: [],
}

static getDerivedStateFromProps (props, state) {
if (!props.isOpen) {
return { isOpen: props.isOpen, hasFocus: false }
}
return { isOpen: props.isOpen }
}

componentDidMount () {
this._input.focus()
this._input.value = ''
Expand All @@ -33,10 +42,14 @@ class Input extends Component {
nextState.value !== this.state.value
|| nextState.menuOpened !== this.state.menuOpened
|| nextState.menuIndexes.length !== this.state.menuIndexes.length
|| nextState.isOpen !== this.state.isOpen
)
}

componentDidUpdate () {
if (this.state.isOpen && !this.state.hasFocus) {
this.setFocusState()
}
if (!this.state.value) {
// Dirty fix textarea placeholder to reset style correctly
setTimeout(() => {
Expand All @@ -49,6 +62,13 @@ class Input extends Component {
this.onInputHeight()
}

setFocusState () {
setTimeout(() => {
this._input.focus()
this.setState({ hasFocus: true })
}, 100)
}

onInputChange = e => {
e.persist()

Expand Down Expand Up @@ -233,6 +253,7 @@ class Input extends Component {
}

Input.propTypes = {
isOpen: PropTypes.bool,
menu: PropTypes.object,
onSubmit: PropTypes.func,
onInputHeight: PropTypes.func,
Expand Down
1 change: 1 addition & 0 deletions src/containers/Chat/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,7 @@ class Chat extends Component {
</div>
{ !readOnlyMode && <Input
menu={preferences.menu && preferences.menu.menu}
isOpen={show}
onSubmit={this.sendMessage}
preferences={preferences}
onInputHeight={height => this.setState({ inputHeight: height })}
Expand Down

0 comments on commit de25be7

Please sign in to comment.