-
Notifications
You must be signed in to change notification settings - Fork 3.5k
7535 refactor picker compatible form #7807
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
Changes from 17 commits
4ef2b51
def46e5
01fa795
a5db0db
68d619f
d940295
c243e76
857ca43
5353111
dc3ba4c
37451fa
82c9cd2
addc22a
0724ae4
094efba
170fc70
ff316d8
7fb4b0f
58d70b5
cf69266
73ef24c
e9a33e2
b8158b9
2da10cf
a893f1a
57ab9f7
bc1310d
f12725a
189ca7c
4287a7a
5dcec43
eb0013d
3f0f3da
7da320a
5060819
bcc4c66
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,30 +1,72 @@ | ||
| import React from 'react'; | ||
| import RNPickerSelect from 'react-native-picker-select'; | ||
| import {View} from 'react-native'; | ||
| import _ from 'underscore'; | ||
|
|
||
| import styles from '../../../styles/styles'; | ||
| import * as basePickerPropTypes from './basePickerPropTypes'; | ||
| import basePickerStyles from './basePickerStyles'; | ||
|
|
||
| const BasePicker = props => ( | ||
| <RNPickerSelect | ||
| onValueChange={props.onChange} | ||
| items={props.items} | ||
| style={props.size === 'normal' ? basePickerStyles(props.disabled, props.hasError, props.focused) : styles.pickerSmall} | ||
| useNativeAndroidPickerStyle={false} | ||
| placeholder={props.placeholder} | ||
| value={props.value} | ||
| Icon={() => props.icon(props.size)} | ||
| disabled={props.disabled} | ||
| fixAndroidTouchableBug | ||
| onOpen={props.onOpen} | ||
| onClose={props.onClose} | ||
| pickerProps={{ | ||
| onFocus: props.onOpen, | ||
| onBlur: props.onClose, | ||
| }} | ||
| /> | ||
| ); | ||
| class BasePicker extends React.Component { | ||
luacmartins marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| constructor(props) { | ||
| super(props); | ||
|
|
||
| this.state = { | ||
| selectValue: this.props.value || this.props.defaultValue, | ||
luacmartins marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| }; | ||
| } | ||
|
|
||
| componentDidMount() { | ||
| const viewRef = this.viewRef; | ||
| if (!viewRef) { | ||
| return; | ||
| } | ||
|
|
||
| if (typeof this.props.innerRef !== 'function') { | ||
| return; | ||
| } | ||
|
|
||
| // 'ref' is not exposed in Picker to focus on it. An alternative is getting a View container ref. View ref will return a <div>, | ||
| // apply 'focus()' on it will not scroll to it. Applying 'scrollIntoView(false)' will scroll to element. https://developer.mozilla.org/es/docs/Web/API/Element/scrollIntoView | ||
| const originalFocus = viewRef.focus; | ||
| viewRef.focus = function () { | ||
| // Execute original focus() using apply on ref context to prevent infinite focus() call stack and preserve original focus() functionality | ||
| originalFocus.apply(viewRef); | ||
| viewRef.scrollIntoView(false); | ||
| }; | ||
| this.props.innerRef(viewRef); | ||
| } | ||
luacmartins marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| handleChange = (value) => { | ||
|
||
| this.props.onChange(value); | ||
| this.setState({selectValue: value}); | ||
| } | ||
|
|
||
| render() { | ||
| const hasError = !_.isEmpty(this.props.errorText); | ||
| return ( | ||
| <View ref={ref => this.viewRef = ref}> | ||
| <RNPickerSelect | ||
| onValueChange={this.handleChange} | ||
| items={this.props.items} | ||
| style={this.props.size === 'normal' ? basePickerStyles(this.props.disabled, hasError, this.props.focused) : styles.pickerSmall} | ||
| useNativeAndroidPickerStyle={false} | ||
| placeholder={this.props.placeholder} | ||
| value={this.state.selectValue} | ||
| Icon={() => this.props.icon(this.props.size)} | ||
| disabled={this.props.disabled} | ||
| fixAndroidTouchableBug | ||
| onOpen={this.props.onOpen} | ||
| onClose={this.props.onClose} | ||
| pickerProps={{ | ||
| onFocus: this.props.onOpen, | ||
| onBlur: this.props.onBlur, | ||
| }} | ||
| /> | ||
| </View> | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| BasePicker.propTypes = basePickerPropTypes.propTypes; | ||
| BasePicker.defaultProps = basePickerPropTypes.defaultProps; | ||
luacmartins marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| import React from 'react'; | ||
| import RNPickerSelect from 'react-native-picker-select'; | ||
| import _ from 'underscore'; | ||
|
|
||
| import styles from '../../../styles/styles'; | ||
| import * as basePickerPropTypes from './basePickerPropTypes'; | ||
| import basePickerStyles from './basePickerStyles'; | ||
|
|
||
| class BasePicker extends React.Component { | ||
| constructor(props) { | ||
| super(props); | ||
|
|
||
| this.state = { | ||
| selectValue: this.props.value || this.props.defaultValue, | ||
| }; | ||
| } | ||
|
|
||
| handleChange = (value) => { | ||
| this.props.onChange(value); | ||
| this.setState({selectValue: value}); | ||
| } | ||
|
|
||
| render() { | ||
| const hasError = !_.isEmpty(this.props.errorText); | ||
| return ( | ||
| <RNPickerSelect | ||
| onValueChange={this.handleChange} | ||
| items={this.props.items} | ||
| style={this.props.size === 'normal' ? basePickerStyles(this.props.disabled, hasError, this.props.focused) : styles.pickerSmall} | ||
| useNativeAndroidPickerStyle={false} | ||
| placeholder={this.props.placeholder} | ||
| value={this.state.selectValue} | ||
| Icon={() => this.props.icon(this.props.size)} | ||
| disabled={this.props.disabled} | ||
| fixAndroidTouchableBug | ||
| onOpen={this.props.onOpen} | ||
| onClose={this.props.onClose} | ||
| pickerProps={{ | ||
| onFocus: this.props.onOpen, | ||
| onBlur: this.props.onBlur, | ||
| ref: this.props.innerRef, | ||
| }} | ||
| /> | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| BasePicker.propTypes = basePickerPropTypes.propTypes; | ||
| BasePicker.defaultProps = basePickerPropTypes.defaultProps; | ||
| BasePicker.displayName = 'BasePicker'; | ||
|
|
||
| export default BasePicker; |
Uh oh!
There was an error while loading. Please reload this page.