Merge branch 'master' into projects
[osm/UI.git] / skyquake / framework / widgets / form_controls / input.jsx
1 /*
2  *
3  *   Copyright 2016 RIFT.IO Inc
4  *
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
8  *
9  *       http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  */
18 import './formControls.scss';
19 import SelectOption from 'widgets/form_controls/selectOption.jsx';
20 import CircleSVG from '../../../node_modules/open-iconic/svg/media-record.svg'
21 import React, {Component} from 'react';
22
23 export default class Input extends Component {
24     render() {
25         let {label, value, defaultValue, ...props} = this.props;
26         let inputProperties = {
27             value: value
28         }
29         let isRequired;
30         let inputType;
31         let className = `sqTextInput ${props.className}`;
32
33         if(this.props.required) {
34            isRequired = <span className="required">*</span>
35         }
36         if (defaultValue) {
37             inputProperties.defaultValue = defaultValue;
38         }
39         if (props.pattern) {
40             inputProperties.pattern = props.pattern;
41         }
42         if(props.hasOwnProperty('type') && (props.type.toLowerCase() == 'checkbox')) {
43             inputProperties.checked = props.checked;
44             className = `${className} checkbox`;
45         }
46         if (value == undefined) {
47             value = defaultValue;
48         }
49         switch(props.type) {
50             case 'textarea':
51                 inputType = <textarea key={props.key} {...inputProperties} value={value} onChange={props.onChange} />
52                 break;
53             case 'select':
54                 inputType = <SelectOption
55                                 key={props.key}
56                                 initial={props.initial}
57                                 defaultValue={defaultValue}
58                                 options={props.options}
59                                 onChange={props.onChange}
60                             />
61                 break;
62             case 'radiogroup':
63                 inputType = buildRadioButtons(this.props);
64                 break;
65             default:
66                 inputType = <input key={props.key} type={props.type} {...inputProperties} onChange={props.onChange} placeholder={props.placeholder}/>;
67         }
68         let displayedValue;
69         if(value === null) {
70             displayedValue = null;
71         } else {
72             displayedValue = value.toString();
73         }
74         if( props.readonly && props.type == "checkbox" && props.checked ) {
75             displayedValue = <img src={CircleSVG} />
76         }
77         let html = (
78             <label className={className} style={props.style}>
79               <span> { label } {isRequired}</span>
80               {
81                 !props.readonly ? inputType : <div className="readonly">{displayedValue}</div>
82               }
83
84             </label>
85         );
86         return html;
87     }
88 }
89
90
91 function buildRadioButtons(props) {
92     let className = 'sqCheckBox';
93     if (props.className) {
94         className = `${className} ${props.className}`;
95     }
96     return(
97        <div className={className}>
98             {
99                 props.options.map((o,i) => {
100                     let label = o.label || o;
101                     let value = o.value || o;
102                     return (
103                         <label key={i}>
104                             {label}
105                             <input type="radio" checked={props.value == value} value={value} onChange={props.onChange} />
106                         </label>
107                     )
108                 })
109             }
110        </div>
111
112     )
113 }
114
115 Input.defaultProps = {
116     onChange: function(e) {
117         console.log(e.target.value, e);
118         console.dir(e.target);
119     },
120     label: '',
121     defaultValue: null,
122     type: 'text',
123     readonly: false,
124     style:{},
125     className: ''
126
127 }
128