-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathindex.es6.js
More file actions
87 lines (71 loc) · 2.2 KB
/
index.es6.js
File metadata and controls
87 lines (71 loc) · 2.2 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
import * as React from "react";
import PropTypes from "prop-types";
import classNames from "classnames";
const MultiToggle = ({
selectedOption,
options,
onSelectOption,
className,
label
}) => {
// If required variables aren't passed, return empty
if (!options || selectedOption === null) return null;
const numOptions = options.length;
const columnWidth = numOptions ? 100 / numOptions : numOptions;
const isSelectedOption = option => option.value == selectedOption;
const getSelectedIndex = () => {
const indexFound = options.findIndex(option => isSelectedOption(option));
return indexFound > -1 ? indexFound : 0;
};
const createToggleOption = (...args) => {
const { value, displayName, isDisabled } = args[0];
const selectOption = () => onSelectOption(value);
const optionClass = classNames("toggleOption", {
selected: isSelectedOption(args[0]),
optionDisabled: isDisabled
});
const optionStyle = {
width: `${columnWidth}%`
};
return (
<div
key={args[1]}
onClick={isDisabled ? null : selectOption}
className={optionClass}
style={optionStyle}
>
{displayName || value}
</div>
);
};
const toggleClass = classNames("toggleContainer", className);
const toggleStyle = {
width: `${columnWidth}%`,
transform: `translateX(${100 * getSelectedIndex()}%)`,
WebkitTransform: `translateX(${100 * getSelectedIndex()}%)`,
MozTransform: `translateX(${100 * getSelectedIndex()}%)`,
msTransform: `translateX(${100 * getSelectedIndex()}%)`
};
const selectedToggleClass = classNames(
"toggle",
options[getSelectedIndex()].optionClass
);
const renderLabel = label ? <label>{label}</label> : null;
return (
<div className="toggle-wrapper">
{renderLabel}
<div className={toggleClass}>
{options.map(createToggleOption)}
<div className={selectedToggleClass} style={toggleStyle} />
</div>
</div>
);
};
MultiToggle.propTypes = {
className: PropTypes.any,
options: PropTypes.array.isRequired,
selectedOption: PropTypes.any.isRequired,
onSelectOption: PropTypes.func,
label: PropTypes.any
};
export default MultiToggle;