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

ForwardRef support - SwitchField #860

Merged
merged 2 commits into from
Nov 24, 2021
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/perfect-scissors-melt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@aws-amplify/ui-react": patch
---

ForwardRef support - SwitchField
58 changes: 34 additions & 24 deletions packages/react/src/primitives/SwitchField/SwitchField.tsx
Original file line number Diff line number Diff line change
@@ -1,33 +1,40 @@
import * as React from 'react';
import classNames from 'classnames';

import { useSwitch } from './useSwitch';
import { Label } from '../Label';
import { ComponentClassNames } from '../shared/constants';
import { Flex } from '../Flex';
import { Input } from '../Input';
import { Label } from '../Label';
import { PrimitiveWithForwardRef, SwitchFieldProps } from '../types';
import { useStableId } from '../shared/utils';
import { useSwitch } from './useSwitch';
import { View } from '../View';
import { Flex } from '../Flex';
import { VisuallyHidden } from '../VisuallyHidden';
import { ComponentClassNames } from '../shared/constants';
import { useStableId } from '../shared/utils';
import { Primitive, SwitchFieldProps } from '../types';

export const SwitchField: Primitive<SwitchFieldProps, typeof Flex> = ({
className,
defaultChecked,
id,
isChecked,
isDisabled,
isLabelHidden,
label,
labelPosition,
name,
onChange,
size,
thumbColor,
trackColor,
trackCheckedColor,
value,
...rest
}) => {
const SwitchFieldPrimitive: PrimitiveWithForwardRef<
SwitchFieldProps,
typeof Flex
> = (
{
className,
defaultChecked,
id,
isChecked,
isDisabled,
isLabelHidden,
label,
labelPosition,
name,
onChange,
size,
thumbColor,
trackCheckedColor,
trackColor,
value,
...rest
},
ref
) => {
const { isOn, changeHandler, isFocused, setIsFocused } = useSwitch({
onChange,
isChecked,
Expand All @@ -43,6 +50,7 @@ export const SwitchField: Primitive<SwitchFieldProps, typeof Flex> = ({
className={classNames(ComponentClassNames.SwitchField, className)}
data-size={size}
data-label-position={labelPosition}
ref={ref}
{...rest}
>
<VisuallyHidden>
Expand Down Expand Up @@ -92,4 +100,6 @@ export const SwitchField: Primitive<SwitchFieldProps, typeof Flex> = ({
);
};

export const SwitchField = React.forwardRef(SwitchFieldPrimitive);

SwitchField.displayName = 'SwitchField';
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
import * as React from 'react';
import { render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import kebabCase from 'lodash/kebabCase';
import userEvent from '@testing-library/user-event';

import { SwitchField } from '../SwitchField';
import { ComponentClassNames } from '../../shared';
import { AUTO_GENERATED_ID_PREFIX } from '../../shared/utils';
import { ComponentClassNames } from '../../shared';
import { ComponentPropsToStylePropsMap } from '../../types';
import { SwitchField } from '../SwitchField';

describe('Switch Field', () => {
const label = 'My switch label';

describe('Switch wrapper', () => {
it('should pass through the className', async () => {
const { container } = render(
<SwitchField label={label} className={'my-switch'} />
<SwitchField label={label} className="my-switch" />
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks!

);

const wrapper = container.getElementsByClassName(
Expand All @@ -21,10 +23,16 @@ describe('Switch Field', () => {
expect(wrapper).toHaveClass('my-switch');
});

it('should forward ref to DOM element', async () => {
const ref = React.createRef<HTMLDivElement>();
render(<SwitchField testId="testId" label={label} ref={ref} />);

await screen.findByLabelText(label);
expect(ref.current.nodeName).toBe('DIV');
});

it('should set the data-size attribute', async () => {
const { container } = render(
<SwitchField label={label} size={'large'} />
);
const { container } = render(<SwitchField label={label} size="large" />);

const wrapper = container.getElementsByClassName(
ComponentClassNames.SwitchField
Expand All @@ -34,7 +42,7 @@ describe('Switch Field', () => {

it('should set the label for attribute to match the passed in id', async () => {
const { container } = render(
<SwitchField label={label} id={'my-switch'} />
<SwitchField label={label} id="my-switch" />
);

const wrapper = container.getElementsByClassName(
Expand All @@ -45,7 +53,7 @@ describe('Switch Field', () => {

it('should set the data-label-position attribute', async () => {
const { container } = render(
<SwitchField label={label} labelPosition={'end'} />
<SwitchField label={label} labelPosition="end" />
);

const wrapper = container.getElementsByClassName(
Expand Down Expand Up @@ -102,11 +110,7 @@ describe('Switch Field', () => {

it('should pass through the name and value properties to the checkbox', async () => {
const { container } = render(
<SwitchField
label={label}
name={'myCheckbox'}
value={'checkboxValue'}
/>
<SwitchField label={label} name="myCheckbox" value="checkboxValue" />
);

const field = container.getElementsByTagName('input')[0];
Expand Down Expand Up @@ -153,7 +157,7 @@ describe('Switch Field', () => {

it('should set the id on the input element', async () => {
const { container } = render(
<SwitchField label={label} id={'my-switch'} />
<SwitchField label={label} id="my-switch" />
);

const field = container.getElementsByTagName('input')[0];
Expand All @@ -170,7 +174,7 @@ describe('Switch Field', () => {
describe('Switch Track', () => {
it('should set the track color for the unchecked switch', async () => {
const { container } = render(
<SwitchField label={label} trackColor={'red'} />
<SwitchField label={label} trackColor="red" />
);

const track = container.getElementsByClassName(
Expand All @@ -187,7 +191,7 @@ describe('Switch Field', () => {
const { container } = render(
<SwitchField
label={label}
trackCheckedColor={'red'}
trackCheckedColor="red"
defaultChecked={true}
/>
);
Expand Down Expand Up @@ -218,7 +222,7 @@ describe('Switch Field', () => {
describe('Switch Thumb', () => {
it('should change the switch thumb color', async () => {
const { container } = render(
<SwitchField label={label} thumbColor={'red'} />
<SwitchField label={label} thumbColor="red" />
);

const track = container.getElementsByClassName(
Expand Down