Material-ui: Simulate Input focus or make its style trigger via prop

Created on 11 May 2018  路  1Comment  路  Source: mui-org/material-ui

I'm creating a date picker using Material-UI Input (TextField) as the base. When the textfield is clicked, naturally the date picker pops up and when it's being manipulated the input losses focus - not a wanted behaviour. I'd like to keep the Material-UI's focused style until needed.

Here's example code, if there's a simple and elegant workaround - I'll be happy to hear it

<TextField
        label="Date"
        onFocus={()=>this.setState({focused: true, showDatePicker: true})}
        inputRef={(input) => { this.dateInput = input; }}
        onBlur={ () => this.setState({showDatePicker: false})}
        value={this.state.date ? this.state.date.format('DD-MM-YYYY') : ''}
        className={classes.textField}
/>
 {(this.state.focused || this.state.showDatePicker) &&
  <DayPickerSingleDateController 
           date={this.state.date}
           focused={this.state.focused}
           onDateChange={date => this.setState({ date })}
           onFocusChange={({ focused }) => this.setState({ focused })}
           onOutsideClick={() => this.setState((prevState)=>(!prevState.showDatePicker && { focused: 
           false }))}
           ...
 />
}
question

Most helpful comment

I'd like to keep the Material-UI's focused style until needed.

@jwm0 We solve the problem like this with the Select component: https://github.com/mui-org/material-ui/blob/5dd0aedb184c62fd0ded0edf8fbe2d16eacb2a22/packages/material-ui/src/Select/SelectInput.js#L113-L124

I'm creating a date picker using Material-UI

@jwm0 We are happy to hear it :), don't forget to benchmark the alternatives:

>All comments

I'd like to keep the Material-UI's focused style until needed.

@jwm0 We solve the problem like this with the Select component: https://github.com/mui-org/material-ui/blob/5dd0aedb184c62fd0ded0edf8fbe2d16eacb2a22/packages/material-ui/src/Select/SelectInput.js#L113-L124

I'm creating a date picker using Material-UI

@jwm0 We are happy to hear it :), don't forget to benchmark the alternatives:

Was this page helpful?
0 / 5 - 0 ratings