gpt4 book ai didi

javascript - 如何防止用户在 react 日期中选择高于结束日期的日期

转载 作者:行者123 更新时间:2023-11-28 00:34:57 25 4
gpt4 key购买 nike

我想知道如何防止用户选择今天日期以上的日期。例如,今天是 3.7,因此将其作为用户可以选择的最晚结束日期。

<DateRangePicker
startDate={this.state.startDate}
startDateId="startDate"
endDate={this.state.endDate}
endDateId="endDate"
onDatesChange={({ startDate, endDate }) => {
this.setState({ startDate, endDate }, () => {});
}}
focusedInput={this.state.focusedInput} // PropTypes.oneOf([START_DATE, END_DATE]) or null,
onFocusChange={focusedInput => this.setState({ focusedInput })} // PropTypes.func.isRequired,
daySize={50}
noBorder={true}
isOutsideRange={() => false}
/>

最佳答案

你应该使用 isOutsideRange Prop 和 Moment.js用于处理可用的日期范围。例如,您可以通过这种方式只允许选择过去 30 天内的日期:

CodeSandbox

import React, { Component } from "react";
import moment from "moment";
import "react-dates/initialize";
import "react-dates/lib/css/_datepicker.css";
import { DateRangePicker } from "react-dates";
import { START_DATE, END_DATE } from "react-dates/constants";

export default class Dates extends Component {
state = {
startDate: null,
endDate: null,
focusedInput: null
};

onDatesChange = ({ startDate, endDate }) =>
this.setState({ startDate, endDate });

onFocusChange = focusedInput => this.setState({ focusedInput });

isOutsideRange = day =>
day.isAfter(moment()) || day.isBefore(moment().subtract(30, "days"));

render() {
const { startDate, endDate, focusedInput } = this.state;

return (
<DateRangePicker
startDate={startDate}
startDateId={START_DATE}
endDate={endDate}
endDateId={END_DATE}
onDatesChange={this.onDatesChange}
focusedInput={focusedInput}
onFocusChange={this.onFocusChange}
daySize={50}
noBorder={true}
isOutsideRange={this.isOutsideRange}
/>
);
}
}

关于javascript - 如何防止用户在 react 日期中选择高于结束日期的日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56871089/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com