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

Commit 11c62ca

Browse files
authored
Merge pull request #25 from misteinb/bugfix/expose-callback-for-datepicker-expand
expose expanded callback for datepicker
2 parents df8b09b + 34a44c6 commit 11c62ca

File tree

7 files changed

+59
-18
lines changed

7 files changed

+59
-18
lines changed

CHANGELOG.md

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

3+
## v3.0.2
4+
### Fixed
5+
- expose callback for clicking calendar icon in date picker. includes next visible state.
6+
- blur handler for form field tooltip so it will close when focus leaves the field
7+
38
## v3.0.1
49
### Fixed
510
- blur listener for datepicker was too greedy.

lib/components/DateTime/DateField.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,11 @@ export interface DateFieldProps extends React.Props<DateFieldType> {
5555
/** Classname to append to top level element of TextInput */
5656
inputClassName?: string;
5757

58+
/**
59+
* callback for clicking the calendar icon
60+
*/
61+
onExpand?: (expanded: boolean) => void;
62+
5863
attr?: DatePickerAttributes & FormFieldAttributes;
5964
}
6065

@@ -117,6 +122,7 @@ export const DateField: React.StatelessComponent<DateFieldProps> = (props: DateF
117122
disabled={props.disabled}
118123
required={props.required}
119124
onChange={props.onChange}
125+
onExpand={props.onExpand}
120126
className={props.inputClassName}
121127
attr={dateAttr}
122128
/>

lib/components/DateTime/DatePicker.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ const initialState = {value: ''};
3131
name='date-picker'
3232
onChange={(newValue) => setState({value: newValue}) }
3333
initialValue={initialValue}
34+
onExpand={(expanded) => {alert(`next expanded state: ${expanded}`)}}
3435
/>
3536
</div>
3637
```
@@ -75,7 +76,7 @@ const initialState = {value: ''};
7576
name='date-picker'
7677
onChange={(newValue) => setState({value: newValue}) }
7778
initialValue={initialValue}
78-
disabled
79+
disabled
7980
/>
8081
</div>
8182
```
@@ -90,7 +91,7 @@ const initialState = {value: ''};
9091
name='date-picker'
9192
onChange={(newValue) => setState({value: newValue}) }
9293
initialValue={initialValue}
93-
showAbove error
94+
showAbove error
9495
/>
9596
</div>
9697
```

lib/components/DateTime/DatePicker.tsx

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,11 @@ export interface DatePickerProps extends React.Props<DatePickerType> {
6666
*/
6767
onPaste?: (newValue: string) => void;
6868

69+
/**
70+
* callback for clicking the calendar icon.
71+
*/
72+
onExpand?: (expanded: boolean) => void;
73+
6974
/** Class to append to top level element */
7075
className?: string;
7176

@@ -76,7 +81,7 @@ export interface DatePickerState {
7681
value: string;
7782
initialValue?: MethodDate;
7883

79-
visible?: boolean;
84+
expanded?: boolean;
8085
}
8186

8287
/**
@@ -118,7 +123,7 @@ export class DatePicker extends React.Component<DatePickerProps, Partial<DatePic
118123
const newState = this.getInitialState(props, '');
119124
this.state = {
120125
...newState,
121-
visible: false,
126+
expanded: false,
122127
};
123128

124129
this.paste = false;
@@ -333,14 +338,18 @@ export class DatePicker extends React.Component<DatePickerProps, Partial<DatePic
333338
}
334339
}
335340

336-
onIconClick = () => {
337-
const nextVisible = !this.state.visible;
341+
onExpand = () => {
342+
const nextExpanded = !this.state.expanded;
338343

339-
this.setState({visible: nextVisible});
344+
this.setState({expanded: nextExpanded});
345+
346+
if (typeof this.props.onExpand === 'function') {
347+
this.props.onExpand(nextExpanded);
348+
}
340349
}
341350

342351
onSelect = (newValue: Date) => {
343-
this.setState({visible: false});
352+
this.setState({expanded: false});
344353
this.props.onChange(newValue.toJSON());
345354
}
346355

@@ -349,22 +358,22 @@ export class DatePicker extends React.Component<DatePickerProps, Partial<DatePic
349358
}
350359

351360
onOuterMouseEvent = (e: MouseEvent) => {
352-
if (this.state.visible && !this._containerRef.contains(e.target as HTMLElement)) {
353-
this.setState({visible: false});
361+
if (this.state.expanded && !this._containerRef.contains(e.target as HTMLElement)) {
362+
this.setState({expanded: false});
354363
}
355364
}
356365

357366
onKeydown = (e: KeyboardEvent) => {
358-
if (this.state.visible && e.keyCode === keyCode.escape) {
367+
if (this.state.expanded && e.keyCode === keyCode.escape) {
359368
e.preventDefault();
360369
e.stopPropagation();
361-
this.setState({visible: false});
370+
this.setState({expanded: false});
362371
}
363372
}
364373

365374
onBlur = (e: React.FocusEvent<any>) => {
366375
if (e.relatedTarget && !this._containerRef.contains(e.relatedTarget as HTMLElement)) {
367-
this.setState({visible: false});
376+
this.setState({expanded: false});
368377
}
369378
}
370379

@@ -418,14 +427,14 @@ export class DatePicker extends React.Component<DatePickerProps, Partial<DatePic
418427
<ActionTriggerButton
419428
icon='calendar'
420429
className={css('date-picker-calendar-icon')}
421-
onClick={this.onIconClick}
430+
onClick={this.onExpand}
422431
disabled={this.props.disabled}
423432
attr={this.props.attr.inputIcon}
424433
aria-haspopup={true}
425-
aria-expanded={this.state.visible}
434+
aria-expanded={this.state.expanded}
426435
/>
427436
</Attr.div>
428-
{this.state.visible &&
437+
{this.state.expanded &&
429438
<Attr.div
430439
className={css('date-picker-dropdown', {
431440
'above': this.props.showAbove

lib/components/DateTime/DateTimeField.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,11 @@ export interface DateTimeFieldProps extends React.Props<DateTimeFieldType> {
7171
/** Classname to append to top level element of DatePicker and TimeInput */
7272
inputClassName?: string;
7373

74+
/**
75+
* callback for clicking calendar icon
76+
*/
77+
onExpand?: (expanded: boolean) => void
78+
7479
attr?: DateTimeFieldAttributes & FormFieldAttributes;
7580
}
7681

@@ -371,6 +376,7 @@ export class DateTimeField extends React.Component<DateTimeFieldProps, Partial<D
371376
required={this.props.required}
372377
onPaste={this.onDatePaste}
373378
onChange={this.onDateChange}
379+
onExpand={this.props.onExpand}
374380
className={css('date-picker', this.props.inputClassName)}
375381
attr={dateAttr}
376382
/>

lib/components/Field/FormField.tsx

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ export class FormField extends React.PureComponent<FormFieldProps, FormFieldStat
7979
constructor(props: FormFieldProps) {
8080
super(props);
8181
this.handleKeyDown = this.handleKeyDown.bind(this);
82+
this.handleBlur = this.handleBlur.bind(this);
8283
this._self = React.createRef();
8384
this.state = {
8485
tooltipVisible: false
@@ -100,6 +101,14 @@ export class FormField extends React.PureComponent<FormFieldProps, FormFieldStat
100101
}
101102
}
102103

104+
handleBlur(e: FocusEvent) {
105+
if (!!e.relatedTarget && !this._self.current.contains(e.relatedTarget as HTMLElement)) {
106+
this.setState({
107+
tooltipVisible: false
108+
});
109+
}
110+
}
111+
103112
render() {
104113
const props = this.props;
105114
const containerClass = css('input-container', {
@@ -113,7 +122,12 @@ export class FormField extends React.PureComponent<FormFieldProps, FormFieldStat
113122
}
114123

115124
return (
116-
<Attr.div methodRef={this._self} className={containerClass} attr={props.attr.fieldContainer}>
125+
<Attr.div
126+
methodRef={this._self}
127+
className={containerClass}
128+
attr={props.attr.fieldContainer}
129+
onBlur={this.handleBlur}
130+
>
117131
{(!!props.label) && <FormLabel
118132
name={props.name}
119133
icon='info'

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.1",
3+
"version": "3.0.2",
44
"description": "Azure IoT Fluent React Controls",
55
"main": "./lib/index.js",
66
"types": "./lib/index.d.ts",

0 commit comments

Comments
 (0)