Skip to content

Commit 3d8797b

Browse files
committed
WHAT: Add quick version for new disabled props
WHY: * XXXXX HOW: * NOTHING TO SAY
1 parent fae32c1 commit 3d8797b

File tree

6 files changed

+69
-5
lines changed

6 files changed

+69
-5
lines changed

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,16 @@ timeMode=12
309309
/>
310310
```
311311

312+
- `disabled`
313+
314+
> Disable component. Default false
315+
316+
```
317+
<TimePicker
318+
disabled={true}
319+
/>
320+
```
321+
312322
- `draggable`
313323

314324
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`

css/base.css

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,16 @@
66
height: 50px;
77
}
88

9-
.time_picker_preview:active {
9+
.time_picker_preview:not(.disabled):active, .time_picker_preview:not(.disabled).active {
1010
box-shadow: 0 8px 8px 0 rgba(0, 0, 0, 0.12), 0 0 8px 0 rgba(0, 0, 0, 0.08);
1111
-moz-box-shadow: 0 8px 8px 0 rgba(0, 0, 0, 0.12), 0 0 8px 0 rgba(0, 0, 0, 0.08);
1212
-webkit-box-shadow: 0 8px 8px 0 rgba(0, 0, 0, 0.12), 0 0 8px 0 rgba(0, 0, 0, 0.08);
1313
}
1414

15+
.time_picker_preview.disabled {
16+
cursor: not-allowed;
17+
}
18+
1519
.preview_container {
1620
position: absolute;
1721
left: 50%;

doc/README_CN.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,26 @@ timeMode=12
271271
/>
272272
```
273273

274+
- `closeOnOutsideClick`
275+
276+
> 点击 Modal 外部后是否关闭。默认为 true
277+
278+
```
279+
<TimePicker
280+
closeOnOutsideClick={false}
281+
/>
282+
```
283+
284+
- `disabled`
285+
286+
> 禁用组件。默认为 false
287+
288+
```
289+
<TimePicker
290+
disabled={true}
291+
/>
292+
```
293+
274294
- `draggable`
275295

276296
如果想禁用拖拽,则可以设置 `draggable``false`,那样的话用户只能通过点击来改变时间。默认为 `true`

src/components/OutsideClickHandler.jsx

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,28 +15,45 @@ const defaultProps = {
1515
class OutsideClickHandler extends React.PureComponent {
1616
constructor(props) {
1717
super(props);
18+
this.hasAction = false;
1819
this.onOutsideClick = this.onOutsideClick.bind(this);
1920
}
2021

2122
componentDidMount() {
23+
this.bindActions();
24+
}
25+
26+
componentDidUpdate() {
27+
this.bindActions();
28+
}
29+
30+
componentWillUnmount() {
31+
this.unbindActions();
32+
}
33+
34+
bindActions() {
2235
const { closeOnOutsideClick } = this.props;
2336
if (closeOnOutsideClick) {
37+
if (this.hasAction) return;
2438
if (document.addEventListener) {
2539
document.addEventListener('mousedown', this.onOutsideClick, true);
2640
} else {
2741
document.attachEvent('onmousedown', this.onOutsideClick);
2842
}
43+
this.hasAction = true;
2944
}
3045
}
3146

32-
componentWillUnmount() {
47+
unbindActions() {
48+
if (!this.hasAction) return;
3349
const { closeOnOutsideClick } = this.props;
3450
if (closeOnOutsideClick) {
3551
if (document.removeEventListener) {
3652
document.removeEventListener('mousedown', this.onOutsideClick, true);
3753
} else {
3854
document.detachEvent('onmousedown', this.onOutsideClick);
3955
}
56+
this.hasAction = false;
4057
}
4158
}
4259

src/components/TimePicker.jsx

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ const propTypes = {
6363
useTz: PropTypes.bool,
6464
closeOnOutsideClick: PropTypes.bool,
6565
timeConfig: PropTypes.object,
66+
disabled: PropTypes.bool,
6667
};
6768

6869
const defaultProps = {
@@ -92,7 +93,8 @@ const defaultProps = {
9293
timeConfig: {
9394
step: 30,
9495
unit: 'minutes'
95-
}
96+
},
97+
disabled: false,
9698
};
9799

98100
class TimePicker extends React.PureComponent {
@@ -146,6 +148,9 @@ class TimePicker extends React.PureComponent {
146148
}
147149

148150
onFocusChange(focused) {
151+
const { disabled } = this.props;
152+
if (disabled) return;
153+
149154
this.setState({ focused });
150155
const { onFocusChange } = this.props;
151156
onFocusChange && onFocusChange(focused);
@@ -286,6 +291,7 @@ class TimePicker extends React.PureComponent {
286291
renderDialPlate() {
287292
const {
288293
theme,
294+
disabled,
289295
timeMode,
290296
autoMode,
291297
autoClose,
@@ -300,6 +306,8 @@ class TimePicker extends React.PureComponent {
300306
timezoneIsEditable,
301307
} = this.props;
302308

309+
if (disabled) return null;
310+
303311
const dialTheme = theme === 'material' ? theme : 'classic';
304312
const DialPlate = DialPlates[dialTheme];
305313

@@ -337,6 +345,7 @@ class TimePicker extends React.PureComponent {
337345
render() {
338346
const {
339347
trigger,
348+
disabled,
340349
placeholder,
341350
withoutIcon,
342351
colorPalette,
@@ -348,7 +357,8 @@ class TimePicker extends React.PureComponent {
348357

349358
const pickerPreviewClass = cx(
350359
'time_picker_preview',
351-
focused && 'active'
360+
focused && 'active',
361+
disabled && 'disabled'
352362
);
353363
const containerClass = cx(
354364
'time_picker_container',
@@ -375,7 +385,7 @@ class TimePicker extends React.PureComponent {
375385
<OutsideClickHandler
376386
focused={focused}
377387
onOutsideClick={this.onBlur}
378-
closeOnOutsideClick={closeOnOutsideClick}
388+
closeOnOutsideClick={disabled ? false : closeOnOutsideClick}
379389
>
380390
{this.renderDialPlate()}
381391
</OutsideClickHandler>

stories/TimePicker.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ storiesOf('Default TimePicker', module)
99
.addWithInfo('basic', () => (
1010
<TimePickerWrapper />
1111
))
12+
.addWithInfo('disabled', () => (
13+
<TimePickerWrapper disabled />
14+
))
1215
.addWithInfo('with default time', () => {
1316
const aDefaultTime = text('set default time', '22:10');
1417
return (

0 commit comments

Comments
 (0)