Skip to content

Commit

Permalink
feat: Timeline.Item support label (ant-design#21560)
Browse files Browse the repository at this point in the history
* feat: Timeline add label

* improve label style with mode

* update snapshot

* use optional chaining

* add doc

* Update label.md

Co-authored-by: 偏右 <[email protected]>
  • Loading branch information
shaodahong and afc163 authored Feb 26, 2020
1 parent a321b5e commit 3269d8c
Show file tree
Hide file tree
Showing 8 changed files with 250 additions and 12 deletions.
26 changes: 16 additions & 10 deletions components/timeline/Timeline.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,6 @@ export default class Timeline extends React.Component<TimelineProps, any> {
} = this.props;
const prefixCls = getPrefixCls('timeline', customizePrefixCls);
const pendingNode = typeof pending === 'boolean' ? null : pending;
const classString = classNames(
prefixCls,
{
[`${prefixCls}-pending`]: !!pending,
[`${prefixCls}-reverse`]: !!reverse,
[`${prefixCls}-${mode}`]: !!mode,
[`${prefixCls}-rtl`]: direction === 'rtl',
},
className,
);

const pendingItem = pending ? (
<TimelineItem pending={!!pending} dot={pendingDot || <LoadingOutlined />}>
Expand Down Expand Up @@ -86,6 +76,22 @@ export default class Timeline extends React.Component<TimelineProps, any> {
});
});

const hasLabelItem = timeLineItems.some(
(item: React.ReactElement<any>) => !!item?.props?.label,
);

const classString = classNames(
prefixCls,
{
[`${prefixCls}-pending`]: !!pending,
[`${prefixCls}-reverse`]: !!reverse,
[`${prefixCls}-${mode}`]: !!mode && !hasLabelItem,
[`${prefixCls}-label`]: hasLabelItem,
[`${prefixCls}-rtl`]: direction === 'rtl',
},
className,
);

