Rift.IO OSM R1 Initial Submission
[osm/UI.git] / skyquake / framework / widgets / text-input / check-box / rw.check-box2.js
1
2 /*
3 *
4 * Copyright 2016 RIFT.IO Inc
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *
18 */
19
20 var React = require('react');
21 var ButtonEventListenerMixin = require('../mixins/ButtonEventListener.js');
22
23
24 /**
25 * A check-box component.
26 * It's props values and a brief description below
27 *
28 * label: The label for the checkbox group.
29 * checkboxes: The object that creates each checkbox. Each object has a property "label" and "checked".
30 * label: The label for the individual checkbox.
31 * checked: If set to true, the individual checkbox is initialized with a check.
32 * requiredText: The text content of the "if required" message.
33 * errorText: The text content of the error message.
34 * ClassName: Css Classes applied to the element.
35 * size: The size of the element.
36 * isRequired: A boolean indicating whether or not the input is required.
37 * isDisabled: A boolean indicating the disabled state of the element.
38 * isReadOnly: A boolean indicating whether or not the input is read only.
39 * instructions: The text content of the instructions
40 **/
41 module.exports = React.createClass({
42 displayName: "CheckBox",
43 mixins:[ButtonEventListenerMixin],
44 propTypes: {
45 children: React.PropTypes.arrayOf(React.PropTypes.object),
46 label: React.PropTypes.string,
47 checked: React.PropTypes.bool,
48 indeterminate: React.PropTypes.bool,
49 requiredText: React.PropTypes.string,
50 instructionText: React.PropTypes.string,
51 errorText: React.PropTypes.string,
52 className: React.PropTypes.string,
53 size: React.PropTypes.string,
54 isRequired: React.PropTypes.bool,
55 isDisabled: React.PropTypes.bool,
56 isReadOnly: React.PropTypes.bool
57 },
58
59
60 /**
61 * Sets the default input state.
62 * If there is no description for the variable, assume it's the same as it's props counterpart.
63 *
64 * isActive: Boolean to indicate if input is active.
65 * isHovered: Boolean to indicate if the input is being hovered over.
66 * isFocused: Boolean to indicate if the input has been focused.
67 * isDisabled: Boolean to indicate if input has been disabled.
68 *
69 * @returns {{sizeOfButton: (*|string), isActive: boolean, isHovered: boolean, isFocused: boolean, isDisabled: (*|boolean),
70 * isReadOnly: (*|.textInput.isReadOnly|.exports.propTypes.isReadOnly|.exports.getInitialState.isReadOnly|boolean),
71 * isRequired: (*|.textInput.isRequired|.exports.propTypes.isRequired|.exports.getInitialState.isRequired|isRequired|null),
72 * isValid: null, isSuccess: null}}
73 */
74 getInitialState: function() {
75
76 return {
77 //mounted: false,
78 label: this.props.label || "",
79 requiredText: this.props.requiredText || "Required",
80 instructionsText: this.props.instructions || "",
81 errorText: this.props.errorText || "",
82 size: this.props.size || '',
83 isActive: false,
84 isHovered: false,
85 isFocused: false,
86 isDisabled: this.props.disabled || false,
87 isReadOnly: this.props.isReadOnly || false,
88 isRequired: this.props.isRequired || null,
89 isValid: null, // Three way bool. Valid: true. Invalid: false. Not acted on: null.
90 isSuccess: null // Three way bool. Success: true. Error: false. Not acted on: null.
91 }
92 },
93
94 /**
95 * If any of the state variables have changed, the component should update.
96 * "nextProps" and "nextState" hold the state and property variables as they will be after the update.
97 * "this.props" and "this.state" hold the state and property variables as they were before the update.
98 * returns true if the state have changed. Otherwise returns false.
99 * @param nextProps
100 * @param nextState
101 * @returns {boolean}
102 */
103 shouldComponentUpdate: function(nextProps, nextState) {
104 var currentStateString = this.state.isReadOnly + this.state.isDisabled + this.state.isActive + this.state.isFocused +
105 this.state.isHovered + this.state.isValid + this.state.isSuccess;
106 var nextStateString = nextState.isReadOnly + nextState.isDisabled + nextState.isActive + nextState.isFocused +
107 nextState.isHovered + nextState.isValid + nextState.isSuccess;
108 if (currentStateString == nextStateString) {
109 return false;
110 }
111 return true;
112 },
113
114 componentDidMount: function() {
115 this.state.mounted = true;
116 this.render();
117 },
118
119 /**
120 * Returns a string reflecting the current state of the input.
121 * If the component state "isDisabled" is true, returns a string "disabled".
122 * If the component state "isReadOnly" is true, returns a string "readonly".
123 * Otherwise returns a string containing a word for each state that has been set to true.
124 * (ie "active focused" if the states active and focused are true, but hovered is false).
125 * @returns {string}
126 */
127 setComponentState: function() {
128
129 var ret = "";
130 if (this.state.isDisabled) {
131 return "disabled";
132 }
133 if (this.state.isReadOnly) {
134 return "readonly";
135 }
136 if (this.state.isActive) {
137 ret += "active ";
138 }
139 if (this.state.isHovered) {
140 ret += "hovered ";
141 }
142 if (this.state.isFocused) {
143 ret += "focused ";
144 }
145 return ret;
146 },
147
148 localOnClick: function(e) {
149 this.onClick(e);
150 },
151
152 /**
153 * Renders the checkbox component.
154 *
155 **/
156 render: function() {
157 var input = [];
158 var checkbox = [];
159 var label = [];
160 var input_style = {};
161 var input_state = this.setComponentState();
162
163 // The group label element
164 label = React.createElement("label", {className: "rw-form__label"}, this.props.label);
165
166 // Creates each individual checkbox element and the label for each.
167 for (var i = 0; i < this.props.checkboxes.length; i++) {
168 checkbox[i] = React.createElement("input",{
169 key: i,
170 defaultChecked: this.props.checkboxes[i].checked,
171 //indeterminate: true,
172 type: "checkbox",
173 readOnly: this.props.readonly,
174 disabled: this.props.disabled,
175 onClick: this.localOnClick,
176 onMouseUp: this.onMouseUp,
177 onMouseDown: this.onMouseDown,
178 onMouseOver: this.onMouseOver,
179 onMouseEnter: this.onMouseEnter,
180 onMouseLeave: this.onMouseLeave,
181 onMouseOut: this.onMouseOut,
182 onTouchCancel: this.onTouchCancel,
183 onTouchEnd: this.onTouchEnd,
184 onTouchMove: this.onTouchMove,
185 onTouchStart: this.onTouchStart,
186 onKeyDown: this.onKeyDown,
187 onKeyPress: this.onKeyPress,
188 onKeyUp: this.onKeyUp,
189 onFocus: this.onFocus,
190 onBlur: this.onBlur
191 }, null);
192 if (this.state.mounted) {
193 this.getDOMNode().children[i + 1].children[0].indeterminate = this.props.checkboxes[i].indeterminate;
194 }
195
196
197 input[i] = React.createElement("label", {className: "rw-form__label-checkBox", key:i, readOnly:this.props.readonly, disabled:this.props.disabled}, checkbox[i], this.props.checkboxes[i].label);
198
199 }
200
201 // The "if required" element. It displays a label if the element is required.
202 if(this.props.isRequired == true){
203 var requiredEle = React.createElement("small", {className: "rw-form__required-label"}, this.state.requiredText);
204 }
205
206 // The "error" element. It pops up as a message if there is an error with the input.
207 if(this.state.errorText != "") {
208 var error = React.createElement("p", {className: "rw-form__message-error"}, this.state.errorText);
209 }
210
211 // The "instruction" element.
212 if(this.state.instructionsText != ""){
213 var instructions = React.createElement("p", {className: "rw-form-instructions"}, this.state.instructionsText)
214 }
215
216 // The parent element for all.
217 var ret = React.createElement("div", {
218 "data-state": input_state,
219 required: this.state.isRequired,
220 disabled: this.state.isDisabled,
221 readOnly: this.state.isReadOnly,
222 className: "rw-form"
223
224 }, requiredEle, label, input, error, instructions);
225
226 return ret;
227 }
228 });