Skip to content
This repository was archived by the owner on Oct 12, 2023. It is now read-only.

Commit df8b09b

Browse files
authored
Merge pull request #24 from misteinb/bugfix/datepicker-event-listeners
fix blur listener to be less greedy
2 parents 7188905 + 06a128c commit df8b09b

File tree

7 files changed

+55
-35
lines changed

7 files changed

+55
-35
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# CHANGELOG
22

3+
## v3.0.1
4+
### Fixed
5+
- blur listener for datepicker was too greedy.
6+
- shrink calendar
7+
- move date picker window listers to component mounting events
8+
39
## v3.0.0
410
### Changed
511
- calendar api changed. no longer extends drop down

lib/components/DateTime/Calendar.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ ______________________________________________________________________________
4141
### Examples
4242

4343
```jsx
44-
<div style={{width: '320px', border: '1px solid #333'}}>
44+
<div style={{width: '260px', border: '1px solid #333'}}>
4545
<Calendar onChange={date => alert(date)} value={new Date(2017, 9, 12)} />
4646
</div>
4747
```

lib/components/DateTime/Calendar.scss

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22
@import '../../common/mixins';
33
@import '../../common/color.controls';
44

5-
$calendar-column-width: 10*$grid-size;
6-
$calendar-width: 52*$grid-size;
5+
$calendar-column-width: 8*$grid-size;
6+
$calendar-chevron-width: 10*$grid-size;
7+
$calendar-width: 40*$grid-size;
78

89
$line-height-chevron: 4*$grid-size;
910
$line-height-header: 4*$grid-size;
1011
$line-height-row: $calendar-column-width;
1112

12-
1313
.calendar {
1414
@include md-box(block, relative);
1515
font-family: $font-family-default;
@@ -20,8 +20,11 @@ $line-height-row: $calendar-column-width;
2020
}
2121
.calendar-header {
2222
@include md-box(block, relative);
23-
margin-left: 4*$grid-size;
23+
margin-left: 2*$grid-size;
2424
font-family: $font-family-light;
25+
display: flex;
26+
align-items: center;
27+
justify-content: space-between;
2528

2629
& button {
2730
text-decoration: none;
@@ -37,6 +40,12 @@ $line-height-row: $calendar-column-width;
3740
}
3841
}
3942

43+
.action-bar {
44+
display: flex;
45+
align-items: center;
46+
justify-content: flex-end;
47+
}
48+
4049
.calendar-month {
4150
@include md-box(inline-block, relative);
4251
margin-top: 4*$grid-size;
@@ -46,18 +55,17 @@ $line-height-row: $calendar-column-width;
4655
font-size: $font-size-h5;
4756
line-height: $line-height-header;
4857
text-align: left;
49-
width: $calendar-width;
5058
vertical-align: top;
59+
width: 100%
5160
}
5261

