material-ui icon indicating copy to clipboard operation
material-ui copied to clipboard

DatePicker has no way to clear it

Open pr1ntr opened this issue 11 years ago • 26 comments

I am trying to clear the datepicker. The best I can do is: this.refs.myDatePicker.setDate();

which clears the field. however this.refs.myDatePicker.getDate(); still gives me the date. Is there a way to remove the date from it?

pr1ntr avatar Aug 28 '15 01:08 pr1ntr

Im struggling with this at the moment as well. I've noticed that the following will log undefined to the console.

this.refs.myDatePicker.setDate();
setTimeout(() => {
    console.log(this.refs.myDatePicker.getDate());
}, 2000);

However, if I remove the timeout I get the actual date value as well.

s-petersson avatar Aug 28 '15 12:08 s-petersson

The problem occurs because reacts setState is asynchronous. I solved it by creating a higher order component for DatePicker, which is ClearableDatePicker. This higher order component manually reaches into setState of DatePicker and sets date = undefined. I then wait for the callback method and manually fire the onChange event for DatePicker. Like this:

const Clearable = ComposedComponent => class extends React.Component {
  clearDate (event) {
    event.preventDefault();

    // We manually reach into the composed component and set it's date to undefined.
    let newDate;
    this.refs.datePicker.setState({
      date: newDate
    }, () => {
      this.refs.datePicker.props.onChange(null, newDate);
    });
  }

  render () {
    return (
      <div className='datepicker'>
        <ComposedComponent { ...this.props } ref="datePicker" />
        { this.renderClearButton() }
      </div>
    )
  }

  renderClearButton () {
    if (this.refs.datePicker && this.refs.datePicker.getDate()) {
      return <button className='clear' onClick={ ::this.clearDate }>X</button>;
    }
  }
}

export default Clearable(DatePicker);

Disclaimer: This higher order component requires me to manually redefine getDate again in order use it for when using my component. There is probably a way of solving this, I have not found a solution for that yet though.

s-petersson avatar Aug 28 '15 13:08 s-petersson

I ended up doing this, thought its bad.

this.refs.myDatePicker.setDate();
this.refs.myDataPicker.state.date = null;

probably shouldn't do that.

pr1ntr avatar Aug 28 '15 18:08 pr1ntr

Notice, in the upcoming version of [email protected], we can no longer do stuff like

this.refs.myDatePicker.setDate();

Edit Turn out we can!

oliviertassinari avatar Aug 28 '15 18:08 oliviertassinari

@oliviertassinari What is a valid future proof approach to clearing the date then? Do you know any?

s-petersson avatar Aug 28 '15 20:08 s-petersson

A workaround I've used when I need to reset the state of a child component is changing its key - which forces the component to remount and, therefore, reinitialize it's state.

LyricL-Gitster avatar Aug 30 '15 06:08 LyricL-Gitster

@oliviertassinari You can use refs. Check out this gist: https://gist.github.com/jkruder/52a2f88eb38ff6c0fec8

jkruder avatar Aug 30 '15 19:08 jkruder

@jkruder Oh great, my bad! Now I understand what this mean from their blog

Refs to custom component classes work exactly as before

oliviertassinari avatar Aug 30 '15 19:08 oliviertassinari

Having this issue here, @Pendla approach almost works, except after reseting the component the text doens't clears up, I'm using floatingText, the floating text goes back to it's original position but the old value still present there.

wilkerlucio avatar Nov 02 '15 22:11 wilkerlucio

@nathanmarks I'd like to suggest that the DatePicker component be changed to have a clearable prop, and if set to true, an "x" will be rendered, similar to the Select component.

ajsharp avatar Apr 20 '16 01:04 ajsharp

Hey @ajsharp ,

I think that DatePicker could simply be something that you "attach" to a TextField, leaving that part of the component to be composable.

nathanmarks avatar Apr 20 '16 01:04 nathanmarks

@nathanmarks Why? It's less code for the user of this library to say "give me a datepicker with these options". What benefit to the user is being "composable" in this instance?

ajsharp avatar Apr 20 '16 19:04 ajsharp

@ajsharp If we followed that principle for everything, the library would be extremely limited. We're talking about a couple of lines of code here, not entire components the user has to implement themselves.

I just flicked through my nexus 6p and found multiple instances of date and time pickers that are not hooked up to material form style text fields -- or anything similar visually (apart from having text). For eg, in the android stock alarm application, the time is just a big number (very large heading size) that you tap and it pops open a time picker.

To do that with our time picker, you'd have to override styles up the ying yang. Seems pointless using a material-ui TextField if you don't actually want a material text input with all the form styling.

nathanmarks avatar Apr 20 '16 23:04 nathanmarks

I've ended up succeeding using datePicker with the new Field API from redux-form and with a clearable button. I had to use null value for resetting the datePicker and not undefined. I also use Moment for dealing with UTC date (It should be the default behaviur by the way). I''d like to thank you people in this thread for the inspiration and explanation.

