-
-
Notifications
You must be signed in to change notification settings - Fork 32.4k
/
TextField.js
154 lines (135 loc) · 3.48 KB
/
TextField.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// @flow weak
import React, { Component, Children, cloneElement, PropTypes } from 'react';
import { createStyleSheet } from 'jss-theme-reactor';
import classNames from 'classnames';
import { easing } from '../styles/transitions';
import { createChainedFunction } from '../utils/helpers';
export const styleSheet = createStyleSheet('TextField', (theme) => {
const focusColor = theme.palette.accent.A200;
return {
root: {
display: 'flex',
position: 'relative',
marginTop: 16,
// Expanding underline
'&:after': {
backgroundColor: focusColor,
left: 0,
bottom: 9,
content: '\'\'',
height: 2,
position: 'absolute',
width: '100%',
transform: 'scaleX(0)',
transition: theme.transitions.create(
'transform',
'200ms',
null,
easing.easeOut,
),
},
},
label: {},
input: {
display: 'block',
marginTop: 10,
marginBottom: 10,
width: '100%',
zIndex: 1,
},
focused: {
'& $label': {
color: focusColor,
},
'&:after': {
transform: 'scaleX(1)',
},
},
};
});
/**
* TextField
*
* @see https://material.google.com/components/text-fields.html
*
* ```js
* import TextField from 'material-ui/TextField';
*
* const Component = () => <TextField value="Hello World">;
* ```
*/
export default class TextField extends Component {
static propTypes = {
/**
* The contents of the `TextField`.
*/
children: PropTypes.node,
/**
* The CSS class name of the root element.
*/
className: PropTypes.string,
};
static contextTypes = {
styleManager: PropTypes.object.isRequired,
};
state = {
dirty: false,
focused: false,
};
classes = {};
handleFocus = () => this.setState({ focused: true });
handleBlur = () => this.setState({ focused: false });
handleDirty = () => {
if (!this.state.dirty) {
this.setState({ dirty: true });
}
};
handleClean = () => {
if (this.state.dirty) {
this.setState({ dirty: false });
}
};
renderChild = (child) => {
const { muiName } = child.type;
if (muiName === 'TextFieldInput') {
return this.renderInput(child);
} else if (muiName === 'TextFieldLabel') {
return this.renderLabel(child);
}
return child;
};
renderInput = (input) => (
cloneElement(input, {
className: classNames(this.classes.input, input.props.className),
onDirty: this.handleDirty,
onClean: this.handleClean,
onFocus: createChainedFunction(this.handleFocus, input.props.onFocus),
onBlur: createChainedFunction(this.handleBlur, input.props.onBlur),
})
);
renderLabel = (label) => (
cloneElement(label, {
className: classNames(this.classes.label, label.props.className),
focused: this.state.focused,
shrink: label.props.hasOwnProperty('shrink') ? // Shrink the label if dirty or focused
label.props.shrink : (this.state.dirty || this.state.focused),
})
);
render() {
const {
children,
className: classNameProp,
...other
} = this.props;
this.classes = this.context.styleManager.render(styleSheet);
const className = classNames({
[this.classes.root]: true,
[this.classes.focused]: this.state.focused,
}, classNameProp);
return (
<div className={className} {...other}>
{Children.map(children, this.renderChild)}
</div>
);
}
}