Skip to content

Instantly share code, notes, and snippets.

@marshallmurphy
Created May 4, 2020 00:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marshallmurphy/7347c9b5c5ba631f80911367b6b05a03 to your computer and use it in GitHub Desktop.
Save marshallmurphy/7347c9b5c5ba631f80911367b6b05a03 to your computer and use it in GitHub Desktop.
import React, { useState } from 'react';
import styles from './toggleRow.module.css';
const ToggleRow = ({ option, index }) => {
const [active, setActive] = useState(false);
function handleToggle(e) {
e.keyCode === 13 && setActive(!active);
}
return (
<div className={styles.toggleRow}>
<label id={'toggle_' + index}>{option}</label> {/* wrap option in <label> and add an ID with the index */}
<label className={styles.toggle} tabIndex='0' onKeyDown={e => handleToggle(e)}>
<input
className={styles.toggleInput}
type='checkbox'
checked={active}
onChange={() => setActive(!active)}
tabIndex='-1'
aria-labelledby={'toggle_' + index} // use label id
/>
<span className={styles.toggleSlider}></span>
</label>
</div>
)
}
export default ToggleRow;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment