forked from parse-community/parse-dashboard
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathToggle.react.js
More file actions
141 lines (134 loc) · 4.86 KB
/
Toggle.react.js
File metadata and controls
141 lines (134 loc) · 4.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/*
* Copyright (c) 2016-present, Parse, LLC
* All rights reserved.
*
* This source code is licensed under the license found in the LICENSE file in
* the root directory of this source tree.
*/
import { input } from 'components/Field/Field.scss';
import PropTypes from 'lib/PropTypes';
import React from 'react';
import styles from 'components/Toggle/Toggle.scss';
import { unselectable } from 'stylesheets/base.scss';
export default class Toggle extends React.Component {
toLeft() {
if (this.props.type === Toggle.Types.TWO_WAY || this.props.type === Toggle.Types.CUSTOM) {
this.props.onChange(this.props.optionLeft);
} else {
this.props.onChange(false);
}
}
toRight() {
if (this.props.type === Toggle.Types.TWO_WAY || this.props.type === Toggle.Types.CUSTOM) {
this.props.onChange(this.props.optionRight);
} else {
this.props.onChange(true);
}
}
toggle() {
if (this.props.type === Toggle.Types.TWO_WAY || this.props.type === Toggle.Types.CUSTOM) {
if (this.props.value === this.props.optionLeft) {
this.props.onChange(this.props.optionRight);
} else {
this.props.onChange(this.props.optionLeft);
}
} else {
this.props.onChange(!this.props.value);
}
}
render() {
let type = this.props.type;
let labelLeft = '';
let labelRight = '';
let colored = false;
let left = false;
switch (type) {
case Toggle.Types.ON_OFF:
labelLeft = 'Off';
labelRight = 'On';
colored = true;
left = !this.props.value;
break;
case Toggle.Types.TRUE_FALSE:
labelLeft = 'False';
labelRight = 'True';
colored = true;
left = !this.props.value;
break;
case Toggle.Types.TWO_WAY:
if (!this.props.optionLeft || !this.props.optionRight) {
throw new Error(
'TWO_WAY toggle must provide optionLeft and optionRight props.'
);
}
labelLeft = this.props.optionLeft;
labelRight = this.props.optionRight;
left = this.props.value === labelLeft;
break;
case Toggle.Types.CUSTOM:
if (!this.props.optionLeft || !this.props.optionRight || !this.props.labelLeft || !this.props.labelRight) {
throw new Error(
'CUSTOM toggle must provide optionLeft, optionRight, labelLeft, and labelRight props.'
);
}
labelLeft = this.props.labelLeft;
labelRight = this.props.labelRight;
left = this.props.value === this.props.optionLeft;
colored = this.props.colored;
break;
case Toggle.Types.HIDE_LABELS:
colored = true;
left = !this.props.value;
break;
default:
labelLeft = 'No';
labelRight = 'Yes';
colored = true;
left = !this.props.value;
break;
}
let switchClasses = [styles.switch];
if (colored) {
switchClasses.push(styles.colored);
}
if (this.props.switchNoMargin) {
switchClasses.push(styles.switchNoMargin);
}
let toggleClasses = [styles.toggle, unselectable, input];
if (left) {
toggleClasses.push(styles.left);
}
if (this.props.darkBg) {
toggleClasses.push(styles.darkBg);
}
return (
<div className={toggleClasses.join(' ')} style={this.props.additionalStyles || {}}>
{labelLeft && <span className={styles.label} onClick={this.toLeft.bind(this)}>{labelLeft}</span>}
<span className={switchClasses.join(' ')} onClick={this.toggle.bind(this)}></span>
{labelRight && <span className={styles.label} onClick={this.toRight.bind(this)}>{labelRight}</span>}
</div>
);
}
}
Toggle.propTypes = {
value: PropTypes.oneOfType([
PropTypes.string,
PropTypes.bool
]),
type: PropTypes.number.describe('Controls the words that appear beside the toggle. Default is Toggle.Types.YES_NO. Other options are Toggle.Types.TRUE_FALSE, Toggle.Types.ON_OFF, Toggle.Types.TWO_WAY or Toggle.Types.CUSTOM. If using TWO_WAY, supply your own text using optionLeft and optionRight. If using CUSTOM, supply your own text using labelLeft and labelRight, supply your own values using optionLeft and optionRight.'),
onChange: PropTypes.func.isRequired,
optionleft: PropTypes.string,
optionRight: PropTypes.string,
labelLeft: PropTypes.string.describe('Custom left toggle label, case when label does not equal content. [For Toggle.Type.CUSTOM]'),
labelRight: PropTypes.string.describe('Custom right toggle label, case when label does not equal content. [For Toggle.Type.CUSTOM]'),
colored: PropTypes.bool.describe('Flag describing is toggle is colored. [For Toggle.Type.CUSTOM]'),
darkBg: PropTypes.bool,
additionalStyles: PropTypes.object.describe('Additional styles for Toggle component.'),
};
Toggle.Types = {
YES_NO: 1,
TRUE_FALSE: 2,
ON_OFF: 3,
TWO_WAY: 4,
CUSTOM: 5,
};