gpt4 book ai didi

javascript - 日期选择器月/年在 iOS 上不起作用

转载 作者:太空狗 更新时间:2023-10-29 14:18:41 26 4
gpt4 key购买 nike

我正在使用 react-day-picker 包来选择包含年/月 Prop 的日期。但是,年/月下拉列表在 iOS 移动浏览器上无法正常工作:

import React from 'react';
import ReactDOM from "react-dom";
import DayPickerInput from 'react-day-picker/DayPickerInput';
import 'react-day-picker/lib/style.css';

const currentYear = new Date().getFullYear();
const fromMonth = new Date(currentYear, 0);
const toMonth = new Date(currentYear + 10, 11);

function YearMonthForm({ date, localeUtils, onChange }) {
const months = localeUtils.getMonths();

const years = [];
for (let i = fromMonth.getFullYear(); i <= toMonth.getFullYear(); i += 1) {
years.push(i);
}

const handleChange = function handleChange(e) {
const { year, month } = e.target.form;
onChange(new Date(year.value, month.value));
};

return (
<form className="DayPicker-Caption">
<select name="month" onChange={handleChange} value={date.getMonth()}>
{months.map((month, i) => (
<option key={month} value={i}>
{month}
</option>
))}
</select>
<select name="year" onChange={handleChange} value={date.getFullYear()}>
{years.map(year => (
<option key={year} value={year}>
{year}
</option>
))}
</select>
</form>
);
}

export default class Example extends React.Component {
constructor(props) {
super(props);
this.handleYearMonthChange = this.handleYearMonthChange.bind(this);
this.state = {
month: fromMonth,
};
}
handleYearMonthChange(month) {
this.setState({ month });
}
render() {
const dayPickerProps = {
month: this.state.month,
fromMonth: fromMonth,
toMonth: toMonth,
captionElement: ({ date, localeUtils }) => (
<YearMonthForm
date={date}
localeUtils={localeUtils}
onChange={this.handleYearMonthChange}
/>
)
};

return (
<div className="YearNavigation">
<DayPickerInput
showOverlay={true}
dayPickerProps={dayPickerProps}
/>
</div>
);
}
}
ReactDOM.render(<Example />, document.getElementById("root"));

您可以在这个链接上看到演示(必须在 iOS 设备上打开它):

https://codesandbox.io/s/0y84wrp8mn

有什么解决方法吗?

最佳答案

问题是默认的 keepFocus 设置为 true。像示例组件中那样更改渲染方法 (keepFocus={false}):

return (
<div className="YearNavigation">
<DayPickerInput
showOverlay={true}
keepFocus={false}
dayPickerProps={dayPickerProps}
/>
</div>
);

关于javascript - 日期选择器月/年在 iOS 上不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55156282/

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