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: added controlled logic for switch #297

Merged
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
30 changes: 30 additions & 0 deletions src/ebay-switch/__tests__/__snapshots__/index.spec.tsx.snap
Original file line number Diff line number Diff line change
@@ -1,5 +1,35 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Storyshots ebay-switch Controlled switch-button 1`] = `
<DocumentFragment>
<span
class="field"
>
<span
class="switch"
>
<input
aria-checked="false"
class="switch__control"
id="switch-30"
role="switch"
type="checkbox"
value=""
/>
<span
class="switch__button"
/>
</span>
<label
class="field__label field__label--end"
for="switch-30"
>
Unchecked
</label>
</span>
</DocumentFragment>
`;

exports[`Storyshots ebay-switch Default switch-button 1`] = `
<DocumentFragment>
<span
Expand Down
9 changes: 9 additions & 0 deletions src/ebay-switch/__tests__/index.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,15 @@ describe('<EbaySwitch>', () => {

expect(spy).toBeCalledWith(anySyntheticEvent, { value, checked: true })
})

it('should not change when controlled', () => {
const checked = true
const wrapper = render(<EbaySwitch checked={checked} />)
const input = wrapper.container.querySelector('input')
fireEvent.click(input)

expect(input?.checked).toBe(checked)
})
})
})

Expand Down
28 changes: 27 additions & 1 deletion src/ebay-switch/__tests__/index.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react'
import React, { useState } from 'react'
import { action } from '../../../.storybook/action'
import { EbaySwitch } from '../index'

Expand Down Expand Up @@ -44,3 +44,29 @@ export const DisabledSwitchButton = () => (
DisabledSwitchButton.story = {
name: 'Disabled switch-button'
}

export const ControlledSwitchButton = () => {
const [checked, setChecked] = useState(false)
return (
<span className="field">
<EbaySwitch
checked={checked}
id="switch-30"
onChange={(e, props) => {
action("onChange")(e, props)
if (props) {
setChecked(props.checked)
}
}}
/>
<label className="field__label field__label--end" htmlFor="switch-30">
{checked ? "Checked" : "Unchecked"}
</label>
</span>
);
};

ControlledSwitchButton.story = {
name: "Controlled switch-button",
};

9 changes: 6 additions & 3 deletions src/ebay-switch/ebay-switch.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,19 @@ type Props = Omit<ComponentProps<'input'>, 'onChange'> & {
onChange?: EbayChangeEventHandler<HTMLInputElement, { value: string | number, checked: boolean }>;
}

const isControlled = checked => typeof checked !== 'undefined'

const EbaySwitch: FC<Props> = ({
id,
value,
name,
className,
checked,
defaultChecked = false,
onChange = () => {},
...rest
}) => {
const [isChecked, setChecked] = useState(!!checked)
const [isChecked, setChecked] = useState(defaultChecked)

useEffect(() => {
setChecked(!!checked)
Expand All @@ -40,8 +43,8 @@ const EbaySwitch: FC<Props> = ({
role="switch"
type="checkbox"
value={value}
aria-checked={isChecked}
checked={isChecked}
aria-checked={isControlled(checked) ? checked : isChecked}
checked={isControlled(checked) ? checked : isChecked}
name={name}
onChange={handleChange}
/>
Expand Down
Loading