Repository navigation
Expand file tree
/
Copy pathTextFieldInput.js
More file actions
169 lines (149 loc) · 3.58 KB
/
Copy pathTextFieldInput.js
File metadata and controls
169 lines (149 loc) · 3.58 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
// @flow weak
import React, { Component, PropTypes } from 'react';
import { createStyleSheet } from 'stylishly';
import shallowEqual from 'recompose/shallowEqual';
import ClassNames from 'classnames';
export const styleSheet = createStyleSheet('TextFieldInput', (theme) => {
const { palette } = theme;
return {
root: {
font: 'inherit',
padding: '6px 0',
border: 0,
display: 'inline-block',
verticalAlign: 'middle',
whiteSpace: 'normal',
background: 'none',
lineHeight: 1,
'&:focus': {
outline: 0,
},
},
disabled: {
color: palette.text.disabled,
cursor: 'not-allowed',
},
underline: {
borderBottom: `1px solid ${palette.text.divider}`,
'&disabled': {
borderBottomStyle: 'dotted',
},
},
};
});
/**
* TextFieldInput
*/
export default class TextFieldInput extends Component {
static muiName = 'TextFieldInput';
static propTypes = {
/**
* The CSS class name of the root element.
*/
className: PropTypes.string,
/**
* The element or component used for the root node.
*/
component: PropTypes.oneOfType([PropTypes.string, PropTypes.func]),
disabled: PropTypes.bool,
/**
* @ignore
*/
onChange: PropTypes.func,
/**
* @ignore
*/
onClean: PropTypes.func,
/**
* @ignore
*/
onDirty: PropTypes.func,
/**
* TextFieldInput type
*/
type: PropTypes.string,
/**
* If set to true, the input will have an underline
*/
underline: PropTypes.bool,
/**
* The input value, required for a controlled component
*/
value: PropTypes.string,
};
static defaultProps = {
component: 'input',
type: 'text',
underline: true,
};
static contextTypes = {
styleManager: PropTypes.object.isRequired,
};
componentWillMount() {
if (this.isControlled()) {
this.checkDirty(this.props);
}
}
shouldComponentUpdate(nextProps, nextState, nextContext) {
return (
!shallowEqual(this.props, nextProps) ||
!shallowEqual(this.context, nextContext)
);
}
componentWillUpdate(nextProps) {
this.checkDirty(nextProps);
}
// Holds the input reference
input = undefined;
handleChange = (event) => {
if (!this.isControlled()) {
this.checkDirty(this.input);
}
if (this.props.onChange) {
this.props.onChange(event);
}
};
isControlled() {
return typeof this.props.value === 'string';
}
isDirty(obj) {
return obj && obj.value && obj.value.length > 0;
}
checkDirty(obj) {
if (this.props.onDirty && this.isDirty(obj)) {
this.props.onDirty();
} else if (this.props.onClean) {
this.props.onClean();
}
}
render() {
const {
className,
component,
disabled,
onChange, // eslint-disable-line no-unused-vars
onDirty, // eslint-disable-line no-unused-vars
onClean, // eslint-disable-line no-unused-vars
type,
underline,
...other,
} = this.props;
const classes = this.context.styleManager.render(styleSheet, { group: 'mui' });
const classNames = ClassNames({
[classes.root]: true,
[classes.underline]: underline,
[classes.disabled]: disabled,
}, className);
const inputProps = {
ref: (c) => this.input = c,
className: classNames,
onChange: this.handleChange,
disabled,
...other,
};
if (component === 'input' || typeof component === 'function') {
inputProps.type = type;
}
return React.createElement(component, inputProps);
}
}