Clearable Component :

import React, {Component} from 'react';
import Moment from 'moment';
import DatePicker from 'material-ui/DatePicker';
import IconButton from 'material-ui/IconButton';
import Clear from 'material-ui/svg-icons/content/clear';

const Clearable = ComposedComponent => class extends React.Component {

    onChange(evt, date) {
        if (this.props.input.onChange) {
            this.props.input.onChange(date ? Moment.utc(Moment(date).format('YYYY-MM-DD[T]HH:mm:ss')).toDate() : null);
        }
    }

    clearDate (event) {
        event.preventDefault();

        // We manually reach into the composed component and set it's date to null.
        let newDate = null;
        this.refs.datePicker.setState({
            date: newDate
        }, () => {
            this.refs.datePicker.props.onChange(null, newDate);
        });
    }
    render () {
        return (
            <div style={{position: 'relative'}}>
                <ComposedComponent
                    { ...this.props.input }
                    autoOk={true}
                    container="inline"
                    ref="datePicker"
                    value={this.props.input.value ? new Date(this.props.input.value) : null}
                    onChange={this.onChange.bind(this)}/>
                {this.props.input.value &&
                <IconButton ref="button" onClick={this.clearDate.bind(this)} style={{position: 'absolute', top: '6px',right: '4px', padding: '0', width: '24px', height: '24px'}}>
                    <Clear />
                </IconButton>
                }
            </div>
        )
    }
};

export default Clearable(DatePicker);

And you can use it like that:

import DatePickerInput from './DatePickerInput';

<Field name="date" component={DatePickerInput} hintText="Date"/>

Hope it will help folks ;)

GuillaumeCisco avatar Jul 27 '16 08:07 GuillaumeCisco

So, is a a higher order component the only way to clear the DatePicker? I think it would be nicer if it cleared when you set the value property as null.

GAumala avatar Aug 12 '16 16:08 GAumala

I ended up with pure functional component like this:

import React from 'react';
import DatePicker from 'material-ui/DatePicker';
import MUI_SquareFlatBtn from '/client/modules/genericComponents/components/MUI_SquareFlatBtn';


export default function MUI_DatePickerClearable({context, style, clearIcon, datePickerStyle={}, ...datePickerProps}) {

  function clearBtnClickWrapper() {
    datePickerProps.onChange(null, null); // (event, newValue)
  }

  return (
    <div
      style={{
        display: 'flex',
        flexDirection: 'row',
        alignItems: 'flex-end'
      }}
    >
      <div style={{flexGrow: '1'}}>
        <DatePicker
          style={datePickerStyle}
          {...datePickerProps}
          fullWidth={true}
        />
      </div>
      <div>
        <MUI_SquareFlatBtn
          icon={clearIcon}
          onTouchTap={clearBtnClickWrapper}
        />
      </div>
    </div>
  );
}

The value is passed to date picker inside datePickerProps

Kshatra avatar Apr 15 '17 17:04 Kshatra

Needed a quick and dirty way to do it would be something like

[...document.querySelectorAll('input[name=date-timestamp]')].forEach(input => {
  console.log(input.value);
   input.value = '';
});

I would think this would work....but some reason that I am not able to get...its not working. console.log returns the correct value, but setting the value no longer works.

hozefaj avatar Apr 21 '17 19:04 hozefaj

So back again 2 years later. Here is my current dirty solution: (this is a piece of a bigger whole)


const DateRange = ({ order, changeStartDate, changeEndDate, startDate, endDate }) => {
    let minDateRef, maxDateRef
    return <div className="row center-xs leaderboards" >
            <Paper className="col-xs-10 col-sm-8 col-md-6">
                <div style={dateRangeContainerStyle}>
                    <div style={dateRangeStyle}>
                        <DatePicker ref={(ref) => minDateRef = ref} floatingLabelText="Min Date" autoOk onChange={changeStartDate} formatDate={formatDate} />
                        <Close style={closeStyle} onTouchTap={() => {
                            changeStartDate()
                            minDateRef.setState({ date: null })
                        }} />
                    </div>
                    <div style={dateRangeStyle}>
                        <DatePicker ref={(ref) => maxDateRef = ref} floatingLabelText="Max Date" autoOk onChange={changeEndDate} formatDate={formatDate} />
                        <Close style={closeStyle} onTouchTap={() => {
                            changeEndDate()
                            maxDateRef.setState({ date: null })
                        }} />
                    </div>
                </div>
            </div>
        </Paper>
    </div>  
}

pr1ntr avatar Sep 14 '17 22:09 pr1ntr

@pr1ntr You can check this comment https://github.com/callemall/material-ui/issues/4952#issuecomment-273845579 regarding clearable fields. This is old code, but I'm sure it will help you making clear and concise code using the HOC pattern :)

GuillaumeCisco avatar Sep 15 '17 07:09 GuillaumeCisco

Why closed? There is still no way to set value as null and see empty field.

Ohar avatar Jan 17 '18 21:01 Ohar