5362
.calendar-chevron {
5463
@include md-box(inline-block, relative);
5564
text-align: center;
5665
vertical-align: bottom;
5766
line-height: $line-height-chevron;
58-
margin-left: $grid-size;
5967
padding-top: 2*$grid-size;
60-
width: $calendar-column-width;
68+
width: $calendar-chevron-width;
6169
text-decoration: none;
6270

6371
&:visited {
@@ -78,7 +86,7 @@ $line-height-row: $calendar-column-width;
7886
@include md-box(block, relative);
7987
width: 100%;
8088
margin: 2*$grid-size 0px;
81-
padding-left: 2*$grid-size;
89+
padding-left: $grid-size;
8290
padding-bottom: 1.5*$grid-size;
8391
@include themify{
8492
border-bottom: 1px solid themed('color-border-rest');
@@ -96,7 +104,7 @@ $line-height-row: $calendar-column-width;
96104
}
97105

98106
.calendar-row {
99-
margin-left: 2*$grid-size;
107+
margin-left: $grid-size;
100108

101109
& > button.selected {
102110
@include themify{

lib/components/DateTime/Calendar.tsx

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as React from 'react';
22
import * as classNames from 'classnames/bind';
3-
import { DivProps, SpanProps, ButtonProps, Elements as Attr } from '../../Attributes';
3+
import { DivProps, ButtonProps, Elements as Attr } from '../../Attributes';
44
import { ActionTriggerButton, ActionTriggerButtonAttributes } from '../ActionTrigger/ActionTriggerButton';
55
import { getLocalMonths, getLocalWeekdays } from './helpers';
66
import { keyCode, MethodDate, weekLength } from '../../Common';
@@ -442,18 +442,20 @@ export class Calendar extends React.Component<CalendarProps, Partial<CalendarSta
442442
>
443443
{`${this.monthNames[curMonth]} ${curYear}`}
444444
</Attr.div>
445-
<ActionTriggerButton
446-
className={css('calendar-chevron')}
447-
onClick={this.onPrevMonth}
448-
icon='chevronUp'
449-
attr={this.props.attr.prevMonthButton}
450-
/>
451-
<ActionTriggerButton
452-
icon='chevronDown'
453-
className={css('calendar-chevron')}
454-
onClick={this.onNextMonth}
455-
attr={this.props.attr.nextMonthButton}
456-
/>
445+
<div className={css('action-bar')}>
446+
<ActionTriggerButton
447+
className={css('calendar-chevron')}
448+
onClick={this.onPrevMonth}
449+
icon='chevronUp'
450+
attr={this.props.attr.prevMonthButton}
451+
/>
452+
<ActionTriggerButton
453+
icon='chevronDown'
454+
className={css('calendar-chevron')}
455+
onClick={this.onNextMonth}
456+
attr={this.props.attr.nextMonthButton}
457+
/>
458+
</div>
457459
</Attr.div>
458460
<Attr.div
459461
className={css('calendar-days')}

lib/components/DateTime/DatePicker.scss

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
@import '../../common/mixins';
33
@import '../../common/color.controls';
44

5-
$date-picker-dropdown-width: 80*$grid-size;
5+
$date-picker-dropdown-width: 65*$grid-size;
66

77
$line-height-text: 5*$grid-size;
88
$line-height-icon: 4*$grid-size;

lib/components/DateTime/DatePicker.tsx

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,16 @@ export class DatePicker extends React.Component<DatePickerProps, Partial<DatePic
207207
}
208208
}
209209

210+
componentDidMount() {
211+
window.addEventListener('click', this.onOuterMouseEvent);
212+
window.addEventListener('keydown', this.onKeydown);
213+
}
214+
215+
componentWillUnmount() {
216+
window.removeEventListener('click', this.onOuterMouseEvent);
217+
window.removeEventListener('keydown', this.onKeydown);
218+
}
219+
210220
parse(newValue: string) {
211221
let valid = true;
212222

@@ -326,11 +336,6 @@ export class DatePicker extends React.Component<DatePickerProps, Partial<DatePic
326336
onIconClick = () => {
327337
const nextVisible = !this.state.visible;
328338

329-
if (nextVisible) {
330-
window.addEventListener('click', this.onOuterMouseEvent);
331-
window.addEventListener('keydown', this.onKeydown);
332-
}
333-
334339
this.setState({visible: nextVisible});
335340
}
336341

@@ -344,26 +349,25 @@ export class DatePicker extends React.Component<DatePickerProps, Partial<DatePic
344349
}
345350

346351
onOuterMouseEvent = (e: MouseEvent) => {
347-
if (!this._containerRef.contains(e.target as HTMLElement)) {
348-
window.removeEventListener('click', this.onOuterMouseEvent);
352+
if (this.state.visible && !this._containerRef.contains(e.target as HTMLElement)) {
349353
this.setState({visible: false});
350354
}
351355
}
352356

353357
onKeydown = (e: KeyboardEvent) => {
354-
if (e.keyCode === keyCode.escape) {
358+
if (this.state.visible && e.keyCode === keyCode.escape) {
355359
e.preventDefault();
356360
e.stopPropagation();
357-
window.removeEventListener('keydown', this.onKeydown);
358361
this.setState({visible: false});
359362
}
360363
}
361364

362365
onBlur = (e: React.FocusEvent<any>) => {
363-
if (!this._containerRef.contains(e.relatedTarget as HTMLElement)) {
366+
if (e.relatedTarget && !this._containerRef.contains(e.relatedTarget as HTMLElement)) {
364367
this.setState({visible: false});
365368
}
366369
}
370+
367371
setContainerRef = (element: HTMLElement) => {
368372
this._containerRef = element;
369373
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@azure-iot/ux-fluent-controls",
3-
"version": "3.0.0",
3+
"version": "3.0.1",
44
"description": "Azure IoT Fluent React Controls",
55
"main": "./lib/index.js",
66
"types": "./lib/index.d.ts",

0 commit comments

Comments
 (0)