) => {
if (!isControlled) {
setInternalChecked(e.target.checked);
}
onChange?.(e);
};
// 确保 size 有默认值
const actualSize = size ?? "md";
// 滑块大小
const thumbSize = {
sm: "h-3.5 w-3.5",
md: "h-4 w-4",
lg: "h-5 w-5",
}[actualSize];
// 滑块位移
const thumbTranslate = {
sm: isChecked ? "translate-x-4" : "translate-x-0.5",
md: isChecked ? "translate-x-5" : "translate-x-0.5",
lg: isChecked ? "translate-x-6" : "translate-x-0.5",
}[actualSize];
const renderSwitch = () => (
);
const renderLabel = () => {
if (!label) return null;
return (
);
};
if (!label) {
return renderSwitch();
}
return (
{labelPosition === "left" && renderLabel()}
{renderSwitch()}
{labelPosition === "right" && renderLabel()}
);
}
);
Switch.displayName = "Switch";