-
Notifications
You must be signed in to change notification settings - Fork 3.3k
17548 Personal details push to page #21049
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 15 commits
6c2c140
c9c320a
e746928
062bc8a
b603569
56924ba
474125f
74cc95a
f01b1e0
f850f23
a2cd0b6
e48c601
8148f4f
c2e715e
7ef53f1
aee62fa
b13ac10
8898224
fe46e3c
64b8dbb
761c75a
d641006
e06bc96
b94e99f
4ee01d6
2cead61
675a7c3
0779f34
d063f55
128597b
94bd35a
f750d18
0e416e0
5c0ef29
60c9fbb
b34b031
4852ccb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
mountiny marked this conversation as resolved.
Show resolved
Hide resolved
gedu marked this conversation as resolved.
Show resolved
Hide resolved
|
mountiny marked this conversation as resolved.
Show resolved
Hide resolved
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,67 +1,91 @@ | ||
import _ from 'underscore'; | ||
import React, {forwardRef} from 'react'; | ||
import React, {useCallback, useEffect} from 'react'; | ||
import {View} from 'react-native'; | ||
import PropTypes from 'prop-types'; | ||
import Picker from './Picker'; | ||
import withLocalize, {withLocalizePropTypes} from './withLocalize'; | ||
import {useFocusEffect} from '@react-navigation/native'; | ||
import styles from '../styles/styles'; | ||
import MenuItemWithTopDescription from './MenuItemWithTopDescription'; | ||
import * as PersonalDetails from '../libs/actions/PersonalDetails'; | ||
import useNavigationStorage from '../hooks/useNavigationStorage'; | ||
import Navigation from '../libs/Navigation/Navigation'; | ||
import useLocalize from '../hooks/useLocalize'; | ||
import ROUTES from '../ROUTES'; | ||
import FormHelpMessage from './FormHelpMessage'; | ||
|
||
const propTypes = { | ||
/** The label for the field */ | ||
label: PropTypes.string, | ||
/** The ISO code of the country */ | ||
countryISO: PropTypes.string, | ||
|
||
/** A callback method that is called when the value changes and it receives the selected value as an argument. */ | ||
onInputChange: PropTypes.func.isRequired, | ||
|
||
/** The value that needs to be selected */ | ||
value: PropTypes.oneOfType([PropTypes.number, PropTypes.string]), | ||
|
||
/** The ID used to uniquely identify the input in a form */ | ||
inputID: PropTypes.string, | ||
|
||
/** Saves a draft of the input value when used in a form */ | ||
shouldSaveDraft: PropTypes.bool, | ||
/** Form Error description */ | ||
errorText: PropTypes.string, | ||
|
||
/** Callback that is called when the text input is blurred */ | ||
onBlur: PropTypes.func, | ||
/** Country to display */ | ||
// eslint-disable-next-line react/require-default-props | ||
value: PropTypes.string, | ||
|
||
/** Error text to display */ | ||
errorText: PropTypes.string, | ||
/** ID of the input */ | ||
inputID: PropTypes.string.isRequired, | ||
|
||
...withLocalizePropTypes, | ||
/** Callback to call when the input changes */ | ||
onInputChange: PropTypes.func, | ||
}; | ||
|
||
const defaultProps = { | ||
label: '', | ||
value: undefined, | ||
countryISO: '', | ||
errorText: '', | ||
shouldSaveDraft: false, | ||
inputID: undefined, | ||
onBlur: () => {}, | ||
onInputChange: () => {}, | ||
}; | ||
|
||
const CountryPicker = forwardRef((props, ref) => { | ||
const COUNTRIES = _.map(props.translate('allCountries'), (countryName, countryISO) => ({ | ||
value: countryISO, | ||
label: countryName, | ||
})); | ||
const CountryPicker = React.forwardRef(({value, countryISO, inputID, errorText, onInputChange}, ref) => { | ||
const {translate} = useLocalize(); | ||
const countryValue = value || countryISO || ''; | ||
const [collect, save] = useNavigationStorage(inputID, countryValue); | ||
|
||
useEffect(() => { | ||
const savedCountry = collect(); | ||
if (!countryValue || savedCountry === countryValue) { | ||
return; | ||
} | ||
save(countryValue); | ||
}, [countryValue, collect, save]); | ||
|
||
useFocusEffect( | ||
useCallback(() => { | ||
const savedCountry = collect(); | ||
if (savedCountry && savedCountry !== countryValue) { | ||
save(savedCountry); | ||
// Needed to call onInputChange, so Form can update the validation and values | ||
onInputChange(savedCountry); | ||
} | ||
// onInputChange isn't a stable function, so we can't add it to the dependency array | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, [collect, countryValue]), | ||
); | ||
|
||
const navigateToCountrySelector = () => { | ||
Navigation.navigate(ROUTES.getCountrySelectionRoute(inputID, Navigation.getActiveRoute())); | ||
}; | ||
|
||
const title = PersonalDetails.getCountryName(countryValue); | ||
const descStyle = title.length === 0 ? styles.addressPickerDescription : null; | ||
return ( | ||
<Picker | ||
ref={ref} | ||
inputID={props.inputID} | ||
placeholder={{value: '', label: '-'}} | ||
items={COUNTRIES} | ||
onInputChange={props.onInputChange} | ||
value={props.value} | ||
label={props.label || props.translate('common.country')} | ||
errorText={props.errorText} | ||
onBlur={props.onBlur} | ||
shouldSaveDraft={props.shouldSaveDraft} | ||
/> | ||
<View> | ||
<MenuItemWithTopDescription | ||
ref={ref} | ||
shouldShowRightIcon | ||
title={title} | ||
descriptionTextStyle={descStyle} | ||
description={translate('common.country')} | ||
onPress={navigateToCountrySelector} | ||
/> | ||
<View style={styles.ml5}> | ||
<FormHelpMessage message={errorText} /> | ||
</View> | ||
</View> | ||
); | ||
}); | ||
|
||
CountryPicker.propTypes = propTypes; | ||
CountryPicker.defaultProps = defaultProps; | ||
CountryPicker.displayName = 'CountryPicker'; | ||
|
||
export default withLocalize(CountryPicker); | ||
export default CountryPicker; |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -6,16 +6,15 @@ const propTypes = { | |||||
...menuItemPropTypes, | ||||||
}; | ||||||
|
||||||
function MenuItemWithTopDescription(props) { | ||||||
return ( | ||||||
<MenuItem | ||||||
// eslint-disable-next-line react/jsx-props-no-spreading | ||||||
{...props} | ||||||
shouldShowBasicTitle | ||||||
shouldShowDescriptionOnTop | ||||||
/> | ||||||
); | ||||||
} | ||||||
const MenuItemWithTopDescription = React.forwardRef((props, ref) => ( | ||||||
<MenuItem | ||||||
// eslint-disable-next-line react/jsx-props-no-spreading | ||||||
{...props} | ||||||
ref={ref} | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we really need to forward the ref here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It was needed for the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess you are right. Form relies on the ref function to be called to set its internal refs. Lines 277 to 278 in 751cf85
In that case can you please re-write the function back using the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I checked and it doesn't show the error labels for example. I think It would be better to handle in a separate PR cc: @mountiny There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. definitely better in a separate PR There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @gedu I'm sorry did you mean that if we write this in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if before was a function, it wasn't working, that's why I need to add the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What I meant is to write this as: function MenuItemWithTopDescription {...}
export default React.forwardRef(...); Just for consistency, I'm aware that ref is still needed. |
||||||
shouldShowBasicTitle | ||||||
shouldShowDescriptionOnTop | ||||||
/> | ||||||
)); | ||||||
|
||||||
MenuItemWithTopDescription.propTypes = propTypes; | ||||||
MenuItemWithTopDescription.displayName = 'MenuItemWithTopDescription'; | ||||||
|
mountiny marked this conversation as resolved.
Show resolved
Hide resolved
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,67 +1,102 @@ | ||
import _ from 'underscore'; | ||
import React, {forwardRef} from 'react'; | ||
import React, {useCallback, useMemo, useEffect} from 'react'; | ||
import {View} from 'react-native'; | ||
import {useRoute, useFocusEffect} from '@react-navigation/native'; | ||
import PropTypes from 'prop-types'; | ||
import Picker from './Picker'; | ||
import withLocalize, {withLocalizePropTypes} from './withLocalize'; | ||
import useNavigationStorage from '../hooks/useNavigationStorage'; | ||
import styles from '../styles/styles'; | ||
import MenuItemWithTopDescription from './MenuItemWithTopDescription'; | ||
import useLocalize from '../hooks/useLocalize'; | ||
import Navigation from '../libs/Navigation/Navigation'; | ||
import ROUTES from '../ROUTES'; | ||
import FormHelpMessage from './FormHelpMessage'; | ||
|
||
const propTypes = { | ||
/** The label for the field */ | ||
label: PropTypes.string, | ||
|
||
/** A callback method that is called when the value changes and it receives the selected value as an argument. */ | ||
onInputChange: PropTypes.func.isRequired, | ||
|
||
/** The value that needs to be selected */ | ||
value: PropTypes.oneOfType([PropTypes.number, PropTypes.string]), | ||
|
||
/** The ID used to uniquely identify the input in a Form */ | ||
inputID: PropTypes.string, | ||
/** Error text to display */ | ||
errorText: PropTypes.string, | ||
|
||
/** Saves a draft of the input value when used in a form */ | ||
shouldSaveDraft: PropTypes.bool, | ||
/** Default value to display */ | ||
defaultValue: PropTypes.string, | ||
|
||
/** Callback that is called when the text input is blurred */ | ||
onBlur: PropTypes.func, | ||
/** State to display */ | ||
// eslint-disable-next-line react/require-default-props | ||
value: PropTypes.string, | ||
|
||
/** Error text to display */ | ||
errorText: PropTypes.string, | ||
/** ID of the input */ | ||
inputID: PropTypes.string.isRequired, | ||
|
||
...withLocalizePropTypes, | ||
/** Callback to call when the input changes */ | ||
onInputChange: PropTypes.func, | ||
}; | ||
|
||
const defaultProps = { | ||
label: '', | ||
value: undefined, | ||
errorText: '', | ||
shouldSaveDraft: false, | ||
inputID: undefined, | ||
onBlur: () => {}, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Coming from #30195 , |
||
defaultValue: '', | ||
onInputChange: () => {}, | ||
}; | ||
|
||
const StatePicker = forwardRef((props, ref) => { | ||
const STATES = _.chain(props.translate('allStates')) | ||
.sortBy((state) => state.stateName.toLowerCase()) | ||
.map((state) => ({value: state.stateISO, label: state.stateName})) | ||
.value(); | ||
const StatePicker = React.forwardRef(({value, defaultValue, inputID, errorText, onInputChange}, ref) => { | ||
const route = useRoute(); | ||
mountiny marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const {translate} = useLocalize(); | ||
const formValue = value || defaultValue || ''; | ||
const [collect, save] = useNavigationStorage(inputID, formValue); | ||
|
||
useEffect(() => { | ||
const collectedState = collect(); | ||
if (!formValue || collectedState === formValue) { | ||
return; | ||
} | ||
save(formValue); | ||
}, [formValue, collect, save]); | ||
|
||
useFocusEffect( | ||
useCallback(() => { | ||
const collectedState = collect(); | ||
if (collectedState && collectedState !== formValue) { | ||
save(collectedState); | ||
// Needed to call onInputChange, so Form can update the validation and values | ||
onInputChange(collectedState); | ||
} | ||
// onInputChange isn't a stable function, so we can't add it to the dependency array | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, [collect, formValue]), | ||
); | ||
|
||
const navigateToCountrySelector = () => { | ||
// Try first using the route.path so I can keep any query params | ||
Navigation.navigate(ROUTES.getUsaStateSelectionRoute(inputID, route.path || Navigation.getActiveRoute())); | ||
}; | ||
|
||
const title = useMemo(() => { | ||
const allStates = translate('allStates'); | ||
|
||
if (allStates[formValue]) { | ||
return allStates[formValue].stateName; | ||
} | ||
|
||
return ''; | ||
}, [translate, formValue]); | ||
|
||
const descStyle = title.length === 0 ? styles.addressPickerDescription : null; | ||
|
||
return ( | ||
<Picker | ||
ref={ref} | ||
inputID={props.inputID} | ||
placeholder={{value: '', label: '-'}} | ||
items={STATES} | ||
onInputChange={props.onInputChange} | ||
value={props.value} | ||
label={props.label || props.translate('common.state')} | ||
errorText={props.errorText} | ||
onBlur={props.onBlur} | ||
shouldSaveDraft={props.shouldSaveDraft} | ||
/> | ||
<View> | ||
<MenuItemWithTopDescription | ||
ref={ref} | ||
shouldShowRightIcon | ||
title={title} | ||
description={translate('common.state')} | ||
descriptionTextStyle={descStyle} | ||
onPress={navigateToCountrySelector} | ||
/> | ||
<View style={styles.ml5}> | ||
<FormHelpMessage message={errorText} /> | ||
</View> | ||
</View> | ||
); | ||
}); | ||
|
||
StatePicker.propTypes = propTypes; | ||
StatePicker.defaultProps = defaultProps; | ||
StatePicker.displayName = 'StatePicker'; | ||
|
||
export default withLocalize(StatePicker); | ||
export default StatePicker; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -371,6 +371,7 @@ function BaseTextInput(props) { | |
</PressableWithoutFeedback> | ||
{!_.isEmpty(inputHelpText) && ( | ||
<FormHelpMessage | ||
style={props.hintContainerStyle} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We don't seem to pass There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Seems that get lost in some merge, I've been looking for a comment or something about why I added in the first place, but I couldn't, and can't remember I will remove it |
||
isError={!_.isEmpty(props.errorText)} | ||
message={inputHelpText} | ||
/> | ||
|
Uh oh!
There was an error while loading. Please reload this page.