Skip to content

Commit 3334263

Browse files
committed
feat(TimePicker): support preview mode
1 parent d18b6f0 commit 3334263

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

src/time-picker/time-picker.jsx

+40
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,15 @@ class TimePicker extends Component {
147147
* 是否禁用
148148
*/
149149
disabled: PropTypes.bool,
150+
/**
151+
* 是否为预览态
152+
*/
153+
isPreview: PropTypes.bool,
154+
/**
155+
* 预览态模式下渲染的内容
156+
* @param {number} value 评分值
157+
*/
158+
renderPreview: PropTypes.func,
150159
/**
151160
* 时间值改变时的回调
152161
* @param {Object|String} value 时间对象或时间字符串
@@ -316,6 +325,30 @@ class TimePicker extends Component {
316325
this.props.onVisibleChange(visible, type);
317326
};
318327

328+
renderPreview(others) {
329+
const { prefix, format, renderPreview } = this.props;
330+
const { value } = this.state;
331+
const previewCls = classnames({
332+
[`${prefix}form-preview`]: true,
333+
});
334+
335+
const label = value ? value.format(format) : '';
336+
337+
if (typeof renderPreview === 'function') {
338+
return (
339+
<div {...others} className={previewCls}>
340+
{renderPreview(value, this.props)}
341+
</div>
342+
);
343+
}
344+
345+
return (
346+
<p {...others} className={previewCls}>
347+
{label}
348+
</p>
349+
);
350+
}
351+
319352
render() {
320353
const {
321354
prefix,
@@ -346,11 +379,18 @@ class TimePicker extends Component {
346379
className,
347380
locale,
348381
rtl,
382+
isPreview,
349383
...others
350384
} = this.props;
351385

352386
const { value, inputStr, inputing, visible } = this.state;
353387

388+
if (isPreview) {
389+
return this.renderPreview(
390+
obj.pickOthers(others, TimePicker.PropTypes)
391+
);
392+
}
393+
354394
const triggerCls = classnames({
355395
[`${prefix}time-picker-trigger`]: true,
356396
});

test/time-picker/index-spec.js

+12
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,18 @@ describe('TimePicker', () => {
133133
).at(10).text() === '10'
134134
);
135135
});
136+
it('should support preview mode render', () => {
137+
wrapper = mount(<TimePicker defaultValue="12:00:00" isPreview />);
138+
assert(wrapper.find('.next-form-preview').length > 0);
139+
assert(wrapper.find('.next-form-preview').text() === '12:00:00');
140+
wrapper.setProps({
141+
renderPreview: (value) => {
142+
assert(value.format('HH:mm:ss') === '12:00:00');
143+
return 'Hello World';
144+
}
145+
});
146+
assert(wrapper.find('.next-form-preview').text() === 'Hello World');
147+
});
136148
});
137149

138150
describe('action', () => {

0 commit comments

Comments
 (0)