This repository has been archived by the owner on Mar 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 68
/
switch.js
120 lines (105 loc) · 2.79 KB
/
switch.js
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
import React from 'react';
import PropTypes from 'prop-types';
import { SwitchSizes, SwitchInputTypes } from '../enums';
import { GeneralPropTypes, FlexboxPropTypes, createClassName, generalClassNames, removeProps, objectValues } from '../utils';
let currentId = 0;
/**
* Switch component.
* http://foundation.zurb.com/sites/docs/switch.html
*
* @param {Object} props
* @returns {Object}
*/
export const Switch = props => {
const className = createClassName(
props.noDefaultClassName ? null : 'switch',
props.className,
props.size,
generalClassNames(props)
);
const switchId = props.id || `switch${currentId++}`;
// TODO: Consider refactoring this, the rendering a little bit messy...
return (
<div {...removeProps(props, ['id'])} className={className}>
<SwitchInput {...props.input} id={switchId}/>
<SwitchPaddle {...props.paddle} htmlFor={switchId}>
{props.active ? <SwitchActive {...props.active}/> : null}
{props.inactive ? <SwitchInactive {...props.inactive}/> : null}
</SwitchPaddle>
</div>
);
};
Switch.propTypes = {
...GeneralPropTypes,
...FlexboxPropTypes,
size: PropTypes.oneOf(objectValues(SwitchSizes)),
id: PropTypes.string
};
/**
* Switch input sub-component.
*
* @param {Object} props
* @returns {Object}
*/
export const SwitchInput = props => {
const className = createClassName(
props.noDefaultClassName ? null : 'switch-input',
props.className,
generalClassNames(props)
);
return (
<input {...removeProps(props, ['type'])} className={className} type={props.type || SwitchInputTypes.CHECKBOX}/>
);
};
SwitchInput.propTypes = {
type: PropTypes.oneOf(objectValues(SwitchInputTypes)),
...GeneralPropTypes
};
/**
* Switch paddle sub-component.
*
* @param {Object} props
* @returns {Object}
*/
export const SwitchPaddle = props => {
const className = createClassName(
props.noDefaultClassName ? null : 'switch-paddle',
props.className,
generalClassNames(props)
);
return (
<label {...props} className={className}/>
);
};
/**
* Switch active sub-component.
*
* @param {Object} props
* @returns {Object}
*/
export const SwitchActive = props => {
const className = createClassName(
props.noDefaultClassName ? null : 'switch-active',
props.className,
generalClassNames(props)
);
return (
<span {...props} className={className} aria-hidden="true">{props.text}</span>
);
};
/**
* Switch inactive sub-component.
*
* @param {Object} props
* @returns {Object}
*/
export const SwitchInactive = props => {
const className = createClassName(
props.noDefaultClassName ? null : 'switch-inactive',
props.className,
generalClassNames(props)
);
return (
<span {...props} className={className} aria-hidden="true">{props.text}</span>
);
};