Merge pull request #307 from vector-im/dbkr/fix_toggle
Fix toggle button toggling
This commit is contained in:
commit
c7dd2e2093
1 changed files with 8 additions and 7 deletions
|
@ -1,20 +1,21 @@
|
||||||
import React, { useRef } from "react";
|
import React, { useCallback, useRef } from "react";
|
||||||
import styles from "./Toggle.module.css";
|
import styles from "./Toggle.module.css";
|
||||||
import { useToggleButton } from "@react-aria/button";
|
import { useToggleButton } from "@react-aria/button";
|
||||||
import { useToggleState } from "@react-stately/toggle";
|
|
||||||
import classNames from "classnames";
|
import classNames from "classnames";
|
||||||
import { Field } from "./Input";
|
import { Field } from "./Input";
|
||||||
|
|
||||||
export function Toggle({ id, label, className, ...rest }) {
|
export function Toggle({ id, label, className, onChange, isSelected }) {
|
||||||
const buttonRef = useRef();
|
const buttonRef = useRef();
|
||||||
const state = useToggleState(rest);
|
const toggle = useCallback(() => {
|
||||||
const { buttonProps, isPressed } = useToggleButton(rest, state, buttonRef);
|
onChange(!isSelected);
|
||||||
|
});
|
||||||
|
const { buttonProps } = useToggleButton({ isSelected }, { toggle }, buttonRef);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Field
|
<Field
|
||||||
className={classNames(
|
className={classNames(
|
||||||
styles.toggle,
|
styles.toggle,
|
||||||
{ [styles.on]: isPressed },
|
{ [styles.on]: isSelected },
|
||||||
className
|
className
|
||||||
)}
|
)}
|
||||||
>
|
>
|
||||||
|
@ -23,7 +24,7 @@ export function Toggle({ id, label, className, ...rest }) {
|
||||||
ref={buttonRef}
|
ref={buttonRef}
|
||||||
id={id}
|
id={id}
|
||||||
className={classNames(styles.button, {
|
className={classNames(styles.button, {
|
||||||
[styles.isPressed]: isPressed,
|
[styles.isPressed]: isSelected,
|
||||||
})}
|
})}
|
||||||
>
|
>
|
||||||
<div className={styles.ball} />
|
<div className={styles.ball} />
|
||||||
|
|
Loading…
Add table
Reference in a new issue