-
Notifications
You must be signed in to change notification settings - Fork 308
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
bug(fix): PhoneNumberField does not honor readonly prop #1878
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
c0ad913
fixing isReadOnly bug for PhoneNumberField CountryCodeSelect primitive
473bef7
Create serious-geckos-act.md
joebuono d44e170
Merge branch 'main' into readonly-phoneNumberField
bd13a85
added comments about mapping isReadOnly to the disabled attribute for…
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@aws-amplify/ui-react": patch | ||
--- | ||
|
||
bug(fix): PhoneNumberField does not honor readonly prop |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,20 +7,26 @@ import { CountryCodeSelectProps, Primitive } from '../types'; | |
import { SelectField } from '../SelectField'; | ||
|
||
const CountryCodeSelectPrimitive: Primitive<CountryCodeSelectProps, 'select'> = | ||
({ className, dialCodeList, ...props }, ref) => { | ||
({ className, dialCodeList, isReadOnly, ...props }, ref) => { | ||
const dialList = dialCodeList ?? countryDialCodes; | ||
const countryCodeOptions = React.useMemo( | ||
() => | ||
dialList.map((dialCode) => ( | ||
<option key={dialCode} value={dialCode}> | ||
// Regarding the `disabled` attribute, see comment in SelectField below | ||
<option key={dialCode} value={dialCode} disabled={isReadOnly}> | ||
{dialCode} | ||
</option> | ||
)), | ||
[dialList] | ||
[dialList, isReadOnly] | ||
); | ||
|
||
return ( | ||
<SelectField | ||
/* | ||
Since <select> elements do not support the `readonly` html attribute, it is suggested to use the `disabled` html attribute | ||
so that a screen reader will announce something to the user about the interactivity of the options list ( https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/readonly) | ||
*/ | ||
aria-disabled={isReadOnly} | ||
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. Same here, I think there should be a comment on why this is more accessible. |
||
autoComplete="tel-country-code" | ||
className={classNames(ComponentClassNames.CountryCodeSelect, className)} | ||
labelHidden={true} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,8 +3,13 @@ import { render, screen } from '@testing-library/react'; | |
import userEvent from '@testing-library/user-event'; | ||
|
||
import { PhoneNumberField } from '../PhoneNumberField'; | ||
import { Flex } from '../../Flex'; | ||
import { Button } from '../../Button'; | ||
import { ComponentClassNames } from '../../shared/constants'; | ||
|
||
const originalLog = console.log; | ||
console.log = jest.fn(); | ||
|
||
describe('PhoneNumberField primitive', () => { | ||
const setup = async ({ | ||
defaultCountryCode = '+1', | ||
|
@@ -29,6 +34,31 @@ describe('PhoneNumberField primitive', () => { | |
}; | ||
}; | ||
|
||
const ReadOnlyFormTest = () => { | ||
const inputRef = React.useRef(null); | ||
const countryCodeRef = React.useRef(null); | ||
|
||
const handleSubmit = (e) => { | ||
e.preventDefault(); | ||
console.log(`${countryCodeRef.current.value} ${inputRef.current.value}`); | ||
}; | ||
|
||
return ( | ||
<Flex as="form" onSubmit={handleSubmit}> | ||
<PhoneNumberField | ||
defaultCountryCode="+40" | ||
defaultValue="1234567" | ||
label="Read Only" | ||
name="read_only_phone" | ||
ref={inputRef} | ||
countryCodeRef={countryCodeRef} | ||
isReadOnly | ||
/> | ||
<Button type="submit">Submit</Button> | ||
</Flex> | ||
); | ||
}; | ||
|
||
it('should forward ref and countryCodeRef to DOM elements', async () => { | ||
const ref = React.createRef<HTMLInputElement>(); | ||
const countryCodeRef = React.createRef<HTMLSelectElement>(); | ||
|
@@ -135,4 +165,28 @@ describe('PhoneNumberField primitive', () => { | |
|
||
expect(onCountryCodeChange).toHaveBeenCalled(); | ||
}); | ||
|
||
/* | ||
Since <select> elements do not support the `readonly` html attribute, it is suggested to use the `disabled` html attribute | ||
so that a screen reader will announce something to the user about the interactivity of the options list ( https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/readonly) | ||
*/ | ||
it('should set aria-disabled="true" when the isReadOnly prop is passed, and disable all the select options', async () => { | ||
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. Add comment on why this behavior exists |
||
const { $countryCodeSelector } = await setup({ isReadOnly: true }); | ||
|
||
expect($countryCodeSelector).toHaveAttribute('aria-disabled', 'true'); | ||
|
||
$countryCodeSelector.querySelectorAll('option').forEach((option) => { | ||
expect(option).toHaveAttribute('disabled'); | ||
}); | ||
}); | ||
|
||
it('should still submit the form values when the isReadOnly prop is passed', async () => { | ||
const { container } = render(<ReadOnlyFormTest />); | ||
|
||
const button = container.getElementsByTagName('button')[0]; | ||
userEvent.click(button); | ||
expect(console.log).toHaveBeenCalledWith('+40 1234567'); | ||
|
||
console.log = originalLog; | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we add a comment of why we are mapping disabled to isReadOnly? We will wonder later on.