User Management Dashboard:
[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
78         if( props.readonly && props.type == "radiogroup" && props.readonlydisplay ) {
79             displayedValue = props.readonlydisplay
80         }
81
82         let html = (
83             <label className={className} style={props.style}>
84               <span> { label } {isRequired}</span>
85               {
86                 !props.readonly ? inputType : <div className="readonly">{displayedValue}</div>
87               }
88
89             </label>
90         );
91         return html;
92     }
93 }
94
95
96 function buildRadioButtons(props) {
97     let className = 'sqCheckBox';
98     return(
99        <div className={className}>
100             {
101                 props.options.map((o,i) => {
102                     let label = o.label || o;
103                     let value = o.value || o;
104                     return (
105                         <label key={i}>
106                             {label}
107                             <input type="radio" checked={props.value == value} value={value} onChange={props.onChange} />
108                         </label>
109                     )
110                 })
111             }
112        </div>
113
114     )
115 }
116
117 Input.defaultProps = {
118     onChange: function(e) {
119         console.log(e.target.value, e);
120         console.dir(e.target);
121     },
122     label: '',
123     defaultValue: null,
124     type: 'text',
125     readonly: false,
126     style:{},
127     className: ''
128
129 }
130