Scenario 1
To customize the placeholder text in the TextField
component, use the label
property and adjust it with the labelClassName
property. Alternatively, you can pass InputLabelProps
with attributes like className
, classes
, or style
for further customization.
Scenario 2
If you prefer not to use the label
property, insert the placeholder text in the placeholder
property of the TextField
. Utilize InputProps
to modify the class of the HTML input
element generated.
Code Example
The code snippet provided below demonstrates both scenarios mentioned above. View the implementation on CodeSandbox here.
import React from 'react';
import TextField from 'material-ui/TextField';
import { withStyles } from 'material-ui/styles';
import withRoot from '../components/withRoot';
const styles = {
'input-label': {
textOverflow: 'ellipsis',
whiteSpace: 'nowrap',
overflow: 'hidden',
width: '100%',
color: 'red'
},
'input': {
'&::placeholder': {
textOverflow: 'ellipsis !important',
color: 'blue'
}
}
};
class Index extends React.Component {
render() {
return <div style={ {width: 150, margin: '0 auto'} }>
{/* Using "label" and "labelClassName". */}
<TextField
fullWidth
label='I am a really really long red TextField label'
labelClassName={ this.props.classes['input-label'] } />
{/* Using "label" and "InputLabelProps" with inline styles. */}
<TextField
fullWidth
label='I am a really really long green TextField label'
InputLabelProps={{
style: {
textOverflow: 'ellipsis',
whiteSpace: 'nowrap',
overflow: 'hidden',
width: '100%',
color: 'green'
} }} />
{/* Using "placeholder" and "InputProps" with "classes". */}
<TextField
fullWidth
margin='normal'
placeholder='I am a really really long glue TextField label'
InputProps={{ classes: {input: this.props.classes['input']} }} />
</div>;
}
}
export default withStyles(styles)(Index);
UPDATE
If you wish to globally change the placeholder behavior, refer to ninjaPixel's response for a comprehensive solution.