【问题标题】:Date picker month / year doesn't work on iOS日期选择器月/年在 iOS 上不起作用
【发布时间】:2019-08-04 23:30:46
【问题描述】:

我正在使用react-day-picker 包来选择包含年/月道具的日期。但是,年/月下拉菜单在 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

有什么解决方法吗?

【问题讨论】:

  • 现在检查它“codesandbox.io/s/rrkovxzzp4”它应该可以工作
  • @ShivaKumarN 仍然不起作用。
  • 在 Safari 中运行良好
  • @ShivaKumarN 在 safari mobile 和 chrome 中都不起作用。
  • 好吧,试一试:添加 #'npm install --save babel-polyfill' 并在你的项目的初始入口点导入它,例如 App.js/index.js #import "babel-polyfill "

标签: javascript html css reactjs react-day-picker


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-17
    • 2013-10-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多