React-Modal:单击React-DatePicker时如何将模态到自动扩展? (附加代码)
单击React-DatePicker时,如何将模式转换为自动扩展?模态最初适合,但是当您单击日期字段和React-datepicker显示日历时,“ React-Mododal”对话框不会自动扩展?
代码:
import React, { useState } from 'react';
import Modal from 'react-modal';
import DatePicker from "react-datepicker";
import "react-datepicker/dist/react-datepicker.css";
const customStyles = {
content: {
top: '50%',
left: '50%',
right: 'auto',
bottom: 'auto',
marginRight: '-50%',
transform: 'translate(-50%, -50%)',
},
};
export default function TestPage2() {
const [modalIsOpen, setIsOpen] = React.useState(false);
const [startDate, setStartDate] = useState(new Date());
function openModal() {
setIsOpen(true);
}
function closeModal() {
setIsOpen(false);
}
return (
<div>
<button onClick={openModal}>Open Modal</button>
<Modal
isOpen={modalIsOpen}
onRequestClose={closeModal}
contentLabel="Example Modal"
style={customStyles}
>
<h3>Prior to Date Picker</h3>
<label htmlFor="Checkin Date">Checkin Date</label>
<DatePicker
selected={startDate}
// wrapperClassName="datePicker"
// className="form-control"
dateFormat="d MMMM yyyy"
name="checkinDate"
onChange={(date, event) => {
if (date) {
setStartDate(date)
}
}}
/>
<h3>After Date Picker</h3>
</Modal>
</div>
);
}
How to get modal to auto-expand when react-datepicker is clicked? Modal fits initially, but then when you click on the date field and the react-datepicker shows the calendar, the react-modal dialog does not auto-expand?
Code:
import React, { useState } from 'react';
import Modal from 'react-modal';
import DatePicker from "react-datepicker";
import "react-datepicker/dist/react-datepicker.css";
const customStyles = {
content: {
top: '50%',
left: '50%',
right: 'auto',
bottom: 'auto',
marginRight: '-50%',
transform: 'translate(-50%, -50%)',
},
};
export default function TestPage2() {
const [modalIsOpen, setIsOpen] = React.useState(false);
const [startDate, setStartDate] = useState(new Date());
function openModal() {
setIsOpen(true);
}
function closeModal() {
setIsOpen(false);
}
return (
<div>
<button onClick={openModal}>Open Modal</button>
<Modal
isOpen={modalIsOpen}
onRequestClose={closeModal}
contentLabel="Example Modal"
style={customStyles}
>
<h3>Prior to Date Picker</h3>
<label htmlFor="Checkin Date">Checkin Date</label>
<DatePicker
selected={startDate}
// wrapperClassName="datePicker"
// className="form-control"
dateFormat="d MMMM yyyy"
name="checkinDate"
onChange={(date, event) => {
if (date) {
setStartDate(date)
}
}}
/>
<h3>After Date Picker</h3>
</Modal>
</div>
);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将以下属性添加到CustomStyles中的内容对象中:
并更改React-DatePicker-Popper类的属性值:
codesandbox: https://codesandbox.io/s/awesome-feather-mj81tz
add the following property to your content object inside customStyles:
and change the property values of react-datepicker-popper class :
codesandbox: https://codesandbox.io/s/awesome-feather-mj81tz
几周前,我完全遇到了同一问题。我正在寻找一个简单的修复程序,但我没有找到一个。我所做的是将datepicker分为2个组件(+ redux)。
&lt; datePicker/&gt;
&lt; datePickerFloatingItem/&gt;
datePicker
datePicker只是输入字段,这就是您可以在整个React WebApp中使用的组件。这里最大的变化是,您需要与调度员一起执行动作以显示浮动物品。此外,您需要确定datepicker的X和Y坐标,以将浮动物品放置在正确的位置。
这是datePicker组件的外观(我已经删除了逻辑代码以不使所有人感到困惑):
datePickerFloingItem,
您只需要在应用程序中将datePickerFloingItem放置在您的应用程序中即可。
最好将其定位在app.js(root组件)上。
具有
位置也很重要:父元素的相对
定义位置:datePickerFloingItem的固定
。现在,您可以使用top:
和左:
带有datePicker的坐标,这就是datePickerFloingItem的样子(我还删除了不必要的代码)为了保持更容易理解)
Redux,
我不得不将一些东西移到Redux商店,以确保 floatingdatepicker 以及 datepicker 有机会以某种方式交流
我的redux商店很简单:
正如您在图像上所看到的那样,它实际上是在模态内工作:

我知道这种方法很耗时,但我仍然希望它仍然对您有所帮助,我也希望您的眼睛不要看到基于班级的组件。
I exactly faced the same issue a few weeks ago. I was looking for a easy fix but I' didn't found one. What I did is to split up the Datepicker into 2 components (+ redux).
<Datepicker />
<DatepickerFloatingItem/>
Datepicker
The datepicker is simply the input field and that's the component you can use throughout your react webapp. The biggest change here is that you need to run an action with the dispatcher to show the floating item. Additionally you need to determine the X and Y coordinates of your Datepicker to place the Floating Item at the correct spot.
Here is how the Datepicker component could look like (I've deleted the logic code to not confuse everyone):
DatepickerFloatingItem
You only need to position the DatepickerFloatingItem once in your application.
It's best to position it at App.js (the root component).
It's also important to have
position: relative
for the parent element and defineposition: fixed
for the DatepickerFloatingItem. Now you can easily position your floating element usingtop:
andleft:
with the coordinates of the DatepickerAnd this is how the DatepickerFloatingItem could look like (I also removed the unnecessary code to keep it more understandable)
Redux
I had to move some stuff to the Redux store to ensure that the FloatingDatePicker as well as the Datepicker have chance to communicate somehow
I've kept the redux store pretty straight forward:
And as you can see at the image, it's actually working inside a modal:

I know this approach is pretty time consuming, but still I hope it was still helping you somehow and I also hope your eyes don't burn from seeing class based components.