-
-
Notifications
You must be signed in to change notification settings - Fork 32.4k
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] Extract floating label #2701
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import React from 'react'; | ||
import Transitions from '../styles/transitions'; | ||
import styleUtils from '../utils/styles'; | ||
|
||
const propTypes = { | ||
/** | ||
* The material-ui theme applied to this component. | ||
*/ | ||
muiTheme: React.PropTypes.object.isRequired, | ||
|
||
/** | ||
* The css class name of the root element. | ||
*/ | ||
className: React.PropTypes.string, | ||
|
||
/** | ||
* The label contents. | ||
*/ | ||
children: React.PropTypes.node, | ||
|
||
/** | ||
* Disables the label if set to true. | ||
*/ | ||
disabled: React.PropTypes.bool, | ||
|
||
/** | ||
* True if the floating label should shrink. | ||
*/ | ||
shrink: React.PropTypes.bool, | ||
|
||
/** | ||
* The id of the target element that this label should refer to. | ||
*/ | ||
htmlFor: React.PropTypes.string, | ||
|
||
/** | ||
* Callback function for when the label is selected via a touch tap. | ||
*/ | ||
onTouchTap: React.PropTypes.func, | ||
|
||
/** | ||
* Override the inline-styles of the floating label. | ||
*/ | ||
style: React.PropTypes.object, | ||
}; | ||
|
||
const defaultProps = { | ||
disabled: false, | ||
shrink: false, | ||
}; | ||
|
||
const TextFieldLabel = (props) => { | ||
|
||
const { | ||
muiTheme, | ||
className, | ||
children, | ||
disabled, | ||
shrink, | ||
htmlFor, | ||
style, | ||
onTouchTap, | ||
} = props; | ||
|
||
const styles = { | ||
root: { | ||
position: 'absolute', | ||
lineHeight: '22px', | ||
top: 38, | ||
transition: Transitions.easeOut(), | ||
zIndex: 1, // Needed to display label above Chrome's autocomplete field background | ||
cursor: disabled ? 'default' : 'text', | ||
transform: shrink | ||
? 'perspective(1px) scale(0.75) translate3d(2px, -28px, 0)' | ||
: 'scale(1) translate3d(0, 0, 0)', | ||
transformOrigin: 'left top', | ||
pointerEvents: shrink ? 'none' : 'auto', | ||
userSelect: 'none', | ||
}, | ||
}; | ||
|
||
return ( | ||
<label | ||
className={className} | ||
style={styleUtils.prepareStyles(muiTheme, styles.root, style)} | ||
htmlFor={htmlFor} | ||
onTouchTap={onTouchTap} | ||
> | ||
{children} | ||
</label> | ||
); | ||
}; | ||
|
||
TextFieldLabel.propTypes = propTypes; | ||
TextFieldLabel.defaultProps = defaultProps; | ||
|
||
export default TextFieldLabel; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we consider extracting the rest of the styles from
styles.floatingLabel
and putting it in theTextFieldLabel
component? That way the only thingTextField
has to pass is thefloatingLabelStyle
prop?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suppose that means we would have to pass a
focused
orisFocused
prop down to theTextFieldLabel
so that it knows how to change styles properly. I think I'm doing something similar for theTextFieldUnderline
component.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My initial thoughts, but it's not that simple. Doing this will couple the components a LOT. In order to calculate the color,
hasError
,hasValue
anderrorColor
must too be passed down only to calculate a simplecolor
so I left that calculation to the TextField.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Mm, okay, I see.