For AI agents: the complete documentation index is available at https://ifhover.github.io/leaf-ui/v/0.6.0/llms.txt, the full documentation bundle is available at https://ifhover.github.io/leaf-ui/v/0.6.0/llms-full.txt, and this page is available as Markdown at https://ifhover.github.io/leaf-ui/v/0.6.0/components/segmented.md.
  • 简体中文
  • Segmented 分段控制器

    快速切换一组相关选项。

    基本用法

    当前视图: 按周
    basic.tsx
    import { Segmented, Space, Text } from '@ifhover/leaf-ui';
    
    import { useState } from 'react';
    
    export function SegmentedBasic() {
      const [value, setValue] = useState('按周');
      return (
        <Space direction="vertical" align="stretch">
          <Segmented options={['按日', '按周', '按月']} value={value} onChange={setValue} />
          <output>
            <Text tone="muted">当前视图: {value}</Text>
          </output>
        </Space>
      );
    }
    

    更多用法

    填满容器
    小尺寸
    advanced.tsx
    import { Segmented, Space, Text } from '@ifhover/leaf-ui';
    
    export function SegmentedOptions() {
      return (
        <Space direction="vertical" align="stretch">
          <Text tone="muted">填满容器</Text>
          <Segmented
            block
            options={[
              { value: 'list', label: '列表' },
              { value: 'grid', label: '栅格' },
              { value: 'archived', label: '归档(不可用)', disabled: true },
            ]}
          />
          <Text tone="muted">小尺寸</Text>
          <Segmented size="sm" options={['A', 'B', 'C']} />
        </Space>
      );
    }
    

    使用说明

    默认宽度由内容决定,block 可填满容器。options 可使用字符串或带图标的选项。value / onChange 控制选中值,方向键切换选项;name 让选中值参与原生表单提交。

    类型

    组装数据、定义回调和使用 ref 时,可以导入这些类型。

    import type { SegmentedOption, SegmentedProps } from '@ifhover/leaf-ui';

    SegmentedOption API

    属性类型默认值说明
    valuestring必填受控值
    labelReactNode必填显示内容
    iconReactNode—自定义图标
    disabledboolean—禁止交互

    SegmentedProps API

    属性类型默认值说明
    optionsreadonly (string | SegmentedOption)[]必填可用选项
    valuestring—受控值
    defaultValuestring—初始值;非受控表单 reset 时恢复
    onChange(value: string) => void—值变化回调
    disabledboolean—禁止交互
    size'sm' | 'md' | 'lg''md'尺寸或间距预设
    blockbooleanfalse填满容器宽度
    namestring—表单字段名称
    formstring—关联表单 ID