Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TextField] Separate TextFieldHint into separate internal component. #2567

Merged
merged 2 commits into from
Dec 17, 2015
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 18 additions & 29 deletions src/TextField/TextField.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import EnhancedTextarea from '../enhanced-textarea';
import DefaultRawTheme from '../styles/raw-themes/light-raw-theme';
import ThemeManager from '../styles/theme-manager';
import ContextPure from '../mixins/context-pure';
import TextFieldHint from './TextFieldHint';
import TextFieldUnderline from './TextFieldUnderline';
import warning from 'warning';

Expand Down Expand Up @@ -178,13 +179,17 @@ const TextField = React.createClass({
color: errorColor,
transition: Transitions.easeOut(),
},
hint: {
floatingLabel: {
position: 'absolute',
lineHeight: '22px',
top: 38,
opacity: 1,
color: hintColor,
transition: Transitions.easeOut(),
bottom: 12,
zIndex: 1, // Needed to display label above Chrome's autocomplete field background
cursor: 'text',
transform: 'scale(1) translate3d(0, 0, 0)',
transformOrigin: 'left top',
},
input: {
tapHighlightColor: 'rgba(0,0,0,0)',
Expand All @@ -202,17 +207,6 @@ const TextField = React.createClass({

styles.error = this.mergeStyles(styles.error, props.errorStyle);

styles.floatingLabel = this.mergeStyles(styles.hint, {
lineHeight: '22px',
top: 38,
bottom: 'none',
opacity: 1,
zIndex: 1, // Needed to display label above Chrome's autocomplete field background
cursor: 'text',
transform: 'scale(1) translate3d(0, 0, 0)',
transformOrigin: 'left top',
});

styles.textarea = this.mergeStyles(styles.input, {
marginTop: props.floatingLabelText ? 36 : 12,
marginBottom: props.floatingLabelText ? -36 : -12,
Expand All @@ -229,17 +223,11 @@ const TextField = React.createClass({
if (this.state.hasValue) {
styles.floatingLabel.color = ColorManipulator.fade(props.disabled ? disabledTextColor : floatingLabelColor, 0.5);
styles.floatingLabel.transform = 'perspective(1px) scale(0.75) translate3d(2px, -28px, 0)';
styles.hint.opacity = 0;
}

if (props.floatingLabelText) {
styles.hint.opacity = 0;
styles.input.boxSizing = 'border-box';

if (this.state.isFocused && !this.state.hasValue) {
styles.hint.opacity = 1;
}

if (!props.multiLine) {
styles.input.marginTop = 14;
}
Expand All @@ -249,10 +237,6 @@ const TextField = React.createClass({
}
}

if (props.style && props.style.height) {
styles.hint.lineHeight = props.style.height;
}

if (this.state.errorText) {
if (this.state.isFocused) {
styles.floatingLabel.color = styles.error.color;
Expand All @@ -277,6 +261,7 @@ const TextField = React.createClass({
onBlur,
onChange,
onFocus,
style,
type,
underlineDisabledStyle,
underlineFocusStyle,
Expand All @@ -295,10 +280,6 @@ const TextField = React.createClass({
<div style={this.prepareStyles(styles.error)}>{this.state.errorText}</div>
) : null;

let hintTextElement = hintText ? (
<div style={this.prepareStyles(styles.hint, hintStyle)}>{hintText}</div>
) : null;

let floatingLabelTextElement = floatingLabelText ? (
<label
style={this.prepareStyles(styles.floatingLabel, this.props.floatingLabelStyle)}
Expand Down Expand Up @@ -354,9 +335,17 @@ const TextField = React.createClass({
return (
<div className={className} style={this.prepareStyles(styles.root, this.props.style)}>
{floatingLabelTextElement}
{hintTextElement}
{hintText ?
<TextFieldHint
hidden={this.state.hasValue || (floatingLabelText && !this.state.isFocused)}
muiTheme={this.state.muiTheme}
style={hintStyle}
text={hintText}
/> :
null
}
{inputElement}
{this.props.underlineShow ?
{underlineShow ?
<TextFieldUnderline
disabled={disabled}
disabledStyle={underlineDisabledStyle}
Expand Down
66 changes: 66 additions & 0 deletions src/TextField/TextFieldHint.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import React from 'react';
import Transitions from '../styles/transitions';
import styleUtils from '../utils/styles';

const propTypes = {
/**
* True if the hint text should be hidden.
*/
hidden: React.PropTypes.bool,

/**
* The material-ui theme applied to this component.
*/
muiTheme: React.PropTypes.object.isRequired,

/**
* Override the inline-styles of the hint text.
*/
style: React.PropTypes.object,

/**
* The hint text displayed.
*/
text: React.PropTypes.string,
};

const defaultProps = {
visible: true,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like a typo.
When we have the choice between hidden, show and visible, I would personally go for show.
I also think that show is what it was chosen in some other component.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup good catch, and I'll change it to show. I just remembered we did underlineShow in that other pull request, so I'll make it show here.

};

const TextFieldHint = (props) => {

const {
hidden,
muiTheme,
style,
text,
} = props;

const {
textField: {
hintColor,
},
} = muiTheme;

const styles = {
root: {
position: 'absolute',
opacity: hidden ? 0 : 1,
color: hintColor,
transition: Transitions.easeOut(),
bottom: 12,
},
};

return (
<div
style={styleUtils.prepareStyles(muiTheme, styles.root, style)}>{text}
</div>
);
};

TextFieldHint.propTypes = propTypes;
TextFieldHint.defaultProps = defaultProps;

export default TextFieldHint;