【问题标题】:Value is reset but input is not?值被重置但输入不是?
【发布时间】:2020-09-17 02:58:45
【问题描述】:

我正在使用react-day-picker 并且正在正确重置所选日期,但不确定如何清除 DayPickerInput 占位符。

示例:我选择一个日期范围并单击重置。结果会发生变化,但旧日期仍会出现在输入中。有什么想法吗?

代码:

<div className={isOpen ? 'date-picker block' : 'date-picker'}>
  <div className="calendar-picker">
    <div className="InputFromTo">
      <span className="fal fa-icon fa-calendar-times calendar-picker-icon" aria-hidden="true" title="decorative calendar icon"></span>
      <DayPickerInput
        value={from}
        placeholder="Start Date"
        disabled
        format="ll"
        formatDate={formatDate}
        parseDate={parseDate}
        dayPickerProps={{
          selectedDays: [from, { from, to }],
          disabledDays: { after: to },
          toMonth: to,
          modifiers,
          numberOfMonths: 2,
          onDayClick: () => filterState.to.getInput().focus(),
        }}
        onDayChange={handleDayClick}
        inputProps={
          {readOnly: true }
        }
      />
      <span className="InputFromTo-to">
        <span className="fal fa-icon fa-calendar-times calendar-picker-icon" aria-hidden="true" title="decorative calendar icon"></span>
        <DayPickerInput
          ref={el => (filterState.to = el)}
          value={to}
          placeholder="End Date"
          disabled
          format="ll"
          formatDate={formatDate}
          parseDate={parseDate}
          dayPickerProps={{
            selectedDays: [from, { from, to }],
            disabledDays: { before: from },
            modifiers,
            month: from,
            fromMonth: from,
            numberOfMonths: 2,
          }}
          onDayChange={handleDayClick}
          inputProps={
            {readOnly: true }
          }
        />
      </span>
    </div>
  </div>
</div>

功能:

function handleResetClick() {
  setState({
    from: undefined,
    to: undefined,
  });
}

按钮:

// Custom Clear Refinements Button - clears filters and day picker
const ClearRefinements = ({ items, refine }) => (
  <button className="mt-3 clear-button" onClick={() => {refine(items); handleResetClick()}}>
    Clear all filters
  </button>
);

const CustomClearRefinements = connectCurrentRefinements(ClearRefinements);

