# @uiw/react-date-input

> DateInput component

Latest version **4.22.3** (published 2023-11-29) · MIT license · 0 weekly downloads

## Install

```sh
npm install @uiw/react-date-input
pnpm add @uiw/react-date-input
yarn add @uiw/react-date-input
bun add @uiw/react-date-input
```

## Health

**Score 40/100 (D)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities; has provenance.

Warnings: low downloads.

Negative: abandoned.

## Facts

| | |
|---|---|
| Version | 4.22.3 |
| Published | 2023-11-29 |
| First published | 2020-05-01 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 6 |
| Unpacked size | 43.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Provenance | attested (GitHub Actions) |
| GitHub stars | 740 |
| Author | Kenny Wong |
| Maintainers | uiwjs, wcjiang |
| Keywords | date-input, react-date-input, design, uiw, uiw-react, react.js, react, react-component, component, components, ui, css, uikit, react-ui, framework, front-end, frontend |

## Links

- npm: https://www.npmjs.com/package/@uiw/react-date-input
- Repository: https://github.com/uiwjs/uiw
- Homepage: https://uiwjs.github.io/#/components/date-input
- Issues: https://github.com/uiwjs/uiw/issues
- Funding: https://jaywcjlove.github.io/#/sponsor
- npm.io page: https://npm.io/package/@uiw/react-date-input

## Dependencies (6)

