Skip to content
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

Fix: No required field validation state picker #33926

Merged
merged 2 commits into from
Jan 9, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions src/components/StatePicker/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ const propTypes = {

/** Label to display on field */
label: PropTypes.string,

/** Callback to call when the picker modal is dismissed */
onBlur: PropTypes.func,
};

const defaultProps = {
Expand All @@ -33,9 +36,10 @@ const defaultProps = {
errorText: '',
onInputChange: () => {},
label: undefined,
onBlur: () => {},
};

function StatePicker({value, errorText, onInputChange, forwardedRef, label}) {
function StatePicker({value, errorText, onInputChange, forwardedRef, label, onBlur}) {
const styles = useThemeStyles();
const {translate} = useLocalize();
const [isPickerVisible, setIsPickerVisible] = useState(false);
Expand All @@ -45,15 +49,20 @@ function StatePicker({value, errorText, onInputChange, forwardedRef, label}) {
setIsPickerVisible(true);
};

const hidePickerModal = () => {
const hidePickerModal = (shouldBlur = true) => {
if (shouldBlur) {
onBlur();
}
setIsPickerVisible(false);
};

const updateStateInput = (state) => {
if (state.value !== value) {
onInputChange(state.value);
}
hidePickerModal();
// If the user selects any state, call the hidePickerModal function with shouldBlur = false
// to prevent the onBlur function from being called.
hidePickerModal(false);
};

const title = value && _.keys(COMMON_CONST.STATES).includes(value) ? translate(`allStates.${value}.stateName`) : '';
Expand Down
Loading