-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13629 from Expensify/tgolen-standardize-keyboard-…
…libs Standardize keyboard libs for determining when the keyboard is open
- Loading branch information
Showing
10 changed files
with
98 additions
and
139 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,64 +1,79 @@ | ||
import React, {Component} from 'react'; | ||
/* eslint-disable react/no-unused-state */ | ||
import React, {forwardRef, createContext} from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import {Keyboard} from 'react-native'; | ||
import getComponentDisplayName from '../libs/getComponentDisplayName'; | ||
|
||
const withKeyboardStatePropTypes = { | ||
/** Returns whether keyboard is open */ | ||
isShown: PropTypes.bool.isRequired, | ||
const KeyboardStateContext = createContext(null); | ||
const keyboardStatePropTypes = { | ||
/** Whether or not the keyboard is open */ | ||
isKeyboardShown: PropTypes.bool.isRequired, | ||
}; | ||
|
||
export default function withKeyboardState(WrappedComponent) { | ||
const WithKeyboardState = class extends Component { | ||
constructor(props) { | ||
super(props); | ||
this.state = { | ||
isShown: false, | ||
}; | ||
} | ||
const keyboardStateProviderPropTypes = { | ||
/* Actual content wrapped by this component */ | ||
children: PropTypes.node.isRequired, | ||
}; | ||
|
||
class KeyboardStateProvider extends React.Component { | ||
constructor(props) { | ||
super(props); | ||
|
||
this.state = { | ||
isKeyboardShown: false, | ||
}; | ||
} | ||
|
||
componentDidMount() { | ||
this.keyboardDidShowListener = Keyboard.addListener( | ||
'keyboardDidShow', | ||
() => { | ||
this.setState({isShown: true}); | ||
}, | ||
); | ||
this.keyboardDidHideListener = Keyboard.addListener( | ||
'keyboardDidHide', | ||
() => { | ||
this.setState({isShown: false}); | ||
}, | ||
); | ||
} | ||
componentDidMount() { | ||
this.keyboardDidShowListener = Keyboard.addListener( | ||
'keyboardDidShow', | ||
() => { | ||
this.setState({isKeyboardShown: true}); | ||
}, | ||
); | ||
this.keyboardDidHideListener = Keyboard.addListener( | ||
'keyboardDidHide', | ||
() => { | ||
this.setState({isKeyboardShown: false}); | ||
}, | ||
); | ||
} | ||
|
||
componentWillUnmount() { | ||
this.keyboardDidShowListener.remove(); | ||
this.keyboardDidHideListener.remove(); | ||
} | ||
componentWillUnmount() { | ||
this.keyboardDidShowListener.remove(); | ||
this.keyboardDidHideListener.remove(); | ||
} | ||
|
||
render() { | ||
// eslint-disable-next-line react/jsx-props-no-spreading | ||
return <WrappedComponent {...this.props} isShown={this.state.isShown} />; | ||
} | ||
}; | ||
render() { | ||
return ( | ||
<KeyboardStateContext.Provider value={this.state}> | ||
{this.props.children} | ||
</KeyboardStateContext.Provider> | ||
); | ||
} | ||
} | ||
|
||
KeyboardStateProvider.propTypes = keyboardStateProviderPropTypes; | ||
|
||
WithKeyboardState.displayName = `WithKeyboardState(${getComponentDisplayName(WrappedComponent)})`; | ||
WithKeyboardState.propTypes = { | ||
forwardedRef: PropTypes.oneOfType([ | ||
PropTypes.func, | ||
PropTypes.shape({current: PropTypes.instanceOf(React.Component)}), | ||
]), | ||
}; | ||
WithKeyboardState.defaultProps = { | ||
forwardedRef: undefined, | ||
}; | ||
return React.forwardRef((props, ref) => ( | ||
// eslint-disable-next-line react/jsx-props-no-spreading | ||
<WithKeyboardState {...props} forwardedRef={ref} /> | ||
/** | ||
* @param {React.Component} WrappedComponent | ||
* @returns {React.Component} | ||
*/ | ||
export default function withKeyboardState(WrappedComponent) { | ||
const WithKeyboardState = forwardRef((props, ref) => ( | ||
<KeyboardStateContext.Consumer> | ||
{keyboardStateProps => ( | ||
// eslint-disable-next-line react/jsx-props-no-spreading | ||
<WrappedComponent {...keyboardStateProps} {...props} ref={ref} /> | ||
)} | ||
</KeyboardStateContext.Consumer> | ||
)); | ||
|
||
WithKeyboardState.displayName = `withKeyboardState(${getComponentDisplayName(WrappedComponent)})`; | ||
return WithKeyboardState; | ||
} | ||
|
||
export { | ||
withKeyboardStatePropTypes, | ||
KeyboardStateProvider, | ||
keyboardStatePropTypes, | ||
}; |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.