Skip to content

Commit e7808e3

Browse files
authored
[Switch] [TextField] Changes for Preact (#16)
* [Switch] [TextField] refactor for better Preact support
1 parent 6496dc2 commit e7808e3

File tree

2 files changed

+48
-12
lines changed

2 files changed

+48
-12
lines changed

src/Switch/Switch.js

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ class Switch extends React.Component {
99
this.onBlur = this.onBlur.bind(this);
1010
this.onMouseUp = this.onMouseUp.bind(this);
1111
this.onKeyUp = this.onKeyUp.bind(this);
12+
this.getTrackColor = this.getTrackColor.bind(this);
13+
this.getThumbColor = this.getThumbColor.bind(this);
1214
this.state = {
1315
keyboardFocused: false
1416
};
@@ -37,6 +39,28 @@ class Switch extends React.Component {
3739
this.input = c;
3840
}
3941

42+
getTrackColor = () => {
43+
const {checked, disabled, primaryColor} = this.props;
44+
if (disabled) {
45+
return '#bdbdbd';
46+
}
47+
if (checked) {
48+
return primaryColor || '#2196f3';
49+
}
50+
return '#000';
51+
}
52+
53+
getThumbColor = () => {
54+
const {checked, disabled, primaryColor} = this.props;
55+
if (disabled && checked) {
56+
return '#bdbdbd';
57+
}
58+
if (checked) {
59+
return primaryColor || '#2196f3';
60+
}
61+
return '#FFF';
62+
}
63+
4064
render() {
4165
const {
4266
checked,
@@ -72,17 +96,13 @@ class Switch extends React.Component {
7296
/>
7397
<div
7498
className={Styles.track}
75-
style={{
76-
backgroundColor: checked && !disabled && primaryColor
77-
}}
99+
style={{backgroundColor: this.getTrackColor()}}
78100
/>
79101
<div
80102
className={makeClass(Styles.thumb, {
81103
[Styles.thumbKeyboardFocus]: keyboardFocused
82104
})}
83-
style={{
84-
backgroundColor: checked && !disabled && primaryColor
85-
}}
105+
style={{backgroundColor: this.getThumbColor()}}
86106
/>
87107
</div>
88108
<label id={labelId} className={Styles.label}>{label}</label>

src/TextField/TextField.js

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ class TextField extends React.Component {
4646

4747
componentWillUnmount() {
4848
window.removeEventListener('resize', this.fixHeight);
49+
this.label.style.animationName = '';
4950
}
5051

5152
onBlur() {
@@ -92,6 +93,19 @@ class TextField extends React.Component {
9293
this.label = c;
9394
}
9495

96+
getColor = () => {
97+
const fallback = 'rgba(0,0,0,.54)';
98+
const {errorColor, primaryColor} = this.props;
99+
const {focused} = this.state;
100+
if (errorColor) {
101+
return errorColor;
102+
}
103+
if (focused) {
104+
return primaryColor || fallback;
105+
}
106+
return fallback;
107+
}
108+
95109
render() {
96110
const {
97111
errorColor,
@@ -105,7 +119,6 @@ class TextField extends React.Component {
105119
width,
106120
...other
107121
} = this.props;
108-
const {focused} = this.state;
109122
const FormComponent = multiline ? 'textarea' : 'input';
110123
const notEmpty = value && value.length > 0;
111124
return (
@@ -139,17 +152,20 @@ class TextField extends React.Component {
139152
ref={this.registerLabel}
140153
className={Styles.label}
141154
id={labelId}
142-
style={{
143-
color: errorColor || (focused && primaryColor)
144-
}}
155+
style={{color: this.getColor()}}
145156
>
146157
{label}
147158
</label>
148159
<div
149160
className={Styles.inkbar}
150-
style={{borderBottomColor: errorColor || (focused && primaryColor)}}
161+
style={{borderBottomColor: this.getColor()}}
151162
/>
152-
<div className={Styles.helper} style={{color: errorColor}}>{helperText}</div>
163+
<div
164+
className={Styles.helper}
165+
style={{color: errorColor || 'rgba(0,0,0,.54)'}}
166+
>
167+
{helperText}
168+
</div>
153169
</div>
154170
);
155171
}

0 commit comments

Comments
 (0)