Skip to content

Commit b05fd10

Browse files
authored
feat: Support disabledTime on TimePicker (#332)
* feat: disabledTime can handle time picker * test: add disabledTime test * chore: warning for legacy
1 parent 13ca887 commit b05fd10

File tree

12 files changed

+303
-136
lines changed

12 files changed

+303
-136
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ render(<Picker />, mountNode);
103103
| renderExtraFooter | (mode) => React.Node | | extra footer |
104104
| onSelect | Function(date: moment) | | a callback function, can be executed when the selected time |
105105
| onPanelChange | Function(value: moment, mode) | | callback when picker panel mode is changed |
106-
| onMouseDown | (event:React.MouseEvent\<HTMLInputElement>) => void | | callback when executed onMouseDown evnent |
106+
| onMouseDown | (event:React.MouseEvent\<HTMLInputElement>) => void | | callback when executed onMouseDown event |
107107
| direction | String: ltr or rtl | | Layout direction of picker component, it supports RTL direction too. |
108108

109109
### RangePicker

assets/index.less

Lines changed: 43 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,10 @@
3030
border: 1px solid blue;
3131
}
3232
&-panel {
33-
border: 1px solid #666;
34-
background: @background-color;
3533
display: inline-block;
3634
vertical-align: top;
35+
background: @background-color;
36+
border: 1px solid #666;
3737

3838
&-focused {
3939
border-color: blue;
@@ -73,8 +73,8 @@
7373
text-align: center;
7474

7575
> button {
76-
border: 0;
7776
padding: 0;
77+
border: 0;
7878
}
7979
}
8080
}
@@ -88,19 +88,19 @@
8888
}
8989

