[英]REACT- How to set enddate same as startdate by default but control if enddate not before startdate?
if a user select a starting date, by default I would like that this selection would be the same for the ending date.如果用户选择开始日期,默认情况下我希望此选择与结束日期相同。 And then if the user wants to change this ending date, he can.
然后,如果用户想要更改此结束日期,他可以。 For now, I can select the starting date and the ending date.
现在,我可以选择开始日期和结束日期。
And putting a condition on my date ie not able to put an ending date that is before a starting date.并在我的日期设置条件,即不能将结束日期设置在开始日期之前。
Here is my code :这是我的代码:
export default function SelectionChoice({ next, selChoice2, setSelChoice2 }) {
const [summary, setSummary] = useState("");
const [start, setStart] = useState("");
const [end, setEnd] = useState("");
const [selChoice1, setSelChoice1] = useState();
const [data, setData] = useState([])
useEffect(() => {
myApi.functionA()
.then((res) => {
console.log(res);
setData(res.data.data);
})
.catch((err) => {
console.log(err);
});
}, []);
...
const choice1 = data?.map((item) => {
return {
label: item.name,
value: item.id
};
});
const choice2 = data?.flatMap((item) => {
return item.choice2.map((c) => ({
label: c.name,
value: c.id
}));
});
const textChange = (e) => {
if (e?.target?.id === undefined) return setSelChoice1(e);
if (e?.target?.id === undefined) return setSelChoice2(e);
switch (e.target.id) {
...
case "start":
setStart(e.target.value);
break;
case "end":
setEnd(e.target.value);
break;
default:
}
};
return (
<>
<form>
<div >
Choose choice1 and choice2
</div>
<div>
<label>
Choice1:
<CustomDropdown
options={choice1}
value={selChoice1}
setValue={setSelChoice1}
isMulti={true}
/>
</label>
<label>
Choice2:
<CustomDropdown
options={choice2}
value={selChoice2}
setValue={setSelChoice2}
isMulti={true}
/>
</label>
</div>
<div>
<label>
Start:
<div>
<input
type="date"
name="start"
value={start}
onChange={textChange}
id="start"
/>
</div>
</label>
<label>
End:
<div>
<input
type="date"
name="end"
value={end}
onChange={textChange}
id="end"
/>
</div>
</label>
<p className="formfield">
<label
for="summary"
>
Description :
<textarea
value={summary}
onChange={textChange}
name="summary"
id="summary"
cols="10"
rows="10"
/>
</label>
</p>
</div>
</form>
<button
onClick={() =>
next({
...,
start,
end
})
}
>
Next
</button>
</>
);
}
You can check if the end date is having any value while updating the start date and set it if it is not having any value.您可以在更新开始日期时检查结束日期是否有任何值,如果没有任何值则设置它。
switch (e.target.id) {
case "start":
setStart(e.target.value);
if (!end) {
setEnd(e.target.value);
}
break;
case "end":
if (e.target.value >= start) {
setEnd(e.target.value);
} else {
//flash error message
}
break;
default:
}
I would like you update the code like below for checking possible cases.我希望您更新如下代码以检查可能的情况。 And I also updated the Async Function with async/await.
我还用 async/await 更新了异步函数。
export default function SelectionChoice({ next, selChoice2, setSelChoice2 }) {
...
const [start, setStart] = useState("");
const [end, setEnd] = useState("");
...
useEffect(() => {
/*
myApi.functionA()
.then((res) => {
console.log(res);
setData(res.data.data);
})
.catch((err) => {
console.log(err);
});
*/
const getData = async () => {
try {
const res = await myApi.functionA();
setData(res.data.data);
} catch(err) {
console.log(err)
}
}
getData();
}, []);
const textChange = (e) => {
switch (e.target.id) {
case "start":
setStart(e.target.value);
const startDate = new Date(e.target.value).getTime();
const endDate = new Date(end).getTime();
// Check if end date is not set
// Or End Date is before Start Date
if (!!end || startDate > endDate) {
setEnd(e.target.value)
}
break;
case "end":
const endDate = new Date(e.target.value).getTime();
const startDate = new Date(start).getTime();
// Check End Date is before the Start Date
if (endDate < startDate) {
setEnd(start);
} else {
setEnd(e.target.value);
}
break;
default:
break;
}
};
return (
...
)
Hope this would help you even a little.希望这会对您有所帮助。
Thanks谢谢
请注意您的结束日期不要早于您的开始日期
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.