feat(components): 添加默认选中功能

- 在 Check、Radio、RadioGroup 和 Segments 组件中添加默认选中值属性
- 使用 onMount 在组件挂载时设置默认选中值
- 优化了组件的初始化逻辑,确保默认值能够正确显示
This commit is contained in:
Vixalie
2025-08-13 22:48:01 +08:00
parent 727fb43791
commit 5523047b69
4 changed files with 28 additions and 0 deletions

View File

@@ -8,6 +8,7 @@ import {
Index,
JSX,
mergeProps,
onMount,
Show,
} from 'solid-js';
@@ -20,6 +21,7 @@ interface SegmentsProps {
name?: string;
options: Option[];
value?: Option['value'];
defaultValue?: Option['value'];
direction?: 'horizontal' | 'vertical';
disabled?: boolean;
onChange?: (value: Option['value'] | undefined) => void;
@@ -43,6 +45,11 @@ const Segments: Component<SegmentsProps> = (props) => {
const [indicatorHeight, setIndicatorHeight] = createSignal(0);
const [indicatorWidth, setIndicatorWidth] = createSignal(0);
onMount(() => {
if (isNotNil(mProps.defaultValue)) {
setSelected(mProps.defaultValue);
}
});
createEffect(() => {
if (isNotNil(originalValue()) && originalValue() !== selected()) {
setSelected(originalValue());