/
IndicatorSelect.js
102 lines (88 loc) · 2.73 KB
/
IndicatorSelect.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
91
92
93
94
95
96
97
98
99
100
101
102
// @flow
import React from 'react';
import { MenuItem, Intent } from '@blueprintjs/core';
import { MultiSelect } from '@blueprintjs/select';
const INTENTS = [Intent.NONE, Intent.PRIMARY, Intent.SUCCESS, Intent.DANGER, Intent.WARNING];
type Indicator = {
name: string,
active: boolean,
height: number,
rank: number,
};
type Props = {
indicators: Array<Indicator>,
onUpdateIndicators: (Indicator, boolean) => void,
};
type State = {
indicators: Array<Indicator>,
};
export default class CustomMultiSelect extends React.PureComponent<Props, State> {
state = {
indicators: this.props.indicators,
};
// silence-error: couldn't resolve built-in Func params
itemRenderer(item: Object, { modifiers, handleClick }) {
return <MenuItem icon={item.active ? 'tick' : ''} onClick={handleClick} text={`${item.name}`} key={item.rank} />;
}
tagRenderer(item: Indicator) {
return item.name;
}
onItemSelect = (e: Indicator) => {
const { indicators } = this.state;
const active = !indicators[e.rank].active;
this.props.onUpdateIndicators(e, active);
this.setState({
indicators: [...indicators.slice(0, e.rank), { ...indicators[e.rank], active }, ...indicators.slice(e.rank + 1)],
});
};
findTag = (tagName: string) => {
let foundIndicator = {};
const { indicators } = this.state;
indicators.map((indicator, index) => {
if (indicator.name === tagName) {
foundIndicator['indicator'] = indicator;
foundIndicator['index'] = index;
}
return indicator;
});
return foundIndicator;
};
handleTagRemove = (tag: string) => {
const { indicators } = this.state;
let { indicator: changedIndicator, index } = this.findTag(tag);
this.props.onUpdateIndicators(indicators[index], false);
changedIndicator.active = false;
this.setState({
indicators: [...indicators.slice(0, index), changedIndicator, ...indicators.slice(index + 1)],
});
};
getSelectedItem = () => {
const { indicators } = this.state;
return indicators.filter(item => item.active);
};
render() {
const {
state: { indicators },
getSelectedItem,
onItemSelect,
itemRenderer,
tagRenderer,
} = this;
const getTagProps = (value: string, index: number) => ({
intent: INTENTS[index % INTENTS.length],
minimal: true,
});
return (
<MultiSelect
tagInputProps={{ tagProps: getTagProps, placeholder: 'Indicators', onRemove: this.handleTagRemove }}
popoverProps={{ minimal: true }}
itemRenderer={itemRenderer}
tagRenderer={tagRenderer}
items={indicators}
resetOnSelect={true}
selectedItems={getSelectedItem()}
onItemSelect={onItemSelect}
/>
);
}
}