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

bug(fix): PhoneNumberField does not honor readonly prop #1878

Merged
merged 4 commits into from
May 13, 2022
Merged
Show file tree
Hide file tree
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
5 changes: 5 additions & 0 deletions .changeset/serious-geckos-act.md
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
Original file line number Diff line number Diff line change
Expand Up @@ -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}>
Copy link
Contributor

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.

{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}
Copy link
Contributor

Choose a reason for hiding this comment

The 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}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const PhoneNumberFieldPrimitive: Primitive<PhoneNumberFieldProps, 'input'> = (
defaultCountryCode,
hasError,
isDisabled,
isReadOnly,
onCountryCodeChange,
onInput,
size,
Expand All @@ -36,6 +37,7 @@ const PhoneNumberFieldPrimitive: Primitive<PhoneNumberFieldProps, 'input'> = (
className={className}
hasError={hasError}
isDisabled={isDisabled}
isReadOnly={isReadOnly}
label={countryCodeLabel}
name={countryCodeName}
onChange={onCountryCodeChange}
Expand All @@ -48,6 +50,7 @@ const PhoneNumberFieldPrimitive: Primitive<PhoneNumberFieldProps, 'input'> = (
className={classNames(ComponentClassNames.PhoneNumberField, className)}
hasError={hasError}
isDisabled={isDisabled}
isReadOnly={isReadOnly}
isMultiline={false}
onInput={onInput}
ref={ref}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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>();
Expand Down Expand Up @@ -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 () => {
Copy link
Contributor

Choose a reason for hiding this comment

The 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;
});
});
1 change: 1 addition & 0 deletions packages/react/src/primitives/types/phoneNumberField.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@ export interface PhoneNumberFieldProps extends TextInputFieldProps {
export interface CountryCodeSelectProps extends SelectFieldProps {
defaultValue: string;
dialCodeList?: Array<string>;
isReadOnly?: boolean;
}