我正在尝试创建一个简单的日期范围选择器,现在将其用于将下拉列表中的日期传递到sql查询中,我只想处理它并传递它以返回带有所选择值的h1标记。今天|最近7天等。如果您的帮助包括带有js时刻的转换,那将是一个好处。这是我的代码 这是指向codesandbox https://codesandbox.io/s/gotrax-daterange-95u2j?fontsize=14
的链接import React, { Component } from "react";
import ReactDOM from "react-dom";
import { Header, Menu, Dropdown, Icon, Button } from "semantic-ui-react";
import moment from "moment";
import "semantic-ui/dist/semantic.min.css";
export default class DatePicker extends Component {
constructor(props) {
super(props);
this.state = {
date: "",
wasSubmitted: false,
dateRange: [
{ key: "Today", text: "Today", value: `${moment()}` },
{ key: "Yesterday", text: "Yesterday", value: "yesterday" },
{ key: "Last 7 Days", text: "Last 7 Days", value: "last 7 days" },
{ key: "Last 30 Days", text: "Last 30 Days", value: "last 30 days" },
{ key: "This Month", text: "This Month", value: "this month" },
{ key: "Last Month", text: "Last Month", value: "last month" },
{ key: "Custom Range", text: "Custom Range", value: "custom range" }
]
};
this.handleChange = this.handleChange.bind(this);
this.handleSubmit = this.handleSubmit.bind(this);
}
handleSubmit = event => {
event.preventDefault();
this.setState({ wasSubmitted: true });
console.log(this.state.dateRange);
};
handleChange = e => {
this.setState({
date: e._d
});
};
render() {
const { date, wasSubmitted } = this.state;
return (
<>
{!wasSubmitted && (
<div>
<div>
<Menu secondary borderless={true} fluid={true}>
<Menu.Item>
<Header>Date Range:</Header>
</Menu.Item>
<Menu.Item className="width15em">
<Icon name="calendar" />
<Dropdown
placeholder="Pick Date Range"
options={this.state.dateRange}
/>
</Menu.Item>
<Menu.Menu position="right" borderless={true}>
<Menu.Item>
<Button
primary
size="small"
onSubmit={this.handleSubmit}
type="submit"
value={this.state.dateRange}
onChange={this.handleChange}
>
Update
</Button>
</Menu.Item>
<Menu.Item>
<Button color="green" size="small">
Export CSV
</Button>
</Menu.Item>
</Menu.Menu>
</Menu>
</div>
</div>
)}
</>
);
}
}
const rootElement = document.getElementById("root");
ReactDOM.render(<DatePicker />, rootElement);
答案 0 :(得分:1)
我已经尝试了。我不明白这个月的含义是什么(范围?)对于自定义范围,您需要添加适当的计算器或输入框:
<div class="wrapper">
<div class="bar1" id="bar1" onclick="change('bar1','bar2')">
Option1
</div>
<div class="bar2" id="bar2" onclick="change('bar2','bar1')">
Option2
</div>
</div>
答案 1 :(得分:1)
为了更好地回答这个问题,我计算了日期,对于自定义日期范围,您需要一个日期范围选择器,例如https://react-day-picker.js.org/examples/selected-range/
我还添加了一个带有onSubmit的表单,以便更新console.logs
您想要的内容。
import React, { Component } from "react";
import ReactDOM from "react-dom";
import { Header, Menu, Dropdown, Icon, Button } from "semantic-ui-react";
import moment from "moment";
import "semantic-ui/dist/semantic.min.css";
export default class DatePicker extends Component {
constructor(props) {
super(props);
this.state = {
date: "",
wasSubmitted: false,
dateRange: [
{
key: "Today",
text: "Today",
value: {
start: moment().format("YYYY-MM-DD"),
end: moment().format("YYYY-MM-DD")
}
},
{
key: "Yesterday",
text: "Yesterday",
value: {
start: moment()
.subtract(1, "d")
.format("YYYY-MM-DD"),
end: moment()
.subtract(1, "d")
.format("YYYY-MM-DD")
}
},
{
key: "Last 7 Days",
text: "Last 7 Days",
value: {
start: moment()
.subtract(7, "d")
.format("YYYY-MM-DD"),
end: moment().format("YYYY-MM-DD")
}
},
{
key: "Last 30 Days",
text: "Last 30 Days",
value: {
start: moment()
.subtract(30, "d")
.format("YYYY-MM-DD"),
end: moment().format("YYYY-MM-DD")
}
},
{
key: "This Month",
text: "This Month",
value: {
start: moment()
.startOf("month")
.format("YYYY-MM-DD"),
end: moment().format("YYYY-MM-DD")
}
},
{
key: "Last Month",
text: "Last Month",
value: {
start: moment()
.subtract(1, "month")
.startOf("month")
.format("YYYY-MM-DD"),
end: moment()
.subtract(1, "month")
.endOf("month")
.format("YYYY-MM-DD")
}
},
{ key: "Custom Range", text: "Custom Range", value: "custom range" }
]
};
// this.handleChange = this.handleChange.bind(this);
// this.handleSubmit = this.handleSubmit.bind(this);
}
handleSubmit = event => {
console.log(this.state.date);
event.preventDefault();
this.setState({ wasSubmitted: true });
};
handleChange = (e, d) => {
e.preventDefault();
e.persist();
this.setState({
date: d.value
});
};
render() {
const { date, wasSubmitted } = this.state;
return (
<div>
<div>
<Menu secondary borderless={true} fluid={true}>
<Menu.Item>
<Header>Date Range:</Header>
</Menu.Item>
<Menu.Item className="width15em">
<Icon name="calendar" />
<Dropdown
placeholder="Pick Date Range"
options={this.state.dateRange}
onChange={this.handleChange}
/>
</Menu.Item>
<Menu.Menu position="right" borderless="true">
<Menu.Item>
<form onSubmit={this.handleSubmit}>
<Button
primary
size="small"
onSubmit={this.handleSubmit}
type="submit"
value={this.state.date}
>
Update
</Button>
</form>
</Menu.Item>
<Menu.Item>
<Button color="green" size="small">
Export CSV
</Button>
</Menu.Item>
</Menu.Menu>
</Menu>
</div>
</div>
);
}
}
const rootElement = document.getElementById("root");
ReactDOM.render(<DatePicker />, rootElement);