-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathIDGeneratorInput.jsx
192 lines (165 loc) · 4.17 KB
/
IDGeneratorInput.jsx
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import Row from 'cspace-layout/lib/components/Row';
import BaseDropdownInput from './DropdownInput';
import LineInput from './LineInput';
import Menu from './Menu';
import changeable from '../enhancers/changeable';
import committable from '../enhancers/committable';
import { getPath } from '../helpers/pathHelpers';
import styles from '../../styles/cspace-input/IDGeneratorInput.css';
const DropdownInput = committable(changeable(BaseDropdownInput));
const propTypes = {
// TODO: Stop using propTypes in isInput, and in render method of cspace-ui Field component.
// Until then, propTypes need to be hoisted from the base component.
// eslint-disable-next-line react/forbid-foreign-prop-types
...DropdownInput.propTypes,
patterns: PropTypes.arrayOf(PropTypes.shape({
name: PropTypes.string,
type: PropTypes.string,
sample: PropTypes.string,
})),
generateID: PropTypes.func,
sampleColumnLabel: PropTypes.string,
typeColumnLabel: PropTypes.string,
readOnly: PropTypes.bool,
onMount: PropTypes.func,
onOpen: PropTypes.func,
};
const defaultProps = {
patterns: [],
generateID: undefined,
sampleColumnLabel: 'Sample',
typeColumnLabel: 'Type',
readOnly: undefined,
onMount: undefined,
onOpen: undefined,
};
const renderItemLabel = (label) => (
<Row>
<div>{label[0]}</div>
<div>{label[1]}</div>
</Row>
);
export default class IDGeneratorInput extends Component {
constructor(props) {
super(props);
this.focusMenu = this.focusMenu.bind(this);
this.handleDropdownInputClose = this.handleDropdownInputClose.bind(this);
this.handleDropdownInputMount = this.handleDropdownInputMount.bind(this);
this.handleDropdownInputOpen = this.handleDropdownInputOpen.bind(this);
this.handleMenuRef = this.handleMenuRef.bind(this);
this.handleMenuSelect = this.handleMenuSelect.bind(this);
this.state = {
open: false,
};
}
componentDidMount() {
const {
onMount,
} = this.props;
if (onMount) {
onMount();
}
}
handleDropdownInputMount({ focusInput }) {
this.focusInput = focusInput;
}
handleDropdownInputClose() {
this.setState({
open: false,
});
}
handleDropdownInputOpen() {
const {
onOpen,
patterns,
} = this.props;
if (onOpen) {
onOpen(patterns);
}
this.setState({
open: true,
});
}
handleMenuRef(ref) {
this.menu = ref;
}
handleMenuSelect(option) {
this.setState({
open: false,
});
this.focusInput();
const {
generateID,
} = this.props;
if (generateID) {
generateID(option.value, getPath(this.props));
}
}
focusMenu() {
if (this.menu) {
this.menu.focus();
}
}
render() {
const {
open,
} = this.state;
const {
generateID,
onMount,
onOpen,
patterns,
sampleColumnLabel,
typeColumnLabel,
readOnly,
...remainingProps
} = this.props;
const {
embedded,
value,
} = remainingProps;
if (readOnly) {
return (
<LineInput
embedded={embedded}
readOnly
value={value}
/>
);
}
const options = patterns.map((pattern) => ({
value: pattern.name,
label: [pattern.type, pattern.sample],
}));
return (
<DropdownInput
// eslint-disable-next-line react/jsx-props-no-spreading
{...remainingProps}
className={styles.normal}
focusPopup={this.focusMenu}
open={open}
onClose={this.handleDropdownInputClose}
onMount={this.handleDropdownInputMount}
onOpen={this.handleDropdownInputOpen}
>
<header>
<Row>
<div>{typeColumnLabel}</div>
<div>{sampleColumnLabel}</div>
</Row>
</header>
<Menu
options={options}
ref={this.handleMenuRef}
renderItemLabel={renderItemLabel}
tabIndex="-1"
onSelect={this.handleMenuSelect}
/>
</DropdownInput>
);
}
}
IDGeneratorInput.propTypes = propTypes;
IDGeneratorInput.defaultProps = defaultProps;