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

update CountryField to better utilize new paragon Form.Autosuggest #1133

Open
brian-smith-tcril opened this issue Jan 23, 2024 · 3 comments
Open

Comments

@brian-smith-tcril
Copy link
Contributor

Paragon 22 includes breaking changes for Form.Autosuggest. These should aid in simplifying the implementation of CountryField, but some changes may be needed in Paragon to support certain parts of the error handling logic. One part that stands out is handleErrorChange as seen here:

@brian-smith-tcril
Copy link
Contributor Author

add callback "onErrorStateChange" to Form.Autosuggest

@brian-smith-tcril brian-smith-tcril changed the title update CountryField to use new paragon Form.Autosuggest update CountryField to better utilize new paragon Form.Autosuggest Feb 12, 2024
@brian-smith-tcril
Copy link
Contributor Author

As of #1157 the new Autosuggest component is being used, but not to its full potential

None of the internal error state management is being used

<FormAutosuggest
floatingLabel={formatMessage(messages['registration.country.label'])}
aria-label="form autosuggest"
name="country"
value={countryFieldValue || {}}
className={classNames({ 'form-field-error': props.errorMessage })}
onFocus={(e) => handleOnFocus(e)}
onBlur={(e) => handleOnBlur(e)}
onChange={(value) => handleOnChange(value)}
>

instead the error state is managed in handleOnFocus and handleOnBlur

const handleOnBlur = (event) => {
// Do not run validations when drop-down arrow is clicked
if (event.relatedTarget && event.relatedTarget.className.includes('pgn__form-autosuggest__icon-button')) {
return;
}
const { value } = event.target;
const { error } = validateCountryField(
value.trim(), countryList, formatMessage(messages['empty.country.field.error']), formatMessage(messages['invalid.country.field.error']),
);
handleErrorChange('country', error);
};
const handleOnFocus = (event) => {
handleErrorChange('country', '');
dispatch(clearRegistrationBackendError('country'));
onFocusHandler(event);
};

and there are workarounds such as

// We have put this check because proviously we also had onSelected event handler and we call
// the onBlur on that event handler but now there is no such handler and we only have
// onChange so we check the is there is proper sectionId which only be
// proper one when we select it from dropdown's item otherwise its null.
if (value.selectionId !== '') {
handleOnBlur({ target: { name: 'country', value: value.userProvidedText } });
}

The comment mentions the lack of onSelected, but handleOnSelected would just call handleOnBlur before

const handleSelected = (value) => {
handleOnBlur({ target: { name: 'country', value } });
};

which, as of #1157, is just used for updating error state

-    const { countryCode, displayValue, error } = validateCountryField(
+    const { error } = validateCountryField(
       value.trim(), countryList, formatMessage(messages['empty.country.field.error']), formatMessage(messages['invalid.country.field.error']),
     );
-
-    onChangeHandler({ target: { name: 'country' } }, { countryCode, displayValue });
     handleErrorChange('country', error);

Therefore, I believe that this can be handled with an onErrorStateChange callback in Form.Autosuggest (openedx/paragon#3002)

@zainab-amir
Copy link
Contributor

zainab-amir commented Feb 13, 2024

Hi @brian-smith-tcril, Thank you for this detailed comment.

In addition to the missing onErrorStateChange, the reason for not using the internal error state provided by Form.Autosuggest is the way it renders error. It is prefixed with "x" icon with no way to remove it. All other errors on Authn don't have it and this makes the form errors inconsistent.

AutoSuggest Error Other Errors on Authn
Screenshot 2024-02-13 at 12 55 14 PM Screenshot 2024-02-13 at 12 55 46 PM

We will resume working on this and update country field once we have onErrorStateChange. This will provide us a way to set multiple errors i.e empty field error and invalid selection error

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants