Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

3 лет назад
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  1. ![react-times](./doc/intro_src/react_times.png)
  2. [![npm version](https://badge.fury.io/js/react-times.svg)](https://badge.fury.io/js/react-times) [![Build Status](https://travis-ci.org/ecmadao/react-times.svg?branch=master)](https://travis-ci.org/ecmadao/react-times) [![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg)](http://standardjs.com) [![react-times](http://img.shields.io/npm/dm/react-times.svg)](https://www.npmjs.com/package/react-times) [![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg)](https://raw.githubusercontent.com/ecmadao/react-times/master/LICENSE)
  3. [![NPM](https://nodei.co/npm/react-times.png?downloads=true&downloadRank=true&stars=true)](https://nodei.co/npm/react-times)
  4. README:[中文版](./doc/README_CN.md)
  5. > A time picker react-component, no jquery-rely, writing in es6 standard style.
  6. **Check [here](./doc/CHANGELOG.md) to see changed props in new version.**
  7. ![react-times](./doc/intro_src/react-times.gif)
  8. # Online demo
  9. Check [here](https://ecmadao.github.io/react-times) to play online demo.
  10. # Play in local
  11. ```bash
  12. $ git clone https://github.com/ecmadao/react-times.git
  13. $ npm install
  14. $ npm run storybook
  15. ```
  16. # Install
  17. dependencies:
  18. - [`moment`](https://github.com/moment/moment/)
  19. - [`react`](https://github.com/facebook/react)
  20. - [`react-dom`](https://github.com/facebook/react)
  21. > No jQuery rely 😤😤😤
  22. So generally speaking, you should already have `react` & `react-dom` dependencies in your project. If not:
  23. ```bash
  24. $ npm install react react-dom moment moment-timezone --save-dev
  25. # and
  26. $ npm install react-times --save-dev
  27. ```
  28. # Config
  29. Cause I'm using `moment-timezone`, you need to be able to parse json file.
  30. Use webpack (version < 2) config as example:
  31. - [How should I use moment-timezone with webpack?](https://stackoverflow.com/questions/29548386/how-should-i-use-moment-timezone-with-webpack)
  32. ```bash
  33. $ npm i json-loader --save
  34. ```
  35. ```javascript
  36. // webpack.config.js
  37. // ATTENTION:
  38. // webpack >= v2.0.0 has native JSON support.
  39. // check here: https://github.com/webpack-contrib/json-loader/issues/65 for more information
  40. {
  41. module: {
  42. loaders: [
  43. {include: /\.json$/, loaders: ["json-loader"]}
  44. ]
  45. },
  46. resolve: {
  47. extensions: ['', '.json', '.jsx', '.js']
  48. }
  49. }
  50. ```
  51. # Usage
  52. This component has two themes now: Material Theme by default, or Classic Theme.
  53. > Always remember import css file when you use react-times
  54. ```javascript
  55. // basic usage
  56. // in some react component
  57. import React from 'react';
  58. import TimePicker from 'react-times';
  59. // use material theme
  60. import 'react-times/css/material/default.css';
  61. // or you can use classic theme
  62. import 'react-times/css/classic/default.css';
  63. export default class SomeComponent extends React.Component {
  64. onTimeChange(options) {
  65. // do something
  66. }
  67. onFocusChange(focusStatue) {
  68. // do something
  69. }
  70. render() {
  71. <TimePicker
  72. onFocusChange={this.onFocusChange.bind(this)}
  73. onTimeChange={this.onTimeChange.bind(this)}
  74. />
  75. }
  76. }
  77. ```
  78. > See more examples here:
  79. ```javascript
  80. // some config example
  81. render() {
  82. <TimePicker
  83. showTimezone // show the timezone, default false
  84. focused // whether to show timepicker modal after rendered. default false
  85. withoutIcon // whether to has time icon on button, default false
  86. colorPalette="dark" // main color, default "light"
  87. time="13:05" // initial time, default current time
  88. theme="material"
  89. // or
  90. // theme="classic"
  91. timeMode="12" // use 24 or 12 hours mode, default 24
  92. timezone="America/New_York" // what timezone to use, detects the user's local timezone by default
  93. />
  94. }
  95. ```
  96. > For more detail usage, you can visit [example](https://github.com/ecmadao/react-times/tree/master/examples) or see the source code.
  97. # Show time
  98. - 24 hours mode with Material Theme, light color by default
  99. ```javascript
  100. <TimePicker />
  101. ```
  102. ![24HoursMode](./doc/intro_src/24HoursMode.png)
  103. - 12 hours mode with Material Theme, light color by default
  104. ```javascript
  105. <TimePicker timeMode="12"/>
  106. ```
  107. ![12HoursMode](./doc/intro_src/12HoursMode.png)
  108. - 24 hours mode with Material Theme, dark color
  109. ```javascript
  110. <TimePicker colorPalette="dark"/>
  111. ```
  112. ![DarkColor](./doc/intro_src/DarkColor.png)
  113. - 24 hours mode, showing user current timezone. (Besides, your can use `timezone` props to custom timezone)
  114. ```javascript
  115. <TimePicker showTimezone={true}/>
  116. ```
  117. ![showTimezone](./doc/intro_src/24HoursMode-showTimezone.png)
  118. - 24 hours mode with Classic Theme, light color by default
  119. ```javascript
  120. <TimePicker theme="classic"/>
  121. ```
  122. ![24HoursMode-ClassicTheme](./doc/intro_src/24HoursMode-ClassicTheme.png)
  123. - 24 hours mode with Classic Theme, dark color
  124. ```javascript
  125. <TimePicker colorPalette="dark" theme="classic"/>
  126. ```
  127. ![24HoursMode-ClassicTheme-dark](./doc/intro_src/24HoursMode-ClassicTheme-dark.png)
  128. # APIs
  129. ## Props
  130. - `time`
  131. > Initial time, must be a string, with `${hour}:${minute}` format, default now (by using `moment()`):
  132. ```javascript
  133. // PropTypes.string
  134. time='11:11'
  135. time='11:01'
  136. time='1:01'
  137. time='1:1'
  138. ```
  139. - `timeFormat`
  140. > To show the time using custom style
  141. ```javascript
  142. // PropTypes.string
  143. // HH, MM means 24 hours mode
  144. // hh, mm means 12 hours mode
  145. timeFormat='HH:MM'
  146. timeFormat='hh:mm'
  147. timeFormat='H:M'
  148. timeFormat='h:m'
  149. // Warning:
  150. // If you are using 12 hours mode but with hh or mm format,
  151. // or using 24 hours mode with HH or MM format,
  152. // you will receive a warning on console, and force to use the timeMode props
  153. // So, if you wanna use hh:mm or h:m, you need to set timeMode props to 12
  154. // (cause timeMode default is 24)
  155. ```
  156. - `timeFormatter`
  157. > To show the time using custom style
  158. ```javascript
  159. // PropTypes.func
  160. timeFormatter={({ hour, minute, meridiem }) => `${hour} - ${minute}`}
  161. // Note:
  162. // If you both set timeFormat and timeFormatter props (and they all validate), component will use timeFormatter function
  163. ```
  164. - `focused`
  165. > Whether the timepicker pannel is focused or not when it did mount. Default `false`
  166. ```javascript
  167. // PropTypes.bool
  168. focused={false}
  169. focused={true}
  170. ```
  171. - `withoutIcon`
  172. > Whether the timepicker has a svg icon. Default `false`.
  173. ```javascript
  174. // PropTypes.bool
  175. withoutIcon={true}
  176. ```
  177. - `colorPalette`
  178. > The main color palette of picker pannel. Default `light`.
  179. ```javascript
  180. // PropTypes.string
  181. colorPalette="dark"
  182. colorPalette="light"
  183. ```
  184. - `timeMode`
  185. > Support "12" or "24" hours mode.
  186. ```javascript
  187. // PropTypes.string or PropTypes.number
  188. timeMode="24"
  189. timeMode=24
  190. timeMode="12"
  191. timeMode=12
  192. ```
  193. - `meridiem`
  194. > `PropTypes.string`, support "PM" or "AM" for 12 hours mode, default `AM`
  195. - `showTimezone`
  196. > `PropTypes.bool`, whether show user timezone or not, default `false`
  197. - `timezone`
  198. > `PropTypes.string`, change user timezone, default user current local timezone.
  199. - `trigger`
  200. > `React.component`, means a DOM which can control TimePicker Modal "open" or "close" status.
  201. ```javascript
  202. <TimePicker
  203. focused={focused}
  204. trigger={(
  205. <div
  206. onClick={this.handleFocusedChange.bind(this)} >
  207. click to open modal
  208. </div>
  209. )}
  210. />
  211. ```
  212. - `closeOnOutsideClick`
  213. > If you don't wanna close panel when outside click, you can use closeOnOutsideClick={false}. Default true
  214. ```
  215. <TimePicker
  216. closeOnOutsideClick={false}
  217. />
  218. ```
  219. - `disabled`
  220. > Disable component. Default false
  221. ```
  222. <TimePicker
  223. disabled={true}
  224. />
  225. ```
  226. - `draggable`
  227. 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`
  228. ```
  229. <TimePicker
  230. draggable={false}
  231. />
  232. ```
  233. - `language`
  234. > `React.string`, use different language. Default english.
  235. ```javascript
  236. /*
  237. * support
  238. * en: english
  239. * zh-cn: 中文简体
  240. * zh-tw: 中文繁体
  241. * fr: Français
  242. * ja: 日本語
  243. */
  244. <TimePicker
  245. language="zh-cn" // 中文简体
  246. />
  247. ```
  248. - `phrases`
  249. > `React.object`, specify text values to use for specific messages. By default, phrases will be set from defaults based on language.
  250. > Specify any of the available phrases you wish to override or all of them if your desired language is not yet supported.
  251. > See [language.js](./src/utils/language.js) for default phrases.
  252. ```javascript
  253. <TimePicker
  254. phrases={{
  255. confirm: 'Are you sure?',
  256. cancel: 'Do you want to cancel?',
  257. close: 'DONE',
  258. am: 'Ante Meridiem',
  259. pm: 'Post Meridiem'
  260. }}
  261. />
  262. ```
  263. - `minuteStep`
  264. > `React.number`, default `5`. It means the minium minute can change. You can set it to 1, 2, 3...
  265. ```javascript
  266. <TimePicker
  267. minuteStep={1}
  268. />
  269. ```
  270. - `timeConfig`
  271. > `React.object`, to config from, to, step limit for classic theme panel.
  272. ```javascript
  273. <TimePicker
  274. theme="classic"
  275. timeMode="12"
  276. timeConfig={{
  277. from: '08:00 PM',
  278. to: '08:00 AM',
  279. step: 1,
  280. unit: 'hour'
  281. }}
  282. />
  283. <TimePickerWrapper
  284. theme="classic"
  285. timeMode="24"
  286. timeConfig={{
  287. from: 9,
  288. to: 19,
  289. step: 30,
  290. unit: 'minutes'
  291. }}
  292. />
  293. ```
  294. - `limitDrag`
  295. > `React.bool`, default `false`. If `true`, it will limite the drag rotation by `minuteStep`
  296. ```javascript
  297. <TimePicker
  298. limitDrag
  299. />
  300. ```
  301. ## Callback
  302. - `onFocusChange`
  303. `PropTypes.func`
  304. > The callback func when component `focused` state is changed.
  305. - `onTimeChange`
  306. `PropTypes.func`
  307. > The callback func when component `hour` or `minute` or `AM/PM` state is changed.
  308. ```javascript
  309. onTimeChange(options) {
  310. // you can get hour, minute and meridiem here
  311. const {
  312. hour,
  313. minute,
  314. meridiem
  315. } = options;
  316. }
  317. ```
  318. - `onTimezoneChange`
  319. `PropTypes.func`
  320. > The callback func when timezone changed. Receives timezone object as argument with the following properties:
  321. * city
  322. * zoneAbbr
  323. * zoneName
  324. # Article
  325. - [一言不合造轮子--撸一个ReactTimePicker](https://github.com/ecmadao/Coding-Guide/blob/master/Notes/React/ReactJS/Write%20a%20React%20Timepicker%20Component%20hand%20by%20hand.md)
  326. # Todos
  327. - Test
  328. - [x] TimePicker Component
  329. - [x] PickerDragHandler Component
  330. - [x] PickerPointGenerator Component
  331. - [x] MaterialTheme Component
  332. - [x] TwelveHoursTheme Component
  333. - [x] PickerPoint Component
  334. - [x] OutsideClickHandler Component
  335. - [x] utils test
  336. - Color Palette (Now It has light and dark color)
  337. - [x] light
  338. - [x] dark
  339. - [ ] colorful
  340. - Themes
  341. - [x] Material Theme
  342. - [x] Classical Theme
  343. - Mode
  344. - [x] 12h mode
  345. - [x] 24h mode
  346. - Animations
  347. # Thx
  348. Thanks to the Airbnb's open source project: [react-dates](https://github.com/airbnb/react-dates), I have learn a lot from that. Thanks.
  349. # Core Contributors 🎉
  350. - **[carlodicelico](https://github.com/carlodicelico)**
  351. - **[erin-doyle](https://github.com/erin-doyle)**
  352. - **[MatthieuLemoine](https://github.com/MatthieuLemoine)**
  353. - **[naseeihity](https://github.com/naseeihity)**
  354. - **[shianqi](https://github.com/shianqi)**
  355. - **[thg303](https://github.com/thg303)**
  356. # License
  357. [MIT License](./LICENSE)