【问题讨论】:

    标签: reactjs react-hooks next.js react-day-picker


    【解决方案1】:
    import React, { Component } from 'react';
    import 'date-fns';
    import moment from "moment";
    import "moment-timezone";
    import DayPickerInput from 'react-day-picker/DayPickerInput';
    import { formatDate, parseDate } from 'react-day-picker/moment';
    import "moment/locale/id";
    import MomentLocaleUtils from 'react-day-picker/moment';
    import { connect } from 'react-redux';
    
    import { setDateAction } from '../../actions/saldosAction';
    
    const limit = (event) => {
        // e.preventDefault();
        if( event.getDate() === new Date().getDate() && event.getMonth() === new Date().getMonth() && event.getFullYear() === new Date().getFullYear() ){
          return new Date();
        }
    
          var day = event.getDate();
          var month = event.getMonth();
          var year = event.getFullYear();
          var D = `${month+1},${day},${year}`;
          var toDate = new Date(D)
          toDate.setDate(toDate.getDate()+29);
    
        //   console.log("Limit date => To date=>",toDate)
    
          if(toDate.getFullYear() === new Date().getFullYear()){
            if( toDate.getMonth() > new Date().getMonth() )  return new Date();
            if( toDate.getMonth() === new Date().getMonth() && toDate.getDate() > new Date().getDate() ) return new Date();
          }
          // now considering that the "year" < "toDate.year"
          return toDate;
      }
    
    
    class DatePickerComponent extends Component {
    
        constructor(props) {
            super(props);
            this.state = {
                from: undefined,
                to: undefined
            }
            this.handleFromChange = this.handleFromChange.bind(this);
            this.handleToChange = this.handleToChange.bind(this);
            this.clearDate = this.clearDate.bind(this);
        };
    
        clearDate() {
            this.setState({
                from: undefined,
                to: undefined,
            },()=>{
                    var fromExist = document.getElementById("from-day") ;
                    var toExist = document.getElementById("to-day");
                    if (fromExist && toExist ){
                        document.getElementById("from-day").value = "";
                        document.getElementById("to-day").value = "";
                    }      
            })       
        }
    
        showFromMonth() {
            const { from, to } = this.state;
            if (!from) {
                return;
            }
            if (moment(to).diff(moment(from), 'months') < 2) {
                this.to.getDayPicker().showMonth(from);
            }
        }
    
        handleFromChange(from) {
            // Change the from date and focus the "to" input field
            this.setState({ from });
        }
    
        handleToChange(to) {
            this.setState({ to }, this.showFromMonth);
            const from = this.state.from;
            this.props.setDateAction({from,to});
        }
    
    
        render() {
            const { from, to } = this.state;
            const date = this.props.dateprops;
            const modifiers = { start: from, end: to };
    
            if((date.from === undefined && date.to === undefined) && (from !== undefined && to !== undefined) ){
                this.clearDate()
            }
    
            return (
                <div className="InputFromTo">
                    <DayPickerInput
                        value={from}
                        placeholder="From"
                        format="LL"
                        formatDate={formatDate}
                        parseDate={parseDate}
                        fixedWeeks={4}
                        dayPickerProps={{
                            selectedDays: [from, { from, to }],
                            disabledDays: [{ after: to ? to : new Date(moment()) , before: from ? from : '' }],
                            toMonth: to,
                            modifiers,
                            numberOfMonths: 1,
                            onDayClick: () => this.to.getInput().focus(),
                            localeUtils: MomentLocaleUtils, locale: "id"
                        }}
                        inputProps={{
                            id :"from-day",
                            style: { width: '160px' }
                        }}
    
                        onDayChange={this.handleFromChange}
                    />{' '}
                    -{' '}
                    <span className="InputFromTo-to">
                        <DayPickerInput
                            ref={el => (this.to = el)}
                            value={to}
                            placeholder="To"
                            format="LL"
                            formatDate={formatDate}
                            parseDate={parseDate}
                            dayPickerProps={{
                                selectedDays: [from, { from, to }],
                                disabledDays: [{ before: from , after: to ? to : ( from === undefined ? new Date() : limit(from) ) }],
                                modifiers,
                                month: from,
                                fromMonth: from,
                                numberOfMonths: 1,
                                localeUtils: MomentLocaleUtils, locale: "id"
                            }}
                            inputProps={{
                                id: "to-day",
                                style: { width: '160px' }
                            }}
                            onDayChange={this.handleToChange}
                        />
                    </span>
    
                </div>
            )
        }
    }
    
    
    export default connect(null,{setDateAction})(DatePickerComponent);
    

    当我单击清除按钮时,清除日期按钮位于其他组件中,它通过setDateAction(undefined,undefined) 在减速器中进行更改。 所以在 render() 方法 const date = this.props.dateprops; 和我用来调用 clear() 方法的 if() 块中

    在这里我使用 vanila JS 来清除日期,因为我也遇到了同样的问题。 您需要在日期组件的输入中添加一个 id/class,否则您也可以使用您的类名 =>“InputFromTo”,但这将变得太长而无法获取子节点数据。

    在clearDate 函数中,您可以看到我检查并清除了值。 所以就试试这个方法,看看能不能运行。

    limit函数用于设置最大30天的范围。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-20
      • 1970-01-01
      • 2020-08-04
      • 1970-01-01
      • 2020-08-12
      • 1970-01-01
      相关资源
      最近更新 更多