Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@uiw/react-time-picker

Package Overview
Dependencies
Maintainers
2
Versions
172
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@uiw/react-time-picker

TimePicker component

  • 4.22.3
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
2
Created
Source

TimePicker 时间选择器

Buy me a coffee Open in unpkg NPM Downloads npm version

当用户需要输入一个时间,可以点击标准输入框,弹出时间面板进行选择。

import { TimePicker } from 'uiw';
// or
import TimePicker from '@uiw/react-time-picker';

基础用法

import React from 'react';
import { TimePicker, Row, Col } from 'uiw';

const Demo = () => (
  <Row gutter={10}>
    <Col style={{ width: 200 }} fixed>
      <TimePicker
        onChange={(formatDate, date) => {
          console.log('--->', formatDate, date);
        }}
      />
    </Col>
    <Col style={{ width: 200 }} fixed>
      <TimePicker format="HH:mm" precision="minute" />
    </Col>
    <Col style={{ width: 200 }} fixed>
      <TimePicker format="HH" precision="hour" />
    </Col>
  </Row>
)
export default Demo;

设置初始值

import React from 'react';
import { TimePicker, Row, Col } from 'uiw';

const Demo = () => {
  const value = new Date(2018, 1, 24, 4,5,35);
  return (
    <Row gutter={10}>
      <Col style={{ width: 200 }} fixed>
        <TimePicker value={value} />
      </Col>
      <Col style={{ width: 200 }} fixed>
        <TimePicker value={value} format="HH:mm" precision="minute" />
      </Col>
      <Col style={{ width: 200 }} fixed>
        <TimePicker value={value} format="HH" precision="hour" />
      </Col>
    </Row>
  )
}
export default Demo;

设置按钮大小

import React from 'react';
import { TimePicker, Row, Col } from 'uiw';

const Demo = () => {
  const value = new Date(2018, 1, 24, 4,5,35);
  return (
    <Row gutter={10}>
      <Col style={{ width: 200 }} fixed>
        <TimePicker size="small" value={value} />
      </Col>
      <Col style={{ width: 200 }} fixed>
        <TimePicker value={value} format="HH" precision="hour" />
      </Col>
      <Col style={{ width: 200 }} fixed>
        <TimePicker size="large" value={value} format="HH:mm" precision="minute" />
      </Col>
    </Row>
  )
}
export default Demo;

表单中应用

在表单返回的数据,并没有将 format 格式化后的数据返回给你,而是返回的一个 Date,你可以通过 formatter 重新格式化。

import React from 'react';
import { TimePicker, Notify, Row, Col, Form, Button } from 'uiw';

const Demo = () => (
  <div>
    <Form
      onSubmit={({initial, current}) => {
        console.log('-->>', initial, current);
        if(current.date) {
          Notify.success({
            title: '提交成功!',
            description: `表单提交时间成功,时间为:${formatter('HH:mm:ss', current.date)}`,
          });
        } else {
          Notify.error({
            title: '提交失败!',
            description: <span>表单提交时间成功,时间为:<b></b>,将自动填充初始化值!</span>,
          });
        }
      }}
      fields={{
        date: {
          labelClassName: 'fieldLabel',
          labelFor: 'date-inline',
          children: <TimePicker />
        },
      }}
    >
      {({ fields, state, canSubmit }) => {
        return (
          <Row gutter={10}>
            <Col style={{ width: 200 }} fixed>{fields.date}</Col>
            <Col>
              <Button disabled={!canSubmit()} type="primary" htmlType="submit">提交</Button>
            </Col>
          </Row>
        )
      }}
    </Form>
  </div>
)
export default Demo;

禁用

可以使用 disabledHours disabledMinutes disabledSeconds 禁用部分时间选择。

import React from 'react';
import { TimePicker, Row, Col } from 'uiw';

const Demo = () => (
  <Row gutter={10} style={{ maxWidth: 360 }}>
      <Col style={{ width: 200 }} fixed>
      <TimePicker
        disabledMinutes={(minute, type) => {
          if (minute % 15 !== 0) {
            return true;
          }
        }}
        disabledHours={(hour, type, date) => {
          // console.log('~~:', hour, type, date);
          if (hour < 3) {
            return true;
          }
        }}
      />
    </Col>
    <Col style={{ width: 200 }} fixed>
      <TimePicker disabled value={new Date(2018, 1, 24, 4,5,35)} />
    </Col>
  </Row>
)
export default Demo;

不显示禁用

可以使用 hideDisabled 将禁用的部分时间隐藏。

import React from 'react';
import { TimePicker, Row, Col } from 'uiw';

const Demo = () => (
  <Row gutter={10} style={{ maxWidth: 360 }}>
    <Col style={{ width: 200 }} fixed>
      <TimePicker
        hideDisabled
        disabledMinutes={(minute, type) => {
          if (minute % 15 !== 0) {
            return true;
          }
        }}
        disabledHours={(hour, type, date) => {
          // console.log('~~:', hour, type, date);
          if (hour < 3) {
            return true;
          }
        }}
      />
    </Col>
  </Row>
)
export default Demo;

间隔时间

可以使用 hideDisabled 将禁用的部分时间隐藏。

import React from 'react';
import { TimePicker, Row, Col } from 'uiw';

const Demo = () => (
  <Row gutter={10} style={{ maxWidth: 360 }}>
    <Col style={{ width: 200 }} fixed>
      <TimePicker
        hideDisabled
        disabledMinutes={(minute, date) => {
          if (minute % 15 !== 0) {
            return true;
          }
        }}
        disabledSeconds={(second, date) => {
          if (second % 15 !== 0) {
            return true;
          }
        }}
      />
    </Col>
  </Row>
)
export default Demo;

Props

参数说明类型默认值
value初始时间值Date-
placeholder输入框提示文字String-
format格式化时间,规则查看 formatter 文档FunctionHH:mm:ss
precision选择时间精确度Enum{hour, minute, second}false
disabled禁用全部操作Booleanfalse
disabledHours禁止选择部分小时选项Function(hour,
type{Hours, Minutes, Seconds},
selectedDate)
-
disabledMinutes禁止选择部分分钟选项Function(minute,
type{Hours, Minutes, Seconds},
selectedDate)
-
disabledSeconds禁止选择部分秒选项Function(second,
type{Hours, Minutes, Seconds},
selectedDate)
-
hideDisabled不可选择的项隐藏Booleanfalse
onChange时间选择的回调函数Function(formatDate, Date,
type{Hours, Minutes, Seconds},
, num, disableds)
-

uiw@3.3.0+ 组件集成部分 Input,不再通过 inputProps 属性传值,更多属性请参考 Input

Props.inputProps

uiw@3.3.0+ 将不支持此属性

参数说明类型默认值
inputProps将参数传递给 <Input> 组件Object-

Props.popoverProps

参数说明类型默认值
popoverProps将参数传递给 <Popover> 组件Object-

Keywords

FAQs

Package last updated on 29 Nov 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc