Skip to content

Commit

Permalink
feat(switch): add tab index for accessibility with tab key on switch (#…
Browse files Browse the repository at this point in the history
…95)

* feat(switch): add tab index for acceblity with tab key on switch

Add tab index for acceblity with tab key on switch

* test(switch): update snapshot tests for teb index

Update snapshot tests for tab index

* feat(switch): add onChange function and keypress enter listener

add onChange function and keypress enter listener

* refactor(switch): add enter key press listener

Add enter key press listener

* feat(switch): change eventlistener with onKeyDown prop

Change eventlistener with onKeyDown prop

* feat(switch): add onChange prop to switch component

add onChange prop to switch component

* test(switch): update switch component screenshot tests

update switch component screenshot tests
  • Loading branch information
esraltintas committed Nov 7, 2022
1 parent f83dbc3 commit 494af3a
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 8 deletions.
29 changes: 25 additions & 4 deletions packages/switch/src/Switch.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react';
import React, { useEffect, useState } from 'react';
import cn from 'classnames';

import { SwitchProps } from './Switch.types';
Expand All @@ -8,17 +8,38 @@ import styles from './styles/default.module.css';
export const Switch = React.forwardRef<HTMLInputElement, SwitchProps>(
({ checked, onChange, disabled = false, className }, ref) => {
const classNames = cn(styles.container, className);
const [isChecked, setChecked] = useState(checked);

useEffect(() => {
setChecked(checked);
}, [checked]);

const handleSwitch = () => {
setChecked(!isChecked);
onChange();
};

const handleKeyDown = (e: React.KeyboardEvent<HTMLInputElement>) => {
if (e?.key === 'Enter') {
handleSwitch();
onChange();
}
};

return (
<label className={classNames}>
<input
checked={checked}
checked={isChecked}
disabled={disabled}
onChange={onChange}
onChange={() => handleSwitch()}
ref={ref}
type="checkbox"
/>
<span className={styles.switch} />
<span
className={styles.switch}
tabIndex={0}
onKeyDown={handleKeyDown}
/>
</label>
);
},
Expand Down
4 changes: 1 addition & 3 deletions packages/switch/src/Switch.types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import React from 'react';

export interface SwitchProps {
/**
* Controls the state of the switch. If true, the switch is displayed as checked
Expand All @@ -8,7 +6,7 @@ export interface SwitchProps {
/**
* Function that will be called every time the Switch value is changed. The `boolean` property `event.target.checked` represent the new/target value of the Switch.
*/
onChange: (event: React.ChangeEvent<HTMLInputElement>) => void;
onChange: () => void;
/**
* Additional `class` names to be added
*/
Expand Down
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Object {
/>
<span
class="switch"
tabindex="0"
/>
</label>
</div>
Expand All @@ -30,6 +31,7 @@ Object {
/>
<span
class="switch"
tabindex="0"
/>
</label>
</div>,
Expand Down Expand Up @@ -101,6 +103,7 @@ Object {
/>
<span
class="switch"
tabindex="0"
/>
</label>
</div>
Expand All @@ -115,6 +118,7 @@ Object {
/>
<span
class="switch"
tabindex="0"
/>
</label>
</div>,
Expand Down Expand Up @@ -185,6 +189,7 @@ Object {
/>
<span
class="switch"
tabindex="0"
/>
</label>
</div>
Expand All @@ -198,6 +203,7 @@ Object {
/>
<span
class="switch"
tabindex="0"
/>
</label>
</div>,
Expand Down Expand Up @@ -269,6 +275,7 @@ Object {
/>
<span
class="switch"
tabindex="0"
/>
</label>
</div>
Expand All @@ -283,6 +290,7 @@ Object {
/>
<span
class="switch"
tabindex="0"
/>
</label>
</div>,
Expand Down
2 changes: 1 addition & 1 deletion packages/switch/src/docs/Component.stories.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export const SwitchTemplate = args => <Switch {...args} />;
<Story
name="Switch"
args={{
checked: true,
checked: false,
onChange: () => {},
}}
>
Expand Down

1 comment on commit 494af3a

@github-actions
Copy link

Choose a reason for hiding this comment

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

Coverage report

St.
Category Percentage Covered / Total
🟢 Statements 99.48% 951/956
🟢 Branches 90.23% 157/174
🟢 Functions 98.18% 54/55
🟢 Lines 99.66% 869/872

Test suite run success

238 tests passing in 34 suites.

Report generated by 🧪jest coverage report action from 494af3a

Please sign in to comment.