refactor(components): 优化复选框组件并引入 Dynamic 组件
This commit is contained in:
@@ -9,6 +9,7 @@ import {
|
|||||||
ParentProps,
|
ParentProps,
|
||||||
Show,
|
Show,
|
||||||
} from 'solid-js';
|
} from 'solid-js';
|
||||||
|
import { Dynamic } from 'solid-js/web';
|
||||||
|
|
||||||
interface CheckBoxProps {
|
interface CheckBoxProps {
|
||||||
name?: string;
|
name?: string;
|
||||||
@@ -17,6 +18,11 @@ interface CheckBoxProps {
|
|||||||
onChange?: (checked?: boolean) => void;
|
onChange?: (checked?: boolean) => void;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const CheckIcon = [
|
||||||
|
() => <Icon icon="hugeicons:square" class="text-[14px] stroke-1" />,
|
||||||
|
() => <Icon icon="hugeicons:checkmark-square-03" class="text-[14px] stroke-1 text-primary" />,
|
||||||
|
];
|
||||||
|
|
||||||
const Check: ParentComponent<CheckBoxProps> = (props) => {
|
const Check: ParentComponent<CheckBoxProps> = (props) => {
|
||||||
const mProps = mergeProps<ParentProps<CheckBoxProps>[]>(
|
const mProps = mergeProps<ParentProps<CheckBoxProps>[]>(
|
||||||
{
|
{
|
||||||
@@ -38,19 +44,13 @@ const Check: ParentComponent<CheckBoxProps> = (props) => {
|
|||||||
if (mProps.disabled) {
|
if (mProps.disabled) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
console.debug('[Check]before', internalChecked());
|
|
||||||
setInternalChecked((prev) => !prev);
|
setInternalChecked((prev) => !prev);
|
||||||
console.debug('[Check]after', internalChecked());
|
|
||||||
mProps.onChange?.(internalChecked());
|
mProps.onChange?.(internalChecked());
|
||||||
};
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div class="flex flex-row items-center gap-2 cursor-pointer" onClick={handleClick}>
|
<div class="flex flex-row items-center gap-2 cursor-pointer" onClick={handleClick}>
|
||||||
<Icon
|
<Dynamic component={CheckIcon[internalChecked() ? 1 : 0]} />
|
||||||
icon={internalChecked() ? 'hugeicons:checkmark-square-03' : 'hugeicons:square'}
|
|
||||||
class="text-[14px] stroke-1"
|
|
||||||
classList={{ 'text-primary': internalChecked() }}
|
|
||||||
/>
|
|
||||||
<div>{mProps.children}</div>
|
<div>{mProps.children}</div>
|
||||||
<Show when={isNotNil(mProps.name)}>
|
<Show when={isNotNil(mProps.name)}>
|
||||||
<input type="hidden" name={mProps.name} value={internalChecked()} />
|
<input type="hidden" name={mProps.name} value={internalChecked()} />
|
||||||
|
Reference in New Issue
Block a user