9090
&-inner {
91-
font-size: 12px;
92-
width: 100%;
93-
height: 20px;
94-
line-height: 20px;
9591
display: inline-block;
9692
box-sizing: border-box;
97-
border: 0;
98-
padding: 0;
93+
width: 100%;
94+
height: 20px;
9995
margin: 0;
96+
padding: 0;
97+
font-size: 12px;
98+
line-height: 20px;
10099
background: transparent;
101-
cursor: pointer;
102-
outline: none;
100+
border: 0;
103101
border: none;
102+
outline: none;
103+
cursor: pointer;
104104
transition: background 0.3s, border 0.3s;
105105

106106
&:hover {
@@ -120,15 +120,15 @@
120120
&-range-hover {
121121
position: relative;
122122
&::after {
123-
content: '';
124123
position: absolute;
125124
top: 3px;
125+
right: 0;
126126
bottom: 0;
127127
left: 0;
128-
right: 0;
129128
border: 1px solid green;
130-
border-left: 0;
131129
border-right: 0;
130+
border-left: 0;
131+
content: '';
132132
pointer-events: none;
133133
}
134134
}
@@ -200,9 +200,9 @@
200200
}
201201

202202
.@{prefix-cls}-cell-week {
203-
font-size: 12px;
204203
color: #999;
205204
font-weight: bold;
205+
font-size: 12px;
206206
}
207207

208208
.@{prefix-cls}-cell:hover > .@{prefix-cls}-cell-inner,
@@ -229,15 +229,15 @@
229229

230230
&-column {
231231
flex: none;
232-
text-align: left;
233-
list-style: none;
232+
width: 50px;
234233
margin: 0;
235234
padding: 0 0 180px 0;
236-
overflow-y: hidden;
237235
overflow-x: hidden;
238-
width: 50px;
239-
transition: background 0.3s;
236+
overflow-y: hidden;
240237
font-size: 12px;
238+
text-align: left;
239+
list-style: none;
240+
transition: background 0.3s;
241241

242242
&-active {
243243
background: rgba(0, 0, 255, 0.1);
@@ -248,18 +248,24 @@
248248
}
249249

250250
> li {
251-
padding: 0;
252251
margin: 0;
252+
padding: 0;
253253
cursor: pointer;
254254

255+
&.@{prefix-cls}-time-panel-cell {
256+
&-disabled {
257+
opacity: 0.5;
258+
}
259+
}
260+
255261
.@{prefix-cls}-time-panel-cell-inner {
256-
color: #333;
257-
padding: 0 0 0 12px;
258-
margin: 0;
259-
height: 20px;
260-
line-height: 20px;
261262
display: block;
262263
width: 100%;
264+
height: 20px;
265+
margin: 0;
266+
padding: 0 0 0 12px;
267+
color: #333;
268+
line-height: 20px;
263269
text-align: left;
264270

265271
.@{prefix-cls}-panel-rtl & {
@@ -321,8 +327,8 @@
321327

322328
&-clear {
323329
position: absolute;
324-
right: 4px;
325330
top: 0;
331+
right: 4px;
326332
cursor: pointer;
327333

328334
.@{prefix-cls}-rtl & {
@@ -368,28 +374,28 @@
368374

369375
.@{prefix-cls}-range-arrow {
370376
position: absolute;
377+
left: @arrow-size;
378+
z-index: 1;
371379
width: @arrow-size;
372380
height: @arrow-size;
373-
z-index: 1;
374-
left: @arrow-size;
375381
margin-left: 10px;
376382
transition: all 0.3s;
377383

378384
.@{prefix-cls}-dropdown-rtl& {
379385
right: @arrow-size;
380386
left: auto;
381-
margin-left: 0;
382387
margin-right: 10px;
388+
margin-left: 0;
383389
}
384390

385391
&::before,
386392
&::after {
387-
content: '';
388393
position: absolute;
389-
box-sizing: border-box;
390394
top: 50%;
391395
left: 50%;
396+
box-sizing: border-box;
392397
transform: translate(-50%, -50%);
398+
content: '';
393399

394400
.@{prefix-cls}-dropdown-rtl& {
395401
right: 50%;
@@ -408,8 +414,7 @@
408414
width: @arrow-size - 2px;
409415
height: @arrow-size - 2px;
410416
border: (@arrow-size - 2px) / 2 solid blue;
411-
border-color: @background-color @background-color transparent
412-
transparent;
417+
border-color: @background-color @background-color transparent transparent;
413418
}
414419
}
415420
}
@@ -418,20 +423,20 @@
418423
// = Range Picker =
419424
// ========================================================
420425
&-range {
421-
display: inline-flex;
422426
position: relative;
427+
display: inline-flex;
423428

424429
&-wrapper {
425430
display: flex;
426431
}
427432

428433
.@{prefix-cls}-active-bar {
429-
background: green;
430434
bottom: 0;
431435
height: 3px;
432-
pointer-events: none;
433-
transition: all 0.3s;
436+
background: green;
434437
opacity: 0;
438+
transition: all 0.3s;
439+
pointer-events: none;
435440
}
436441

437442
&.@{prefix-cls}-focused {

docs/demo/time.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
## time
2+
<code src="../examples/time.tsx">

docs/examples/time.tsx

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import React from 'react';
2+
import moment from 'moment';
3+
import Picker, { RangePicker } from '../../src';
4+
import momentGenerateConfig from '../../src/generate/moment';
5+
import zhCN from '../../src/locale/zh_CN';
6+
import '../../assets/index.less';
7+
8+
const defaultValue = moment('2019-11-28 01:02:03');
9+
10+
export default () => {
11+
return (
12+
<div>
13+
<h3>DatePicker</h3>
14+
<Picker
15+
defaultValue={defaultValue}
16+
picker="date"
17+
showTime
18+
disabledTime={() => ({
19+
disabledHours: () => [1, 2, 3, 4, 5, 6],
20+
})}
21+
locale={zhCN}
22+
generateConfig={momentGenerateConfig}
23+
/>
24+
25+
<h3>TimePicker</h3>
26+
<Picker
27+
defaultValue={defaultValue}
28+
picker="time"
29+
locale={zhCN}
30+
disabledTime={(now) => ({
31+
disabledHours: () => [now.hours()],
32+
})}
33+
generateConfig={momentGenerateConfig}
34+
/>
35+
36+
<h3>RangePicker</h3>
37+
<RangePicker
38+
defaultValue={[defaultValue, defaultValue]}
39+
picker="time"
40+
locale={zhCN}
41+
generateConfig={momentGenerateConfig}
42+
disabledTime={(now, type) => ({
43+
disabledHours: () => (type === 'start' ? [now.hours()] : [now.hours() - 5]),
44+
})}
45+
/>
46+
</div>
47+
);
48+
};

src/Picker.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import usePickerInput from './hooks/usePickerInput';
3333
import useTextValueMapping from './hooks/useTextValueMapping';
3434
import useValueTexts from './hooks/useValueTexts';
3535
import useHoverValue from './hooks/useHoverValue';
36+
import { legacyPropsWarning } from './utils/warnUtil';
3637

3738
export type PickerRefConfig = {
3839
focus: () => void;
@@ -180,6 +181,11 @@ function InnerPicker<DateType>(props: PickerProps<DateType>) {
180181

181182
const needConfirmButton: boolean = (picker === 'date' && !!showTime) || picker === 'time';
182183

184+
// ============================ Warning ============================
185+
if (process.env.NODE_ENV !== 'production') {
186+
legacyPropsWarning(props);
187+
}
188+
183189
// ============================= State =============================
184190
const formatList = toArray(getDefaultFormat(format, picker, showTime, use12Hours));
185191

src/RangePicker.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import getRanges from './utils/getRanges';
3333
import useRangeViewDates from './hooks/useRangeViewDates';
3434
import type { DateRender } from './panels/DatePanel/DateBody';
3535
import useHoverValue from './hooks/useHoverValue';
36+
import { legacyPropsWarning } from './utils/warnUtil';
3637

3738
function reorderValues<DateType>(
3839
values: RangeValue<DateType>,
@@ -233,6 +234,11 @@ function InnerRangePicker<DateType>(props: RangePickerProps<DateType>) {
233234
const startInputRef = useRef<HTMLInputElement>(null);
234235
const endInputRef = useRef<HTMLInputElement>(null);
235236

237+
// ============================ Warning ============================
238+
if (process.env.NODE_ENV !== 'production') {
239+
legacyPropsWarning(props);
240+
}
241+
236242
// ============================= Misc ==============================
237243
const formatList = toArray(getDefaultFormat<DateType>(format, picker, showTime, use12Hours));
238244

0 commit comments

Comments
 (0)