Skip to content

Commit 92ce8cd

Browse files
authored
fix(design): Segmented style with icon (#1300)
1 parent da067c9 commit 92ce8cd

File tree

6 files changed

+31
-14
lines changed

6 files changed

+31
-14
lines changed

packages/design/src/segmented/__tests__/__snapshots__/index.test.tsx.snap

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,6 @@ exports[`Segmented > render options with ellipsis 1`] = `
152152
>
153153
<div
154154
class="ant-flex ant-flex-align-center ant-flex-justify-center"
155-
style="gap: 4px;"
156155
>
157156
<span
158157
aria-describedby="test-id"

packages/design/src/segmented/demo/ellipsis.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import React from 'react';
22
import { Segmented } from '@oceanbase/design';
3+
import { BarsOutlined } from '@oceanbase/icons';
34

45
export default () => {
56
return (
@@ -12,14 +13,13 @@ export default () => {
1213
value: 'longtext1',
1314
label:
1415
'longtext-longtext-longtext-longtext-longtext-longtext-longtext-longtext-longtext-longtext-longtext-longtext-longtext-longtext-longtext-longtext',
15-
ellipsis: {
16-
tooltip: true,
17-
},
16+
icon: <BarsOutlined />,
1817
},
1918
{
2019
value: 'longtext2',
2120
label:
2221
'longtext-longtext-longtext-longtext-longtext-longtext-longtext-longtext-longtext-longtext-longtext-longtext-longtext-longtext-longtext-longtext',
22+
// custom ellipsis
2323
ellipsis: {
2424
tooltip: {
2525
title: 'custom tooltip title',
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import React from 'react';
2+
import { Segmented } from '@oceanbase/design';
3+
import { AppstoreOutlined, BarsOutlined } from '@oceanbase/icons';
4+
5+
const Demo: React.FC = () => (
6+
<Segmented
7+
options={[
8+
{ label: 'List', value: 'List', icon: <BarsOutlined /> },
9+
{ label: 'Kanban', value: 'Kanban', icon: <AppstoreOutlined /> },
10+
]}
11+
/>
12+
);
13+
14+
export default Demo;

packages/design/src/segmented/index.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,13 @@ nav:
1111

1212
## 代码演示
1313

14-
### 基本使用
15-
1614
<!-- prettier-ignore -->
1715
<code src="./demo/basic.tsx" title="基本"></code>
16+
<code src="./demo/icon.tsx" title="带图标"></code>
1817
<code src="./demo/disabled.tsx" title="不可用"></code>
1918
<code src="./demo/size.tsx" title="三种大小"></code>
2019
<code src="./demo/block.tsx" title="block" description="block 属性使其撑满父元素宽度。"></code>
21-
<code src="./demo/ellipsis.tsx" title="省略" description="需要同时配置分段器的 block 和选项的 ellipsis 属性"></code>
20+
<code src="./demo/ellipsis.tsx" title="省略" description="内容超长会自动省略,并展示 tooltip,仅开启 `block` 时生效。可通过 `ellipsis` 设置省略属性"></code>
2221
<code src="./demo/badge.tsx" title="带徽标" description="展示徽标"></code>
2322

2423
## API

packages/design/src/segmented/index.tsx

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ export * from 'antd/es/segmented';
1717
type BadgeType = BadgeProps | BadgeProps['count'];
1818

1919
export type SegmentedLabeledOption = AntSegmentedLabeledOption & {
20+
icon?: React.ReactNode;
2021
ellipsis?: EllipsisConfig;
2122
badge?: BadgeType;
2223
};
@@ -44,16 +45,19 @@ const Segmented = React.forwardRef<HTMLDivElement, SegmentedProps>(
4445

4546
const newOptions = options?.map(item => {
4647
if (typeof item === 'object') {
47-
const { label, badge, ...restItem } = item;
48+
const {
49+
label,
50+
icon,
51+
badge,
52+
ellipsis = { tooltip: true },
53+
...restItem
54+
} = item as SegmentedLabeledOption;
4855
return {
4956
...restItem,
5057
label: (
51-
<Flex gap={4} align="center" justify="center">
52-
{(item as SegmentedLabeledOption)?.ellipsis ? (
53-
<Typography.Text ellipsis={item.ellipsis}>{label}</Typography.Text>
54-
) : (
55-
label
56-
)}
58+
<Flex align="center" justify="center">
59+
{icon && <span className={`${prefixCls}-item-icon`}>{icon}</span>}
60+
{ellipsis ? <Typography.Text ellipsis={ellipsis}>{label}</Typography.Text> : label}
5761
{badge && renderBadge(badge)}
5862
</Flex>
5963
),

packages/design/src/segmented/style/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ export const genSegmentedStyle: GenerateStyle<SegmentedToken> = (
2828
[`>${componentCls}-item`]: {
2929
[`>${componentCls}-item-label`]: {
3030
[`${antCls}-badge >${antCls}-badge-count`]: {
31+
marginInlineStart: 4,
3132
backgroundColor: colorFillSecondary,
3233
color: 'inherit',
3334
boxShadow: 'none',

0 commit comments

Comments
 (0)