我正在使用 react-flatpickr range 插件,提供以下代码:
索引.js
import React from "react";
import ReactDOM from "react-dom";
import DatePicker from "./datePicker.js";
import "./styles.css";
function App() {
return (
<div className="App">
<DatePicker
options={{
dateFormat: "d-M-Y",
defaultDate: "",
disableMobile: "true",
maxDate: "today"
}}
fromDateID="DashboardEndDatePicker"
selectValue={[]}
placeholder="From Date"
/>
<input id="DashboardEndDatePicker" placeholder="To Date" />
</div>
);
}
const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);
日期选择器.js
import React, { Component } from "react";
import Flatpickr from "react-flatpickr";
import rangePlugin from "flatpickr/dist/plugins/rangePlugin";
import "flatpickr/dist/flatpickr.min.css";
import "flatpickr/dist/themes/light.css";
export default class DatePicker extends Component {
constructor(props) {
super(props);
this.setDate = this.setDate.bind(this);
this.clearDate = this.clearDate.bind(this);
this.state = {
selectValue: props.selectValue ? props.selectValue : "",
options: props.options
? Object.assign({}, props.options, {
plugins: [new rangePlugin({ input: "#" + props.fromDateID })]
})
: { plugins: [new rangePlugin({ input: "#" + props.fromDateID })] },
disabled: props.disabled ? props.disabled : false,
placeholder: props.placeholder ? props.placeholder : ""
};
}
componentWillReceiveProps(newProps) {
this.setState({
selectValue: newProps.selectValue ? newProps.selectValue : "",
options: newProps.options
? Object.assign({}, newProps.options, {
plugins: [new rangePlugin({ input: "#" + newProps.fromDateID })]
})
: { plugins: [new rangePlugin({ input: "#" + newProps.fromDateID })] },
disabled: newProps.disabled ? newProps.disabled : false,
placeholder: newProps.placeholder ? newProps.placeholder : ""
});
}
clearDate() {
this.refs.refDatePicker.flatpickr.clear();
}
setDate(newValue) {
this.setState({
selectValue: newValue ? newValue : ""
});
if (this.props.onChange) {
this.props.onChange(newValue);
}
}
render() {
return (
<Flatpickr
className="form-control clickable"
disabled={this.state.disabled}
ref="refDatePicker"
placeholder={this.state.placeholder}
options={this.state.options}
value={this.state.selectValue}
onChange={this.setDate}
/>
);
}
}
当我选择“to date”时,它也在“from date”字段中设置相同的值。提供下图:
我为此创建了一个代码沙箱:
不知道我在这里缺少什么。