3 * Copyright 2017 RIFT.IO Inc
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
18 import React from 'react';
19 import ClassNames from 'classnames'
20 import changeCase from 'change-case'
21 import yang from '../../yang/leaf-utils.js'
22 import { Input, Empty, Boolean, Reference, Enumeration } from './LeafEditor'
24 // import '../../styles/EditDescriptorModelProperties.scss'
26 function buildEditor(model, path, property, value, readOnly, id, onChange, onError, onExit) {
27 const title = path.join('.');
30 id = id || property.name;
31 if (yang.isEnumeration(property)) {
37 onChange={onChange} onError={onError} onExit={onExit}
40 } else if (yang.isLeafRef(property)) {
48 onChange={onChange} onError={onError} onExit={onExit}
51 } else if (yang.isBoolean(property)) {
57 onChange={onChange} onError={onError} onExit={onExit}
60 } else if (yang.isLeafEmpty(property)) {
66 onChange={onChange} onError={onError} onExit={onExit}
75 onChange={onChange} onError={onError} onExit={onExit}
82 export default class LeafField extends React.Component {
85 this.state = { showHelp: props.showHelp, isInError: props.errorMessage };
87 componentWillReceiveProps(nextProps) {
88 const { showHelp, errorMessage } = nextProps
89 if (showHelp !== this.state.showHelp && !this.state.isInError) {
90 this.setState({ showHelp })
92 if (errorMessage !== this.state.errorMessage) {
93 this.setState({ showHelp: !!errorMessage || showHelp, isInError: errorMessage })
97 const { model, path, property, value, id, readOnly, extraHelp, onChange, onError } = this.props;
98 let title = changeCase.titleCase(property.name);
99 const showHelp = this.state.showHelp;
100 const description = (extraHelp ? extraHelp + ' ' : '') + property.description;
101 const helpText = this.state.isInError ? this.state.isInError + " " + description : description;
102 if (property.mandatory && !readOnly) {
103 title = "* " + title;
105 const errorHandler = (message) => {
106 this.setState({ showHelp: true, isInError: message });
108 const changeHandler = (value) => {
109 this.setState({ showHelp: this.props.showHelp, isInError: false });
110 onChange && onChange(value);
112 const exitHandler = (exitResult) => {
113 if (!exitResult.success) {
114 // errorHandler(exitResult.message);
115 onError && onError(exitResult.message);
119 const editor = buildEditor(
120 model, path, property, value, readOnly, id,
121 changeHandler, errorHandler, exitHandler);
123 display: showHelp ? 'inline-block' : 'none',
126 color: this.state.isInError ? 'red' : 'darkgray',
130 <div className={ClassNames('leaf-property -is-leaf property')} style={{paddingBottom: '10px'}} >
131 <h3 className="property-label">
133 <span className={'leaf-name name info'}>{title}</span>
136 <div className={ClassNames('property-content')}>
139 <span className={'leaf-description description'}
140 style={helpStyle}>{helpText}</span>