This repository was archived by the owner on May 19, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 707
Expand file tree
/
Copy pathindex.js
More file actions
153 lines (139 loc) · 4.66 KB
/
index.js
File metadata and controls
153 lines (139 loc) · 4.66 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import styles from '../../styles';
import { defaultInputRanges, defaultStaticRanges } from '../../defaultRanges';
import { rangeShape } from '../DayCell';
import InputRangeField from '../InputRangeField';
import cx from 'classnames';
class DefinedRange extends Component {
constructor(props) {
super(props);
this.state = {
rangeOffset: 0,
focusedInput: -1,
};
}
handleRangeChange = range => {
const { onChange, ranges, focusedRange } = this.props;
let selectedRange = ranges[focusedRange[0]];
selectedRange = {
...selectedRange,
...{ label: range.label }
}
if (!onChange || !selectedRange) return;
onChange({
[selectedRange.key || `range${focusedRange[0] + 1}`]: { ...selectedRange, ...range },
});
};
getRangeOptionValue(option) {
const { ranges = [], focusedRange = [] } = this.props;
if (typeof option.getCurrentValue !== 'function') {
return '';
}
const selectedRange = ranges[focusedRange[0]] || {};
return option.getCurrentValue(selectedRange) || '';
}
getSelectedRange(ranges, staticRange) {
const focusedRangeIndex = ranges.findIndex(range => {
if (!range.startDate || !range.endDate || range.disabled) return false;
return staticRange.isSelected(range);
});
const selectedRange = ranges[focusedRangeIndex];
return { selectedRange, focusedRangeIndex };
}
render() {
const {
headerContent,
footerContent,
onPreviewChange,
inputRanges,
staticRanges,
ranges,
renderStaticRangeLabel,
rangeColors,
className,
} = this.props;
return (
<div className={cx(styles.definedRangesWrapper, className)}>
{headerContent}
<div className={styles.staticRanges}>
{staticRanges.map((staticRange, i) => {
const { selectedRange, focusedRangeIndex } = this.getSelectedRange(ranges, staticRange);
let labelContent;
if (staticRange.hasCustomRendering) {
labelContent = renderStaticRangeLabel(staticRange);
} else {
labelContent = staticRange.label;
}
return (
<button
type="button"
className={cx(styles.staticRange, {
[styles.staticRangeSelected]: Boolean(selectedRange),
})}
style={{
color: selectedRange
? selectedRange.color || rangeColors[focusedRangeIndex]
: null,
}}
key={i}
onClick={() => {
let rangeWithLabel = {
...staticRange.range(this.props),
...{ label: staticRange.label }
}
this.handleRangeChange(rangeWithLabel)
}}
onFocus={() => onPreviewChange && onPreviewChange(staticRange.range(this.props))}
onMouseOver={() =>
onPreviewChange && onPreviewChange(staticRange.range(this.props))
}
onMouseLeave={() => {
onPreviewChange && onPreviewChange();
}}>
<span tabIndex={-1} className={styles.staticRangeLabel}>
{labelContent}
</span>
</button>
);
})}
</div>
<div className={styles.inputRanges}>
{inputRanges.map((rangeOption, i) => (
<InputRangeField
key={i}
styles={styles}
label={rangeOption.label}
onFocus={() => this.setState({ focusedInput: i, rangeOffset: 0 })}
onBlur={() => this.setState({ rangeOffset: 0 })}
onChange={value => this.handleRangeChange(rangeOption.range(value, this.props))}
value={this.getRangeOptionValue(rangeOption)}
/>
))}
</div>
{footerContent}
</div>
);
}
}
DefinedRange.propTypes = {
inputRanges: PropTypes.array,
staticRanges: PropTypes.array,
ranges: PropTypes.arrayOf(rangeShape),
focusedRange: PropTypes.arrayOf(PropTypes.number),
onPreviewChange: PropTypes.func,
onChange: PropTypes.func,
footerContent: PropTypes.any,
headerContent: PropTypes.any,
rangeColors: PropTypes.arrayOf(PropTypes.string),
className: PropTypes.string,
renderStaticRangeLabel: PropTypes.func,
};
DefinedRange.defaultProps = {
inputRanges: defaultInputRanges,
staticRanges: defaultStaticRanges,
ranges: [],
rangeColors: ['#3d91ff', '#3ecf8e', '#fed14c'],
focusedRange: [0, 0],
};
export default DefinedRange;