-
Notifications
You must be signed in to change notification settings - Fork 34
/
field.js
131 lines (114 loc) · 3.14 KB
/
field.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
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
import PropTypes from 'prop-types';
import React, { Component } from 'react';
import Consult from './consult';
import Edit from './edit';
/**
* Multi-autocomplete with Chip component.
*/
class MultiAutocomplete extends Component {
/**
* Component's display name.
*/
static displayName = 'MultiAutocomplete';
/**
* Component's prop types.
*/
static propTypes = {
value: PropTypes.arrayOf(PropTypes.number).isRequired,
isEdit: PropTypes.bool.isRequired,
keyResolver: PropTypes.func.isRequired,
querySearcher: PropTypes.func.isRequired,
onChange: PropTypes.func.isRequired,
checkDuplicate: PropTypes.bool,
ChipElement: PropTypes.any,
position: PropTypes.oneOf(['top', 'bottom'])
};
/**
* Component's default props.
*/
static defaultProps = {
checkDuplicate: false,
ChipElement: undefined,
position: 'top'
};
/**
* Class constructor.
* @param {object} props props.
*/
constructor(props) {
super(props);
this.getValue = this.getValue.bind(this);
this.validate = this.validate.bind(this);
this.renderEdit = this.renderEdit.bind(this);
this.renderConsult = this.renderConsult.bind(this);
}
/**
* @inheritdoc
*/
componentWillReceiveProps({ value }) {
this.setState({ value });
}
/**
* Returns value.
* @return {array} current value.
*/
getValue() {
return this.props.value;
}
/**
* Validate component state.
*/
validate() {
// do nothing, validation is done at each selection
const { isRequired } = this.props;
return ({
isValid: !isRequired || (this.props.value && this.props.value.length > 0),
message: 'field.required'
});
}
/**
* Render in consult mode.
* @return {ReactElement} markup.
*/
renderConsult() {
const { value, keyResolver, keyName, labelName } = this.props;
return (
<Consult
value={value}
keyResolver={keyResolver}
keyName={keyName}
labelName={labelName}
readonly
/>
);
}
/**
* Render in edit mode.
* @return {ReactElement} markup.
*/
renderEdit() {
const { keyResolver, querySearcher, onChange, checkDuplicate, position, ChipElement, keyName, labelName } = this.props;
return (
<Edit
value={this.props.value}
readonly={false}
keyResolver={keyResolver}
querySearcher={querySearcher}
onChange={onChange}
checkDuplicate={checkDuplicate}
position={position}
ChipElement={ChipElement}
keyName={keyName}
labelName={labelName}
/>
);
}
/**
* Render component.
* @return {ReactElement} markup.
*/
render() {
return this.props.isEdit ? this.renderEdit() : this.renderConsult();
}
}
export default MultiAutocomplete;