向Switch组件增加表单可用功能。
This commit is contained in:
parent
d98e3a69d9
commit
d817024bf3
|
@ -1,15 +1,16 @@
|
||||||
import cx from 'clsx';
|
import cx from 'clsx';
|
||||||
import { isEqual } from 'lodash-es';
|
import { isEqual, isNil } from 'lodash-es';
|
||||||
import { useCallback, useEffect, useState } from 'react';
|
import { useCallback, useEffect, useState } from 'react';
|
||||||
import styles from './Switch.module.css';
|
import styles from './Switch.module.css';
|
||||||
|
|
||||||
type SwitchProps = {
|
type SwitchProps = {
|
||||||
|
name?: string;
|
||||||
checked?: boolean;
|
checked?: boolean;
|
||||||
disabled?: boolean;
|
disabled?: boolean;
|
||||||
onChange?: (checked: boolean) => void;
|
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 [isChecked, setIsChecked] = useState(checked);
|
||||||
const handleSwitch = useCallback(() => {
|
const handleSwitch = useCallback(() => {
|
||||||
if (!disabled) {
|
if (!disabled) {
|
||||||
|
@ -29,6 +30,7 @@ export function Switch({ checked = false, disabled = false, onChange }: SwitchPr
|
||||||
<div
|
<div
|
||||||
className={cx(styles.switch_handle, isChecked && styles.checked)}
|
className={cx(styles.switch_handle, isChecked && styles.checked)}
|
||||||
onClick={handleSwitch}></div>
|
onClick={handleSwitch}></div>
|
||||||
|
{!isNil(name) && <input type="hidden" name={name} value={isChecked} />}
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user