return (
<ul {...restProps} className={classString}>
{items}
Expand Down
3 changes: 3 additions & 0 deletions components/timeline/TimelineItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export interface TimeLineItemProps {
pending?: boolean;
position?: string;
style?: React.CSSProperties;
label?: React.ReactNode;
}

const TimelineItem: React.SFC<TimeLineItemProps> = props => (
Expand All @@ -23,6 +24,7 @@ const TimelineItem: React.SFC<TimeLineItemProps> = props => (
children,
pending,
dot,
label,
...restProps
} = props;

Expand All @@ -43,6 +45,7 @@ const TimelineItem: React.SFC<TimeLineItemProps> = props => (

return (
<li {...omit(restProps, ['position'])} className={itemClassName}>
{label && <div className={`${prefixCls}-item-label`}>{label}</div>}
<div className={`${prefixCls}-item-tail`} />
<div
className={dotClassName}
Expand Down
147 changes: 147 additions & 0 deletions components/timeline/__tests__/__snapshots__/demo.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,153 @@ exports[`renders ./components/timeline/demo/custom.md correctly 1`] = `
</ul>
`;

exports[`renders ./components/timeline/demo/label.md correctly 1`] = `
Array [
<div
class="ant-radio-group ant-radio-group-outline"
style="margin-bottom:20px"
>
<label
class="ant-radio-wrapper ant-radio-wrapper-checked"
>
<span
class="ant-radio ant-radio-checked"
>
<input
checked=""
class="ant-radio-input"
type="radio"
value="left"
/>
<span
class="ant-radio-inner"
/>
</span>
<span>
Left
</span>
</label>
<label
class="ant-radio-wrapper"
>
<span
class="ant-radio"
>
<input
class="ant-radio-input"
type="radio"
value="right"
/>
<span
class="ant-radio-inner"
/>
</span>
<span>
Right
</span>
</label>
<label
class="ant-radio-wrapper"
>
<span
class="ant-radio"
>
<input
class="ant-radio-input"
type="radio"
value="alternate"
/>
<span
class="ant-radio-inner"
/>
</span>
<span>
Alternate
</span>
</label>
</div>,
<ul
class="ant-timeline ant-timeline-label"
>
<li
class="ant-timeline-item ant-timeline-item-left"
>
<div
class="ant-timeline-item-label"
>
2015-09-01
</div>
<div
class="ant-timeline-item-tail"
/>
<div
class="ant-timeline-item-head ant-timeline-item-head-blue"
/>
<div
class="ant-timeline-item-content"
>
Create a services
</div>
</li>
<li
class="ant-timeline-item ant-timeline-item-left"
>
<div
class="ant-timeline-item-label"
>
2015-09-01 09:12:11
</div>
<div
class="ant-timeline-item-tail"
/>
<div
class="ant-timeline-item-head ant-timeline-item-head-blue"
/>
<div
class="ant-timeline-item-content"
>
Solve initial network problems
</div>
</li>
<li
class="ant-timeline-item ant-timeline-item-left"
>
<div
class="ant-timeline-item-tail"
/>
<div
class="ant-timeline-item-head ant-timeline-item-head-blue"
/>
<div
class="ant-timeline-item-content"
>
Technical testing
</div>
</li>
<li
class="ant-timeline-item ant-timeline-item-last ant-timeline-item-left"
>
<div
class="ant-timeline-item-label"
>
2015-09-01 09:12:11
</div>
<div
class="ant-timeline-item-tail"
/>
<div
class="ant-timeline-item-head ant-timeline-item-head-blue"
/>
<div
class="ant-timeline-item-content"
>
Network problems being solved
</div>
</li>
</ul>,
]
`;

exports[`renders ./components/timeline/demo/pending.md correctly 1`] = `
<div>
<ul
Expand Down
15 changes: 14 additions & 1 deletion components/timeline/__tests__/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ import rtlTest from '../../../tests/shared/rtlTest';

const { Item } = TimeLine;

const wrapperFactory = (timeLineProps = {}) =>
const wrapperFactory = (timeLineProps = {}, labelItems) =>
mount(
<TimeLine type="editable-card" {...timeLineProps}>
<Item key="1">foo</Item>
<Item key="2">bar</Item>
<Item key="3">baz</Item>
{labelItems}
</TimeLine>,
);

Expand Down Expand Up @@ -136,4 +137,16 @@ describe('TimeLine', () => {
).toBe(true);
});
});

it('renders Timeline item with label correctly', () => {
const label = '2020-01-01';
const wrapper = wrapperFactory(
{},
<Item key="1" label={label}>
foo
</Item>,
);
expect(wrapper.find('.ant-timeline-label')).toHaveLength(1);
expect(wrapper.find('.ant-timeline-item-label').text()).toBe(label);
});
});
51 changes: 51 additions & 0 deletions components/timeline/demo/label.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
---
order: 5
title:
zh-CN: 标签
en-US: Label
---

## zh-CN

使用 `label` 标签单独展示时间。

## en-US

Use `label` show time alone.

```jsx
import { useState } from 'react';
import { Timeline, Radio } from 'antd';

function TimelimeLabelDemo() {
const [mode, setMode] = useState('left');

const onChange = e => {
setMode(e.target.value);
};

return (
<>
<Radio.Group
onChange={onChange}
value={mode}
style={{
marginBottom: 20,
}}
>
<Radio value="left">Left</Radio>
<Radio value="right">Right</Radio>
<Radio value="alternate">Alternate</Radio>
</Radio.Group>
<Timeline mode={mode}>
<Timeline.Item label="2015-09-01">Create a services</Timeline.Item>
<Timeline.Item label="2015-09-01 09:12:11">Solve initial network problems</Timeline.Item>
<Timeline.Item>Technical testing</Timeline.Item>
<Timeline.Item label="2015-09-01 09:12:11">Network problems being solved</Timeline.Item>
</Timeline>
</>
);
}

ReactDOM.render(<TimelimeLabelDemo />, mountNode);
```
1 change: 1 addition & 0 deletions components/timeline/index.en-US.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,4 @@ Node of timeline
| color | Set the circle's color to `blue`, `red`, `green`, `gray` or other custom colors | string | `blue` |
| dot | Customize timeline dot | string\|ReactNode | - |
| position | Customize node position | `left` \| `right` | - |
| label | Set the label | ReactNode | - |
1 change: 1 addition & 0 deletions components/timeline/index.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,4 @@ title: Timeline
| color | 指定圆圈颜色 `blue, red, green, gray`,或自定义的色值 | string | blue |
| dot | 自定义时间轴点 | string\|ReactNode | - |
| position | 自定义节点位置 | `left` \| `right` | - |
| label | 设置标签 | ReactNode | - |
18 changes: 17 additions & 1 deletion components/timeline/style/index.less
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,8 @@
}

&.@{timeline-prefix-cls}-alternate,
&.@{timeline-prefix-cls}-right {
&.@{timeline-prefix-cls}-right,
&.@{timeline-prefix-cls}-label {
.@{timeline-prefix-cls}-item {
&-tail,
&-head,
Expand Down Expand Up @@ -231,4 +232,19 @@
min-height: 48px;
}
}
&.@{timeline-prefix-cls}-label {
.@{timeline-prefix-cls}-item-label {
position: absolute;
top: -(@font-size-base * @line-height-base - @font-size-base) + 1px;
width: calc(50% - 12px);
text-align: right;
}
.@{timeline-prefix-cls}-item-right {
.@{timeline-prefix-cls}-item-label {
left: calc(50% + 14px);
width: calc(50% - 14px);
text-align: left;
}
}
}
}

0 comments on commit 3269d8c

Please sign in to comment.