This repository has been archived by the owner on Sep 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
QuickSelection.js
90 lines (81 loc) · 2.57 KB
/
QuickSelection.js
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
/* eslint-disable react/static-property-placement */
/* eslint-disable react/require-default-props */
/* eslint-disable react/destructuring-assignment */
/* eslint-disable jsx-a11y/click-events-have-key-events */
import * as React from 'react';
import PropTypes from 'prop-types';
import classNames from 'classnames';
import FormattedMessage from './IntlProvider/FormattedMessage';
import { prefix, defaultProps } from './utils';
class QuickSelection extends React.PureComponent {
static propTypes = {
ranges: PropTypes.array,
className: PropTypes.string,
classPrefix: PropTypes.string,
pageDate: PropTypes.array,
onShortcut: PropTypes.func,
disabledShortcutButton: PropTypes.func,
selectedIndex: PropTypes.number,
tempSelectedIndex: PropTypes.number
};
hasLocaleKey = (key) => {
return this.props.ranges.some((item) => item.label === key);
};
addPrefix = (name) => prefix(this.props.classPrefix)(name);
getSelectedIndex = () => {
const { selectedIndex, tempSelectedIndex } = this.props;
if (tempSelectedIndex === null) {
return selectedIndex;
}
if (selectedIndex !== tempSelectedIndex) {
return tempSelectedIndex;
}
return selectedIndex;
};
render() {
const { ranges, onShortcut, disabledShortcutButton, pageDate } = this.props;
return (
<div
className={classNames(
this.addPrefix('ranges'),
'kizen-quick-selection'
)}
>
{ranges.map((item, index) => {
const currSelectedIndex = this.getSelectedIndex();
const value =
typeof item.value === 'function'
? item.value(pageDate)
: item.value;
const disabled =
disabledShortcutButton && disabledShortcutButton(value);
const itemClassName = classNames(this.addPrefix('option'), {
[this.addPrefix('option-disabled')]: disabled,
selected: currSelectedIndex === index
});
return (
<button
key={index}
type="button"
tabIndex={-1}
className={itemClassName}
onClick={() => {
!disabled && onShortcut(value, index);
}}
>
{this.hasLocaleKey(item.label) ? (
<FormattedMessage id={item.label} />
) : (
item.label
)}
</button>
);
})}
</div>
);
}
}
const enhance = defaultProps({
classPrefix: 'picker-quickSelection'
});
export default enhance(QuickSelection);