向Switch组件增加表单可用功能。

This commit is contained in:
徐涛 2025-02-07 15:07:30 +08:00
parent d98e3a69d9
commit d817024bf3

View File

@ -1,15 +1,16 @@
import cx from 'clsx';
import { isEqual } from 'lodash-es';
import { isEqual, isNil } from 'lodash-es';
import { useCallback, useEffect, useState } from 'react';
import styles from './Switch.module.css';
type SwitchProps = {
name?: string;
checked?: boolean;
disabled?: boolean;
onChange?: (checked: boolean) => void;
};
export function Switch({ checked = false, disabled = false, onChange }: SwitchProps) {
export function Switch({ name, checked = false, disabled = false, onChange }: SwitchProps) {
const [isChecked, setIsChecked] = useState(checked);
const handleSwitch = useCallback(() => {
if (!disabled) {
@ -29,6 +30,7 @@ export function Switch({ checked = false, disabled = false, onChange }: SwitchPr
<div
className={cx(styles.switch_handle, isChecked && styles.checked)}
onClick={handleSwitch}></div>
{!isNil(name) && <input type="hidden" name={name} value={isChecked} />}
</div>
);
}