- [@uiw/utils](https://npm.io/package/@uiw/utils.md) ^4.22.3
- [@uiw/formatter](https://npm.io/package/@uiw/formatter.md) ~2.0.0
- [@uiw/react-icon](https://npm.io/package/@uiw/react-icon.md) ^4.22.3
- [@uiw/react-input](https://npm.io/package/@uiw/react-input.md) ^4.22.3
- [@uiw/react-popover](https://npm.io/package/@uiw/react-popover.md) ^4.22.3
- [@uiw/react-date-picker](https://npm.io/package/@uiw/react-date-picker.md) ^4.22.3

## Alternatives

- [@progress/kendo-ooxml](https://npm.io/package/@progress/kendo-ooxml.md) — 152.1K weekly downloads
- [@progress/kendo-react-ripple](https://npm.io/package/@progress/kendo-react-ripple.md) — 8.0K weekly downloads
- [@progress/kendo-react-orgchart](https://npm.io/package/@progress/kendo-react-orgchart.md) — 4.3K weekly downloads
- [@praxisui/dynamic-fields](https://npm.io/package/@praxisui/dynamic-fields.md) — 2.4K weekly downloads
- [@progress/kendo-vue-listbox](https://npm.io/package/@progress/kendo-vue-listbox.md) — 932 weekly downloads

## Recent versions

- 4.22.3 (latest) — 2023-11-29
- 5.0.0-bate.2.1 (bate) — 2023-03-10
- 4.22.2 — 2023-11-09
- 4.22.1 — 2023-11-08
- 4.22.0 — 2023-09-19
- 4.21.28 — 2023-05-05
- 4.21.27 — 2023-04-21
- 4.21.26 — 2023-04-20
- 4.21.25 — 2023-03-14
- 4.21.24 — 2023-03-08
- 5.0.0-bate.2.0 — 2023-03-02
- 4.21.23 — 2022-12-21
- 4.21.22 — 2022-12-19
- 4.21.21 — 2022-12-19
- 5.0.0-bate-19 — 2022-12-12
- … 157 more at https://npm.io/package/@uiw/react-date-input/versions

## README

DateInput 日期输入框
===

[![Buy me a coffee](https://img.shields.io/badge/Buy%20me%20a%20coffee-048754?logo=buymeacoffee)](https://jaywcjlove.github.io/#/sponsor)
[![Open in unpkg](https://img.shields.io/badge/Open%20in-unpkg-blue)](https://uiwjs.github.io/npm-unpkg/#/pkg/@uiw/react-date-input/file/README.md)
[![NPM Downloads](https://img.shields.io/npm/dm/@uiw/react-date-input.svg?style=flat)](https://www.npmjs.com/package/@uiw/react-date-input)
[![npm version](https://img.shields.io/npm/v/@uiw/react-date-input.svg?label=@uiw/react-date-input)](https://npmjs.com/@uiw/react-date-input)

显示一个月的日历，并允许用户选择单个日期。

```jsx
import { DateInput, DateInputRange } from 'uiw';
// or
import DateInput,{ DateInputRange } from '@uiw/react-date-input';
// 在 `v4.13.0` 版本中增加 DateInputRange 组件
```

## 基本使用

```jsx mdx:preview&bg=#fff
import React from 'react';
import { DateInput, DateInputRange, Row, Col } from 'uiw';

export default function Demo () {
  const [dataRange,dataRangeSet] =React.useState(['2022/02/25 15:06:24','2022/02/27 14:47:32'])

  function onChange(selectedDate,dataRange) {
    console.log('selectedDate',selectedDate,dataRange)
  }

  return (
    <div>
      <Row style={{ marginBottom:10 }}>
        <Col style={{ width: 200 }} fixed>
          <DateInput
            value={new Date()}
            datePickerProps={{ todayButton: '今天' }}
            onChange={onChange}
          />
        </Col>
        <Col style={{ width: 200, marginLeft: 10 }} fixed>
          <DateInput
            value={new Date()}
            disabled
            onChange={onChange}
          />
        </Col>
        <Col style={{ width: 400, marginLeft: 10 }} fixed>
          <DateInputRange
            bodyStyle={{ width: 400 }}
            format="YYYY/MM/DD HH:mm:ss"
            value={dataRange}
            datePickerProps={{ todayButton: '今天',showTime:true }}
            onChange={onChange}
          />
        </Col>
      </Row>
    </div>
  )
}
```

## 在表单中使用

在 [`<Form />`](#/components/form) 表单中应用 [`<DateInput />`](#/components/date-input) 组件。

```jsx mdx:preview&bg=#fff
import React from 'react';
import { DateInput,DateInputRange, Notify, Button, Form, Row, Col } from 'uiw';

export default function Demo() {
  const form = React.useRef(null)

  const resetDateRange = () => {
    form.current.resetForm()
  }

  const setDateRange = () => {
    form.current.setFields({dateRange:[new Date(),new Date()]})
  }

  return (
    <Form
      ref={form}
      onSubmit={({ initial, current }) => {
        if (current.date) {
          Notify.success({
            title: '提交成功！',
            description: `表单提交时间成功，时间为：${current.date} range:${current.dateRange}`,
          });
        } else {
          Notify.error({
            title: '提交失败！',
            description: `表单提交时间成功，时间为：${current.date} range:${current.dateRange}，将自动填充初始化值！`,
          });
        }
      }}
      fields={{
        date: {
          initialValue: '2019/02/17',
          labelClassName: 'fieldLabel',
          labelFor: 'date-inline',
          children: <DateInput datePickerProps={{ todayButton: '今天' }} id="date-inline" />
        },
        dateRange: {
          initialValue: ['2019/02/17', '2019/02/20'],
          labelClassName: 'fieldLabel',
          labelFor: 'date-inline',
          children: <DateInputRange datePickerProps={{ todayButton: '今天' }} id="date-inline" />
        },
      }}
    >
      {({ fields, state, canSubmit }) => {
        return (
          <div>
            <Row>
              <Col style={{ width: 200 }} fixed>{fields.date}</Col>
              <Col fixed>{fields.dateRange}</Col>
            </Row>
            <Row>
              <Col style={{ overflow: 'auto', height: 130 }}>
                <pre>
                  {JSON.stringify(state, null, 2)}
                </pre>
              </Col>
            </Row>
            <Row gutter={10}>
              <Col>
                <Button disabled={!canSubmit()} type="primary" htmlType="submit">提交</Button>
                <Button onClick={resetDateRange} >重置</Button>
                <Button onClick={setDateRange}>setValue</Button>
              </Col>
            </Row>
          </div>
        )
      }}
    </Form>
  )
}
```

## 日期格式

```jsx mdx:preview&bg=#fff
import React from 'react';
import { DateInput } from 'uiw';

class Demo extends React.Component {
  onChange(selectedDate) {
    console.log('selectedDate:', selectedDate);
  }
  render() {
    return (
      <div style={{ width: 200 }}>
        <DateInput
          format="YYYY # MM # DD"
          datePickerProps={{ todayButton: '今天' }}
          onChange={this.onChange.bind(this)}
        />
      </div>
    )
  }
}
export default Demo;
```

## 日期时间设置

```jsx mdx:preview&bg=#fff
import React from 'react';
import { DateInput } from 'uiw';

class Demo extends React.Component {
  onChange(selectedDate) {
    console.log('selectedDate:', selectedDate);
  }
  render() {
    return (
      <div style={{ width: 200 }}>
        <DateInput
          format="YYYY/MM/DD HH:mm:ss"
          datePickerProps={{ showTime: true, todayButton: '今天' }}
          onChange={this.onChange.bind(this)}
        />
      </div>
    )
  }
}

export default Demo;
```

## 自动隐藏弹层

```jsx mdx:preview&bg=#fff
import React from 'react';
import { DateInput } from 'uiw';

class Demo extends React.Component {
  onChange(selectedDate) {
    console.log('selectedDate:', selectedDate);
  }
  render() {
    return (
      <div style={{ maxWidth: 200 }}>
        <DateInput
          autoClose={true}
          format="YYYY/MM/DD HH:mm:ss"
          datePickerProps={{ showTime: true, todayButton: '今天' }}
          onChange={this.onChange.bind(this)}
        />
      </div>
    )
  }
}

export default Demo;
```

## Props

| 参数 | 说明 | 类型 | 默认值 |
| ---- | ---- | ---- | ---- |
| value | 初始时间值 | Date | - |
| placeholder | 输入框提示文字 | String | - |
| allowClear | 是否显示清除按钮 | Boolean | true |
| format | 格式化时间，规则查看 [`<formatter>`](#/components/formatter) 文档 | String | `YYYY/MM/DD` |
| onChange | 选择一天时调用。 | Function(selectedDate:Date) | - |
| popoverProps | 将参数传递给 [`<Popover>`](#/components/popover) 组件 | Object | - |
| datePickerProps | 将参数传递给 [`<DatePicker>`](#/components/date-picker) 组件 | Object | - |
| disabled | 组件 [`<Input>`](#/components/input) 的属性，禁用日历 | Boolean | - |
| autoClose | 是否自动关闭弹层 | Boolean | false |

组件 `DateInput` 继承 `<Input>` 组件，更多属性文档请参考 [`<Input>`](#/components/input)。

## DateInputRange

在 `v4.13.0` 版本中增加 `DateInputRange` 组件。

| 参数 | 说明 | 类型 | 默认值 |
| ---- | ---- | ---- | ---- |
| value | 初始时间值 | Array<Date, string> | - |
| placeholder | 输入框提示文字 | String | - |
| allowClear | 是否显示清除按钮 | Boolean | true |
| format | 格式化时间，规则查看 [`<formatter>`](#/components/formatter) 文档 | String | `YYYY/MM/DD` |
| onChange | 选择一天时调用。 | Function(selectedDate:Date, dateRange: Array\<Date>) | - |
| popoverProps | 将参数传递给 [`<Popover>`](#/components/popover) 组件 | Object | - |
| datePickerProps | 将参数传递给 [`<DatePicker>`](#/components/date-picker) 组件 | Object | - |
| disabled | 组件 [`<Input>`](#/components/input) 的属性，禁用日历 | Boolean | - |
| bodyStyle | 选择框样式  | Object | { width: 300 } | - |

组件 `DateInputRange` 继承 `<Input>` 组件，更多属性文档请参考 [`<Input>`](#/components/input)。

---
_Source: https://npm.io/package/@uiw/react-date-input · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
