diff --git a/src/components/RSegment/README.md b/src/components/RSegment/README.md new file mode 100644 index 00000000..f2c22ef7 --- /dev/null +++ b/src/components/RSegment/README.md @@ -0,0 +1,7 @@ +# RSegment + +`width` 支持 `fitContent`、`block` 和像素数值。示例见 [分段器试验台](../../views/demo/segment/index.tsx)。 + +在 Naive UI 2.45.3 中,选中态默认加粗。轨道使用内容宽度时,尤其是标签长短不同或带图标、提示时,切换选中态会改变轨道宽度。NTabs 的轨道 ResizeObserver 随即执行 `handleSegmentResize`,临时加上 `transition-disabled` 并强制同步胶囊位置。这个同步会让刚开始的位移动画跳到终点。 + +`fitContent` 模式固定各标签的字重,用颜色和胶囊标示选中态。内容过长时导航区可以横向滚动,避免撑破外层。轨道真正发生尺寸变化时继续让 NTabs 同步位置。减少动态效果偏好下取消胶囊过渡。 diff --git a/src/components/RSegment/src/Segment.tsx b/src/components/RSegment/src/Segment.tsx index 47b3d8db..c5e1e9da 100644 --- a/src/components/RSegment/src/Segment.tsx +++ b/src/components/RSegment/src/Segment.tsx @@ -92,7 +92,10 @@ export default defineComponent({ '我是自定义', - }, - }, - ] - const disabledOptions: RSegmentOptions[] = [ - ...baseOptions, - { - label: '不许点', - key: 'disabled option', - disabled: true, - }, - { - label: 'popover', - key: 'popover', - popover: '我是 popover', - }, - { - label: 'popover options', - key: 'popover options', - popover: { - label: '我是配置项的 popover', - }, - }, - ] - const iconOptions = [ - { - label: '设置', - key: 'setting', - icon: , - }, - { - label: '刷新', - key: 'reload', - icon: , - }, - { - label: '搜索', - key: 'search', - icon: , - popover: '我是搜索', - }, - ] - const segmentValueOne = ref('this is three') + const { t } = useI18n('showcase') + const value = ref('overview') const size = ref('medium') - const width = ref('block') + const width = ref('fitContent') + const options = computed(() => [ + { + label: t('segmentOverview'), + key: 'overview', + icon: () => , + }, + { + label: t('segmentReports'), + key: 'reports', + popover: t('segmentPopover'), + }, + { label: t('segmentDisabled'), key: 'disabled', disabled: true }, + ]) - const segmentChange = (value: string | number) => { - window.$message.info(String(value)) - } - - return { - baseOptions, - options, - segmentValueOne, - segmentChange, - size, - width, - disabledOptions, - iconOptions, - } - }, - render() { - const { options, baseOptions, disabledOptions, iconOptions } = this - const { segmentChange } = this - - return ( - - - n-tabs 默认情况下会从 slot 获取默认展示的 tab 值,因此会产生一个 vue - slot 的 warning。如果你不想看到这个 warning,请设定一个 - default-value。 - - - 由于 NTabs 设计缘故,手动设置 width 为 fitContent 后,配置了 disabled, - popover 的配置项,会丢失过度效果。具体可以看下面的例子。 -
- 解决方法,在该组件外层包一层 div,设置宽度,但是不能为 fit-content。 -
- -

- 该组件基于 NTabs + NTabPane 封装,所以保留了一些该组件的特性。 -

-
- - - - - - - - - - - - - - - - - - - - - - - - - - 小 - 不小 - 不太小 + return () => ( + +
+ + + + + fitContent + block + 480px - - - - - - - - - RSegment 组件宽度默认为 - fitContent,按照内容自适应宽度;如果需要自适应宽度,请设置 - width 为 block;如果需要手动指定宽度请设置一个数字。 - - - fitContent - block - 800 + + S + M + L - - - - - +
+ +
+ + {t('segmentSelected', { + value: + options.value.find(({ key }) => key === value.value) + ?.label ?? value.value, + })} + + + + + + + + + + +
+
) }, })