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 Button from '../Button'
20 import ClassNames from 'classnames'
21 import changeCase from 'change-case'
22 import Property from '../../libraries/model/DescriptorModelMetaProperty'
23 import { Input, Empty, Boolean, Reference, Enumeration } from './LeafEditor'
25 import '../../styles/EditDescriptorModelProperties.scss'
27 function buildEditor(container, property, path, value, readOnly, id, onChange, onError, onExit) {
28 const title = path.join('.');
31 if (Property.isEnumeration(property)) {
37 onChange={onChange} onError={onError} onExit={onExit}
40 } else if (Property.isLeafRef(property)) {
48 onChange={onChange} onError={onError} onExit={onExit}
51 } else if (Property.isBoolean(property)) {
57 onChange={onChange} onError={onError} onExit={onExit}
60 } else if (Property.isLeafEmpty(property)) {
66 onChange={onChange} onError={onError} onExit={onExit}
69 } else if (Property.name === 'meta') {
70 if (typeof value === 'object') {
71 value = JSON.stringify(value, undefined, 12);
72 } else if (typeof value !== 'string') {
81 onChange={onChange} onError={onError} onExit={onExit}
88 export default class LeafField extends React.Component {
91 this.state = { showHelp: props.showHelp, isInError: props.errorMessage };
93 componentWillReceiveProps(nextProps) {
94 const { showHelp, errorMessage } = nextProps
95 if (showHelp !== this.state.showHelp && !this.state.isInError) {
96 this.setState({ showHelp })
98 if (errorMessage !== this.state.errorMessage) {
99 this.setState({ showHelp: !!errorMessage || showHelp, isInError: errorMessage })
103 const { container, property, path, value, id, readOnly, onChange, onError } = this.props;
104 let title = changeCase.titleCase(property.name);
105 const showHelp = this.state.showHelp;
106 const description = property.description;
107 const helpText = this.state.isInError ? this.state.isInError + " " + description : description;
108 if (property.mandatory) {
109 title = "* " + title;
111 const errorHandler = (message) => {
112 this.setState({ showHelp: true, isInError: message });
114 const changeHandler = (value) => {
115 this.setState({ showHelp: this.props.showHelp, isInError: false });
116 onChange && onChange(value);
118 const exitHandler = (exitResult) => {
119 if (!exitResult.success) {
120 // errorHandler(exitResult.message);
121 onError && onError(exitResult.message);
125 const editor = buildEditor(
126 container, property, path, value, readOnly, id,
127 changeHandler, errorHandler, exitHandler);
129 display: showHelp ? 'inline-block' : 'none',
131 color: this.state.isInError ? 'red' : 'inherit'
134 <div className={ClassNames('leaf-property -is-leaf property')}>
135 <h3 className="property-label">
137 <span className={'leaf-name name info'}>{title}</span>
140 <val className="property-value">
141 <div className={ClassNames('property-content')}>
145 <span className={'leaf-description description'}
146 style={helpStyle}>{helpText}</span>