跳转到正文

RSegment ​

RSegment 用 Naive UI Tabs 的 segment 模式渲染选项。通过 value 和 onUpdateValue(或 v-model:value)切换业务状态,defaultValue 可提供非受控初值。页面内容由调用方根据值渲染。

属性默认说明
options[]RSegmentOptions[],每个选项的 key 需唯一。
width'fitContent''block' 占满、'fitContent' 按内容、数字按像素。
themeOverrides内置分段间距业务覆盖与内置配置浅合并。

每项必填 key: string \| number、label(文本、VNode 或渲染类型)。可用 disabled、icon(VNode 或函数)、popover(文本或含 label 的 Naive UI Popover 配置)、slots.default。虽然类型提供 displayDirective,当前实现没有把该字段传给选项,不能用它控制面板挂载。组件固定 type="segment" 与动画,不支持 Tabs 的增删页签、位置、Pane 样式等被排除字段。

交互示例 ​

查看示例源码
tsx
import type { RSegmentWidth } from '@/components'
import { useI18n } from '@/hooks'
import type { ComponentSize } from '@/types'
import { RIcon } from '@ray-template/ui/icon'
import { RSegment } from '@ray-template/ui/segment'
import type { RSegmentOptions } from '@ray-template/ui/segment'
import { NCard, NFlex, NRadioButton, NRadioGroup } from 'naive-ui'
import DemoPage from '../components/DemoPage'

export default defineComponent({
  name: 'SegmentDemo',
  setup() {
    const { t } = useI18n('showcase')
    const value = ref<string | number>('overview')
    const size = ref<ComponentSize>('medium')
    const width = ref<RSegmentWidth>('fitContent')
    const options = computed<RSegmentOptions[]>(() => [
      {
        label: t('segmentOverview'),
        key: 'overview',
        icon: () => <RIcon name="navigation/dashboard" />,
      },
      {
        label: t('segmentReports'),
        key: 'reports',
        popover: t('segmentPopover'),
      },
      { label: t('segmentDisabled'), key: 'disabled', disabled: true },
    ])

    return () => (
      <DemoPage
        title={t('segmentTitle')}
        description={t('segmentDescription')}
        eyebrow="RAY TEMPLATE / EXAMPLES"
      >
        <div class="r-demo-grid">
          <NCard class="r-demo-grid__wide" title={t('segmentPlayground')}>
            <NFlex vertical size="large">
              <NFlex align="center">
                <NRadioGroup
                  v-model:value={width.value}
                  aria-label={t('segmentWidth')}
                >
                  <NRadioButton value="fitContent">fitContent</NRadioButton>
                  <NRadioButton value="block">block</NRadioButton>
                  <NRadioButton value={480}>480px</NRadioButton>
                </NRadioGroup>
                <NRadioGroup
                  v-model:value={size.value}
                  aria-label={t('segmentSize')}
                >
                  <NRadioButton value="small">S</NRadioButton>
                  <NRadioButton value="medium">M</NRadioButton>
                  <NRadioButton value="large">L</NRadioButton>
                </NRadioGroup>
              </NFlex>
              <div class="r-demo-stage">
                <RSegment
                  v-model:value={value.value}
                  options={options.value}
                  width={width.value}
                  size={size.value}
                />
              </div>
              <output>
                {t('segmentSelected', {
                  value:
                    options.value.find(({ key }) => key === value.value)
                      ?.label ?? value.value,
                })}
              </output>
            </NFlex>
          </NCard>

          <NCard title={t('segmentUncontrolled')}>
            <RSegment
              defaultValue="overview"
              options={options.value.slice(0, 2)}
            />
          </NCard>
          <NCard title={t('segmentCustom')}>
            <RSegment
              defaultValue="overview"
              options={[
                options.value[0],
                {
                  key: 'custom',
                  label: h(
                    'span',
                    { style: { fontVariantNumeric: 'tabular-nums' } },
                    t('segmentCustomLabel'),
                  ),
                },
              ]}
            />
          </NCard>
        </div>
      </DemoPage>
    )
  },
})
tsx
import { RSegment, type RSegmentOptions } from '@/components'
import { defineComponent, ref } from 'vue'

export default defineComponent({
  setup() {
    const selected = ref<string | number>('day')
    const options: RSegmentOptions[] = [
      { key: 'day', label: '日', popover: '按日查看' },
      { key: 'week', label: '周' },
    ]
    return () => (
      <RSegment
        options={options}
        value={selected.value}
        onUpdateValue={(value) => {
          selected.value = value
        }}
      />
    )
  },
})

组件实现 · 选项类型 · 不支持的 Tabs 属性