Skip to content
This repository was archived by the owner on Jan 14, 2025. It is now read-only.

Commit 0818061

Browse files
Ryan BareMatt Goo
authored andcommitted
feat(text-field): add placeholder property (#930)
1 parent f829e12 commit 0818061

File tree

9 files changed

+60
-64
lines changed

9 files changed

+60
-64
lines changed

package-lock.json

Lines changed: 26 additions & 59 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@
8989
"@material/snackbar": "^1.0.0",
9090
"@material/tab": "^1.0.0",
9191
"@material/tab-bar": "^1.0.0",
92-
"@material/textfield": "^1.1.1",
92+
"@material/textfield": "^2.3.1",
9393
"@material/typography": "^1.0.0",
9494
"@types/chai": "^4.1.7",
9595
"@types/classnames": "^2.2.6",

packages/text-field/index.tsx

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ export interface Props<T extends HTMLElement = HTMLInputElement> {
5959
onTrailingIconSelect?: () => void;
6060
textarea?: boolean;
6161
trailingIcon?: React.ReactElement<React.HTMLProps<HTMLOrSVGElement>>;
62+
noLabel?: boolean;
6263
}
6364

6465
type TextFieldProps<T extends HTMLElement = HTMLInputElement> = Props<T> &
@@ -96,6 +97,7 @@ class TextField<
9697
notchedOutlineClassName: '',
9798
outlined: false,
9899
textarea: false,
100+
noLabel: false,
99101
};
100102

101103
constructor(props: TextFieldProps<T>) {
@@ -150,6 +152,7 @@ class TextField<
150152
textarea,
151153
trailingIcon,
152154
leadingIcon,
155+
noLabel,
153156
} = this.props;
154157

155158
return classnames(cssClasses.ROOT, Array.from(classList), className, {
@@ -164,7 +167,7 @@ class TextField<
164167
// TODO change literal to constant
165168
'mdc-text-field--fullwidth': fullWidth,
166169
'mdc-text-field--with-trailing-icon': trailingIcon,
167-
'mdc-text-field--no-label': !this.labelAdapter.hasLabel(),
170+
'mdc-text-field--no-label': !this.labelAdapter.hasLabel() || noLabel,
168171
});
169172
}
170173

@@ -187,6 +190,7 @@ class TextField<
187190
outlined,
188191
textarea,
189192
trailingIcon,
193+
noLabel,
190194
/* eslint-enable @typescript-eslint/no-unused-vars */
191195
...otherProps
192196
} = this.props;
@@ -301,6 +305,7 @@ class TextField<
301305
setInputId: (id: string) => this.setState({inputId: id}),
302306
syncInput: (input: Input<T>) => (this.inputComponent_ = input),
303307
inputType: this.props.textarea ? 'textarea' : 'input',
308+
placeholder: this.props.noLabel ? this.props.label : null,
304309
});
305310
}
306311

@@ -325,6 +330,7 @@ class TextField<
325330
leadingIcon,
326331
trailingIcon,
327332
textarea,
333+
noLabel,
328334
} = this.props;
329335
const {foundation} = this.state;
330336

@@ -349,7 +355,9 @@ class TextField<
349355
this.renderNotchedOutline()
350356
) : (
351357
<React.Fragment>
352-
{this.labelAdapter.hasLabel() ? this.renderLabel() : null}
358+
{this.labelAdapter.hasLabel() && !noLabel
359+
? this.renderLabel()
360+
: null}
353361
{!textarea && !fullWidth ? this.renderLineRipple() : null}
354362
</React.Fragment>
355363
)}

packages/text-field/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
"@material/react-floating-label": "^0.13.0",
2424
"@material/react-line-ripple": "^0.13.0",
2525
"@material/react-notched-outline": "^0.13.0",
26-
"@material/textfield": "^1.1.1",
26+
"@material/textfield": "^2.3.1",
2727
"classnames": "^2.2.6"
2828
},
2929
"publishConfig": {

test/screenshot/golden.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,11 @@
2727
"text-field/icon": "0bbc8c762d27071e55983e5742548d166864b6fcebc0b9f1e413523fb93b7075",
2828
"text-field/textArea": "dde78e3f154a8b910a989f8ce96e320e7ad2b3e199e6e7a81034174c598cbd9d",
2929
"text-field/standard": "43c25e9eaadb2cd0f7ceeac939d3416344504d92baaca5ed8be02df84c2bba79",
30-
"text-field/fullWidth": "6555b4398509aa79f2e6ae1ef3be678a9a877984c4aaf77804afc01fe4fc50a6",
30+
"text-field/fullWidth": "9de5299b12f691b107d8ac4e5c3f9ef40c75012d85eb8bb2cb369a3f8c626523",
3131
"text-field/outlined": "a6f22c45fe20e8dab39c39711cbec6c249933b7c655d4dff39d4a635c846acc6",
3232
"text-field/refTest": "0310e81ea870fb0e6e5968d0e1567e22b0e952aeeff9653d3dfe8d9b5b1f5588",
3333
"text-field/autoFocus": "3b5e7d823fb7c8caf0f7568add1f99bc7c0171afb19f5d1f9978a68091dd07bd",
34+
"text-field/noLabel": "03812f8c2eeb6bbfeb5bae4cdc7794a7ca760628e119edaec974d8f87740453c",
3435
"top-app-bar/fixed": "7a2dd6318d62ac2eabd66f1b28100db7c15840ccb753660065fa9524db6435d6",
3536
"top-app-bar/prominent": "2506ed2dd5f370c7bab69315d2daebd58b443d2b9e32bbaec762e40a8736309b",
3637
"top-app-bar/short": "90dba9623f16d58cfc4a24b2a3ab652c7e0cc6d5ccfd030566a170a55d6bce0c",

test/screenshot/text-field/TestTextField.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ class TestField extends React.Component<TestFieldProps, TestFieldState> {
4444
outlined={variant === 'outlined'}
4545
fullWidth={variant === 'fullWidth'}
4646
textarea={variant === 'textarea'}
47+
noLabel={variant === 'noLabel'}
4748
{...otherProps}
4849
className='text-field'
4950
>
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import React from 'react';
2+
import TextFieldPermutations from './TextFieldPermutations';
3+
4+
const NoLabelTextField = () => {
5+
return <TextFieldPermutations variant='noLabel' />;
6+
};
7+
8+
export default NoLabelTextField;

test/screenshot/text-field/variants.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ export default [
55
'textArea',
66
'refTest',
77
'autoFocus',
8+
'noLabel',
89
];

test/unit/text-field/index.test.tsx

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,16 @@ test('classNames get dense class when prop.dense is true', () => {
9191
assert.equal(textField.length, 1);
9292
});
9393

94+
test('classNames get noLabel class when prop.noLabel is true', () => {
95+
const wrapper = mount(
96+
<TextField label='my label' noLabel>
97+
<Input />
98+
</TextField>
99+
);
100+
const textField = wrapper.find('.mdc-text-field--no-label');
101+
assert.equal(textField.length, 1);
102+
});
103+
94104
test('style prop adds style attribute', () => {
95105
const wrapper = mount(
96106
<TextField label='my label' style={{backgroundColor: 'red'}}>

0 commit comments

Comments
 (0)