-
-
Notifications
You must be signed in to change notification settings - Fork 338
Expand file tree
/
Copy pathMultipleDates.tsx
More file actions
95 lines (85 loc) · 2.61 KB
/
MultipleDates.tsx
File metadata and controls
95 lines (85 loc) · 2.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import classNames from 'classnames';
import Overflow from 'rc-overflow';
import * as React from 'react';
import type { PickerProps } from '../../SinglePicker';
export interface MultipleDatesProps<DateType extends object = any>
extends Pick<PickerProps, 'maxTagCount' | 'maxTagPlaceholder'> {
prefixCls: string;
value: DateType[];
onRemove: (value: DateType) => void;
removeIcon?: React.ReactNode;
formatDate: (date: DateType) => string;
disabled?: boolean;
placeholder?: React.ReactNode;
}
export default function MultipleDates<DateType extends object = any>(
props: MultipleDatesProps<DateType>,
) {
const {
prefixCls,
value,
onRemove,
removeIcon = '×',
formatDate,
disabled,
maxTagCount,
maxTagPlaceholder,
placeholder,
} = props;
const selectorCls = `${prefixCls}-selector`;
const selectionCls = `${prefixCls}-selection`;
const overflowCls = `${selectionCls}-overflow`;
// ========================= Item =========================
function renderSelector(content: React.ReactNode, onClose?: React.MouseEventHandler) {
return (
<span
className={classNames(`${selectionCls}-item`)}
title={typeof content === 'string' ? content : null}
>
<span className={`${selectionCls}-item-content`}>{content}</span>
{!disabled && onClose && (
<span
onMouseDown={(e) => {
e.preventDefault();
}}
onClick={onClose}
className={`${selectionCls}-item-remove`}
>
{removeIcon}
</span>
)}
</span>
);
}
function renderItem(date: DateType) {
const displayLabel: React.ReactNode = formatDate(date);
const onClose = (event?: React.MouseEvent) => {
if (event) event.stopPropagation();
onRemove(date);
};
return renderSelector(displayLabel, onClose);
}
// ========================= Rest =========================
function renderRest(omittedValues: DateType[]) {
const content =
typeof maxTagPlaceholder === 'function'
? maxTagPlaceholder(omittedValues)
: maxTagPlaceholder;
return renderSelector(content);
}
// ======================== Render ========================
return (
<div className={selectorCls}>
<Overflow
prefixCls={overflowCls}
data={value}
renderItem={renderItem}
renderRest={renderRest}
// suffix={inputNode}
itemKey={(date) => formatDate(date)}
maxCount={maxTagCount}
/>
{!value.length && <span className={`${prefixCls}-selection-placeholder`}>{placeholder}</span>}
</div>
);
}