Skip to content

Commit

Permalink
Merge pull request #15673 from redstar504/Brayden-PickerIndicator
Browse files Browse the repository at this point in the history
Add additional events for controlling picker indicator on web
  • Loading branch information
marcaaron authored Mar 15, 2023
2 parents ad1e244 + f956e52 commit 38defc6
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 13 deletions.
51 changes: 38 additions & 13 deletions src/components/Picker.js → src/components/Picker/Picker.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ import React, {PureComponent} from 'react';
import {View} from 'react-native';
import PropTypes from 'prop-types';
import RNPickerSelect from 'react-native-picker-select';
import Icon from './Icon';
import * as Expensicons from './Icon/Expensicons';
import FormHelpMessage from './FormHelpMessage';
import Text from './Text';
import styles from '../styles/styles';
import themeColors from '../styles/themes/default';
import {ScrollContext} from './ScrollViewWithContext';
import Icon from '../Icon';
import * as Expensicons from '../Icon/Expensicons';
import FormHelpMessage from '../FormHelpMessage';
import Text from '../Text';
import styles from '../../styles/styles';
import themeColors from '../../styles/themes/default';
import {ScrollContext} from '../ScrollViewWithContext';

const propTypes = {
/** Picker label */
Expand Down Expand Up @@ -75,6 +75,9 @@ const propTypes = {
current: PropTypes.element,
}),
]),

/** Additional events passed to the core Picker for specific platforms such as web */
additionalPickerEvents: PropTypes.func,
};

const defaultProps = {
Expand All @@ -97,16 +100,19 @@ const defaultProps = {
),
onBlur: () => {},
innerRef: () => {},
additionalPickerEvents: () => {},
};

class Picker extends PureComponent {
constructor(props) {
super(props);
this.state = {
isOpen: false,
isHighlighted: false,
};

this.onInputChange = this.onInputChange.bind(this);
this.enableHighlight = this.enableHighlight.bind(this);
this.disableHighlight = this.disableHighlight.bind(this);

// Windows will reuse the text color of the select for each one of the options
// so we might need to color accordingly so it doesn't blend with the background.
Expand Down Expand Up @@ -151,6 +157,18 @@ class Picker extends PureComponent {
this.props.onInputChange(this.props.items[0].value, 0);
}

enableHighlight() {
this.setState({
isHighlighted: true,
});
}

disableHighlight() {
this.setState({
isHighlighted: false,
});
}

render() {
const hasError = !_.isEmpty(this.props.errorText);

Expand All @@ -161,7 +179,7 @@ class Picker extends PureComponent {
styles.pickerContainer,
this.props.isDisabled && styles.inputDisabled,
...this.props.containerStyles,
this.state.isOpen && styles.borderColorFocus,
this.state.isHighlighted && styles.borderColorFocus,
hasError && styles.borderColorDanger,
]}
>
Expand All @@ -184,15 +202,22 @@ class Picker extends PureComponent {
Icon={() => this.props.icon(this.props.size)}
disabled={this.props.isDisabled}
fixAndroidTouchableBug
onOpen={() => this.setState({isOpen: true})}
onClose={() => this.setState({isOpen: false})}
onOpen={this.enableHighlight}
onClose={this.disableHighlight}
textInputProps={{allowFontScaling: false}}
pickerProps={{
onFocus: () => this.setState({isOpen: true}),
onFocus: this.enableHighlight,
onBlur: () => {
this.setState({isOpen: false});
this.disableHighlight();
this.props.onBlur();
},
...this.props.additionalPickerEvents(
this.enableHighlight,
(value, index) => {
this.onInputChange(value, index);
this.disableHighlight();
},
),
}}
ref={(el) => {
if (!_.isFunction(this.props.innerRef)) {
Expand Down
19 changes: 19 additions & 0 deletions src/components/Picker/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import React, {forwardRef} from 'react';
import BasePicker from './Picker';

const additionalPickerEvents = (onMouseDown, onChange) => ({
onMouseDown,
onChange: (e) => {
if (e.target.selectedIndex === undefined) {
return;
}
const index = e.target.selectedIndex;
const value = e.target.options[index].value;
onChange(value, index);
},
});

export default forwardRef((props, ref) => (
// eslint-disable-next-line react/jsx-props-no-spreading
<BasePicker {...props} additionalPickerEvents={additionalPickerEvents} ref={ref} />
));
7 changes: 7 additions & 0 deletions src/components/Picker/index.native.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import React, {forwardRef} from 'react';
import BasePicker from './Picker';

export default forwardRef((props, ref) => (
// eslint-disable-next-line react/jsx-props-no-spreading
<BasePicker {...props} ref={ref} />
));

0 comments on commit 38defc6

Please sign in to comment.