Search code examples
javascriptreactjstimepicker

Unable to retrieve the id property from rc-time-picker


I am using rc-time-picker in Reactjs. It displays fine. I am able to access its value property, However, I am trying to access the id property of the rc-time-picker and unable to do so.

import 'rc-time-picker/assets/index.css';
import TimePicker from 'rc-time-picker';
import moment from "moment";


<TimePicker

       onChange={ function(value, id){console.log(value, id)} }

                                          id={item.day}
                                          defaultValue={moment()}
                                          value={moment(item.start, format)}
                                          format={format}
                                          disabled={!item.isChecked}
                                          showSecond={false}
                                        />

Console outputs only the value property, while id property is shown as undefined.

Github Ref: https://github.com/react-component/time-picker

Any help will be highly appreciated here.


Solution

  • You can do this by following way:

    class App extends React.Component {
      state = {
        value: moment(),
      };
    
      handleValueChange = (value, id) => {
        console.log(value && value.format('HH:mm:ss'));
        console.log(id);
        this.setState({ value });
      };
    
      clear = () => {
        this.setState({
          value: undefined,
        });
      };
    
      render() {
        const { value } = this.state;
        return (
          <div>
            <TimePicker
            id={item.day}
            defaultValue={value}
            onChange={(value, id=item.day) => this.handleValueChange(value, id)}
        />
            <button onClick={this.clear} type="button">
              clear
            </button>
          </div>
        );
      }
    }
    

    Hope this solve the issue.