Skip to content

Commit

Permalink
[TextField] Fix label position with dense margins (mui#7946)
Browse files Browse the repository at this point in the history
Fixes mui#7945
  • Loading branch information
phallguy authored and oliviertassinari committed Aug 30, 2017
1 parent f8fabb6 commit 5ef085a
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/Input/InputLabel.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ export const styles = (theme: Object) => ({
// slight alteration to spec spacing to match visual spec result
transform: `translate(0, ${theme.spacing.unit * 3 - 1}px) scale(1)`,
},
// Compensation for the `Input.inputDense` style.
labelDense: {
transform: `translate(0, ${theme.spacing.unit * 2.5 + 1}px) scale(1)`,
},
shrink: {
transform: 'translate(0, 1.5px) scale(0.75)',
transformOrigin: 'top left',
Expand Down Expand Up @@ -68,6 +72,11 @@ export type Props = {
* If `true`, the input of this label is focused.
*/
focused?: boolean,
/**
* If `dense`, will adjust vertical spacing. This is normally obtained via context from
* FormControl.
*/
margin?: 'dense',
/**
* if `true`, the label will indicate that the input is required.
*/
Expand All @@ -88,6 +97,7 @@ function InputLabel(props: AllProps, context: { muiFormControl: Object }) {
classes,
className: classNameProp,
shrink: shrinkProp,
margin: marginProp,
...other
} = props;

Expand All @@ -98,13 +108,19 @@ function InputLabel(props: AllProps, context: { muiFormControl: Object }) {
shrink = muiFormControl.dirty || muiFormControl.focused;
}

let margin = marginProp;
if (typeof margin === 'undefined' && muiFormControl) {
margin = muiFormControl.margin;
}

const className = classNames(
classes.root,
{
[classes.formControl]: muiFormControl,
[classes.animated]: !disableAnimation,
[classes.shrink]: shrink,
[classes.disabled]: disabled,
[classes.labelDense]: margin === 'dense',
},
classNameProp,
);
Expand Down
6 changes: 6 additions & 0 deletions src/Input/InputLabel.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ describe('<InputLabel />', () => {
setFormControlContext({});
assert.strictEqual(wrapper.hasClass(classes.formControl), true);
});

it('should have the labelDense class when margin is dense', () => {
setFormControlContext({ margin: 'dense' });
assert.strictEqual(wrapper.hasClass(classes.labelDense), true);
});

['dirty', 'focused'].forEach(state => {
describe(state, () => {
beforeEach(() => {
Expand Down
13 changes: 13 additions & 0 deletions test/regressions/tests/TextField/TextFieldMargin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// @flow

import React from 'react';
import TextField from 'material-ui/TextField';

export default function TextFieldMargin() {
return (
<div style={{ display: 'flex' }}>
<TextField label="Dense" defaultValue="Default Value" margin="dense" />
<TextField style={{ position: 'absolute ' }} label="Dense" margin="dense" />
</div>
);
}

0 comments on commit 5ef085a

Please sign in to comment.