1 import React from 'react';
2 import i18n from 'nfvo-utils/i18n/i18n.js';
3 import classNames from 'classnames';
4 import Select from 'nfvo-components/input/SelectInput.jsx';
6 export const other = {OTHER: 'Other'};
8 class InputOptions extends React.Component {
11 values: React.PropTypes.arrayOf(React.PropTypes.shape({
12 enum: React.PropTypes.string,
13 title: React.PropTypes.string
15 isEnabledOther: React.PropTypes.bool,
16 title: React.PropTypes.string,
17 selectedValue: React.PropTypes.string,
18 multiSelectedEnum: React.PropTypes.array,
19 selectedEnum: React.PropTypes.string,
20 otherValue: React.PropTypes.string,
21 onEnumChange: React.PropTypes.func,
22 onOtherChange: React.PropTypes.func,
23 isRequired: React.PropTypes.bool,
24 isMultiSelect: React.PropTypes.bool
28 static contextTypes = {
29 isReadOnlyMode: React.PropTypes.bool
33 otherInputDisabled: !this.props.otherValue
43 let {label, isRequired, values, otherValue, onOtherChange, isMultiSelect, onBlur, multiSelectedEnum, selectedEnum, hasError, validations, children} = this.props;
45 let currentMultiSelectedEnum = [];
46 let currentSelectedEnum = '';
47 let {otherInputDisabled} = this.state;
49 currentMultiSelectedEnum = multiSelectedEnum;
50 if(!otherInputDisabled) {
51 currentSelectedEnum = multiSelectedEnum ? multiSelectedEnum.toString() : undefined;
54 else if(selectedEnum){
55 currentSelectedEnum = selectedEnum;
58 let isReadOnlyMode = this.context.isReadOnlyMode;
61 <div className={classNames('form-group', {'required' : validations.required , 'has-error' : hasError})}>
62 {label && <label className='control-label'>{label}</label>}
63 {isMultiSelect && otherInputDisabled ?
66 value={currentMultiSelectedEnum}
67 className='options-input'
70 disabled={isReadOnlyMode || Boolean(this.props.disabled)}
71 onBlur={() => onBlur()}
72 onMultiSelectChanged={value => this.multiSelectEnumChanged(value)}
73 options={this.renderMultiSelectOptions(values)}
75 <div className={classNames('input-options',{'has-error' : hasError})}>
79 className='form-control input-options-select'
80 value={currentSelectedEnum}
81 style={{'width' : otherInputDisabled ? '100%' : '95px'}}
82 onBlur={() => onBlur()}
83 disabled={isReadOnlyMode || Boolean(this.props.disabled)}
84 onChange={ value => this.enumChanged(value)}
86 {values && values.length && values.map(val => this.renderOptions(val))}
87 {onOtherChange && <option key='other' value={other.OTHER}>{i18n(other.OTHER)}</option>}
91 {!otherInputDisabled && <div className='input-options-separator'/>}
93 className='form-control input-options-other'
94 placeholder={i18n('other')}
96 style={{'display' : otherInputDisabled ? 'none' : 'block'}}
97 disabled={isReadOnlyMode || Boolean(this.props.disabled)}
98 value={otherValue || ''}
99 onBlur={() => onBlur()}
100 onChange={() => this.changedOtherInput()}/>
109 <option key={val.enum} value={val.enum}>{val.title}</option>
114 renderMultiSelectOptions(values) {
115 let {onOtherChange} = this.props;
116 let optionsList = [];
118 optionsList = values.map(option => {
124 label: i18n(other.OTHER),
125 value: i18n(other.OTHER),
129 optionsList = values.map(option => {
136 if (optionsList.length > 0 && optionsList[0].value === '') {
144 let {isMultiSelect} = this.props;
145 let {otherInputDisabled} = this.state;
147 if (otherInputDisabled) {
148 res = isMultiSelect ? this.refs._myInput.getValue() : this.refs._myInput.value;
150 res = this.refs._otherValue.value;
156 let enumValue = this.refs._myInput.value;
157 let {onEnumChange, isMultiSelect, onChange} = this.props;
159 otherInputDisabled: enumValue !== other.OTHER
162 let value = isMultiSelect ? [enumValue] : enumValue;
171 multiSelectEnumChanged(enumValue) {
172 let {onEnumChange} = this.props;
173 let selectedValues = enumValue.map(enumVal => {
174 return enumVal.value;
177 if (this.state.otherInputDisabled === false) {
178 selectedValues.shift();
180 else if (selectedValues.includes(i18n(other.OTHER))) {
181 selectedValues = [i18n(other.OTHER)];
185 otherInputDisabled: !selectedValues.includes(i18n(other.OTHER))
187 onEnumChange(selectedValues);
190 changedOtherInput() {
191 let {onOtherChange} = this.props;
192 onOtherChange(this.refs._otherValue.value);
195 componentDidUpdate() {
196 let {otherValue, selectedEnum, onInputChange, multiSelectedEnum} = this.props;
197 if (this.oldProps.otherValue !== otherValue
198 || this.oldProps.selectedEnum !== selectedEnum
199 || this.oldProps.multiSelectedEnum !== multiSelectedEnum) {
209 static getTitleByName(values, name) {
210 for (let key of Object.keys(values)) {
211 let option = values[key].find(option => option.enum === name);
221 export default InputOptions;