README:中文版
A time picker react-component, no jquery-rely, writing in es6 standard style.
Check here to see changed props in new version.
Online demo
Check here to play online demo.
Play in local
$ git clone https://github.com/ecmadao/react-times.git
$ npm install
$ npm run storybook
Install
dependencies:
No jQuery rely 😤😤😤
So generally speaking, you should already have react
& react-dom
dependencies in your project. If not:
$ npm install react react-dom moment moment-timezone --save-dev
$ npm install react-times --save-dev
Config
Cause I'm using moment-timezone
, you need to be able to parse json file.
Use webpack (version < 2) config as example:
$ npm i json-loader --save
{
module: {
loaders: [
{include: /\.json$/, loaders: ["json-loader"]}
]
},
resolve: {
extensions: ['', '.json', '.jsx', '.js']
}
}
Usage
This component has two themes now: Material Theme by default, or Classic Theme.
Always remember import css file when you use react-times
import React from 'react';
import TimePicker from 'react-times';
import 'react-times/css/material/default.css';
import 'react-times/css/classic/default.css';
export default class SomeComponent extends React.Component {
onTimeChange(options) {
}
onFocusChange(focusStatue) {
}
render() {
<TimePicker
onFocusChange={this.onFocusChange.bind(this)}
onTimeChange={this.onTimeChange.bind(this)}
/>
}
}
See more examples here:
render() {
<TimePicker
showTimezone // show the timezone, default false
focused // whether to show timepicker modal after rendered. default false
withoutIcon // whether to has time icon on button, default false
colorPalette="dark" // main color, default "light"
time="13:05" // initial time, default current time
theme="material"
// or
// theme="classic"
timeMode="12" // use 24 or 12 hours mode, default 24
timezone="America/New_York" // what timezone to use, detects the user's local timezone by default
/>
}
For more detail usage, you can visit example or see the source code.
Show time
- 24 hours mode with Material Theme, light color by default
<TimePicker />
- 12 hours mode with Material Theme, light color by default
<TimePicker timeMode="12"/>
- 24 hours mode with Material Theme, dark color
<TimePicker colorPalette="dark"/>
- 24 hours mode, showing user current timezone. (Besides, your can use
timezone
props to custom timezone)
<TimePicker showTimezone={true}/>
- 24 hours mode with Classic Theme, light color by default
<TimePicker theme="classic"/>
- 24 hours mode with Classic Theme, dark color
<TimePicker colorPalette="dark" theme="classic"/>
APIs
Props
Initial time, must be a string, with ${hour}:${minute}
format, default now (by using moment()
):
time='11:11'
time='11:01'
time='1:01'
time='1:1'
To show the time using custom style
timeFormat='HH:MM'
timeFormat='hh:mm'
timeFormat='H:M'
timeFormat='h:m'
To show the time using custom style
timeFormatter={({ hour, minute, meridiem }) => `${hour} - ${minute}`}
Whether the timepicker pannel is focused or not when it did mount. Default false
focused={false}
focused={true}
Whether the timepicker has a svg icon. Default false
.
withoutIcon={true}
The main color palette of picker pannel. Default light
.
colorPalette="dark"
colorPalette="light"
Support "12" or "24" hours mode.
timeMode="24"
timeMode=24
timeMode="12"
timeMode=12
PropTypes.string
, support "PM" or "AM" for 12 hours mode, default AM
PropTypes.bool
, whether show user timezone or not, default false
PropTypes.string
, change user timezone, default user current local timezone.
React.component
, means a DOM which can control TimePicker Modal "open" or "close" status.
<TimePicker
focused={focused}
trigger={(
<div
onClick={this.handleFocusedChange.bind(this)} >
click to open modal
</div>
)}
/>
If you don't wanna close panel when outside click, you can use closeOnOutsideClick={false}. Default true
<TimePicker
closeOnOutsideClick={false}
/>
Disable component. Default false
<TimePicker
disabled={true}
/>
If you don't want to drag the pointer, then you can set draggable
props to false
, then users can only use click to change time. Default true
<TimePicker
draggable={false}
/>
React.string
, use different language. Default english.
<TimePicker
language="zh-cn"
/>
React.object
, specify text values to use for specific messages. By default, phrases will be set from defaults based on language.
Specify any of the available phrases you wish to override or all of them if your desired language is not yet supported.
See language.js for default phrases.
<TimePicker
phrases={{
confirm: 'Are you sure?',
cancel: 'Do you want to cancel?',
close: 'DONE',
am: 'Ante Meridiem',
pm: 'Post Meridiem'
}}
/>
React.number
, default 5
. It means the minium minute can change. You can set it to 1, 2, 3...
<TimePicker
minuteStep={1}
/>
React.object
, to config from, to, step limit for classic theme panel.
<TimePicker
theme="classic"
timeMode="12"
timeConfig={{
from: '08:00 PM',
to: '08:00 AM',
step: 1,
unit: 'hour'
}}
/>
<TimePickerWrapper
theme="classic"
timeMode="24"
timeConfig={{
from: 9,
to: 19,
step: 30,
unit: 'minutes'
}}
/>
React.bool
, default false
. If true
, it will limite the drag rotation by minuteStep
<TimePicker
limitDrag
/>
Callback
PropTypes.func
The callback func when component focused
state is changed.
PropTypes.func
The callback func when component hour
or minute
or AM/PM
state is changed.
onTimeChange(options) {
const {
hour,
minute,
meridiem
} = options;
}
PropTypes.func
The callback func when timezone changed. Receives timezone object as argument with the following properties:
Article
Todos
Thx
Thanks to the Airbnb's open source project: react-dates, I have learn a lot from that. Thanks.
Core Contributors 🎉
License
MIT License