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

Commit

Permalink
Merge pull request #25 from misteinb/bugfix/expose-callback-for-datep…
Browse files Browse the repository at this point in the history
…icker-expand

expose expanded callback for datepicker
  • Loading branch information
misteinb authored Aug 10, 2018
2 parents df8b09b + 34a44c6 commit 11c62ca
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 18 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# CHANGELOG

## v3.0.2
### Fixed
- expose callback for clicking calendar icon in date picker. includes next visible state.
- blur handler for form field tooltip so it will close when focus leaves the field

## v3.0.1
### Fixed
- blur listener for datepicker was too greedy.
Expand Down
6 changes: 6 additions & 0 deletions lib/components/DateTime/DateField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ export interface DateFieldProps extends React.Props<DateFieldType> {
/** Classname to append to top level element of TextInput */
inputClassName?: string;

/**
* callback for clicking the calendar icon
*/
onExpand?: (expanded: boolean) => void;

attr?: DatePickerAttributes & FormFieldAttributes;
}

Expand Down Expand Up @@ -117,6 +122,7 @@ export const DateField: React.StatelessComponent<DateFieldProps> = (props: DateF
disabled={props.disabled}
required={props.required}
onChange={props.onChange}
onExpand={props.onExpand}
className={props.inputClassName}
attr={dateAttr}
/>
Expand Down
5 changes: 3 additions & 2 deletions lib/components/DateTime/DatePicker.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ const initialState = {value: ''};
name='date-picker'
onChange={(newValue) => setState({value: newValue}) }
initialValue={initialValue}
onExpand={(expanded) => {alert(`next expanded state: ${expanded}`)}}
/>
</div>
```
Expand Down Expand Up @@ -75,7 +76,7 @@ const initialState = {value: ''};
name='date-picker'
onChange={(newValue) => setState({value: newValue}) }
initialValue={initialValue}
disabled
disabled
/>
</div>
```
Expand All @@ -90,7 +91,7 @@ const initialState = {value: ''};
name='date-picker'
onChange={(newValue) => setState({value: newValue}) }
initialValue={initialValue}
showAbove error
showAbove error
/>
</div>
```
37 changes: 23 additions & 14 deletions lib/components/DateTime/DatePicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ export interface DatePickerProps extends React.Props<DatePickerType> {
*/
onPaste?: (newValue: string) => void;

/**
* callback for clicking the calendar icon.
*/
onExpand?: (expanded: boolean) => void;

/** Class to append to top level element */
className?: string;

Expand All @@ -76,7 +81,7 @@ export interface DatePickerState {
value: string;
initialValue?: MethodDate;

visible?: boolean;
expanded?: boolean;
}

/**
Expand Down Expand Up @@ -118,7 +123,7 @@ export class DatePicker extends React.Component<DatePickerProps, Partial<DatePic
const newState = this.getInitialState(props, '');
this.state = {
...newState,
visible: false,
expanded: false,
};

this.paste = false;
Expand Down Expand Up @@ -333,14 +338,18 @@ export class DatePicker extends React.Component<DatePickerProps, Partial<DatePic
}
}

onIconClick = () => {
const nextVisible = !this.state.visible;
onExpand = () => {
const nextExpanded = !this.state.expanded;

this.setState({visible: nextVisible});
this.setState({expanded: nextExpanded});

if (typeof this.props.onExpand === 'function') {
this.props.onExpand(nextExpanded);
}
}

onSelect = (newValue: Date) => {
this.setState({visible: false});
this.setState({expanded: false});
this.props.onChange(newValue.toJSON());
}

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

onOuterMouseEvent = (e: MouseEvent) => {
if (this.state.visible && !this._containerRef.contains(e.target as HTMLElement)) {
this.setState({visible: false});
if (this.state.expanded && !this._containerRef.contains(e.target as HTMLElement)) {
this.setState({expanded: false});
}
}

onKeydown = (e: KeyboardEvent) => {
if (this.state.visible && e.keyCode === keyCode.escape) {
if (this.state.expanded && e.keyCode === keyCode.escape) {
e.preventDefault();
e.stopPropagation();
this.setState({visible: false});
this.setState({expanded: false});
}
}

onBlur = (e: React.FocusEvent<any>) => {
if (e.relatedTarget && !this._containerRef.contains(e.relatedTarget as HTMLElement)) {
this.setState({visible: false});
this.setState({expanded: false});
}
}

Expand Down Expand Up @@ -418,14 +427,14 @@ export class DatePicker extends React.Component<DatePickerProps, Partial<DatePic
<ActionTriggerButton
icon='calendar'
className={css('date-picker-calendar-icon')}
onClick={this.onIconClick}
onClick={this.onExpand}
disabled={this.props.disabled}
attr={this.props.attr.inputIcon}
aria-haspopup={true}
aria-expanded={this.state.visible}
aria-expanded={this.state.expanded}
/>
</Attr.div>
{this.state.visible &&
{this.state.expanded &&
<Attr.div
className={css('date-picker-dropdown', {
'above': this.props.showAbove
Expand Down
6 changes: 6 additions & 0 deletions lib/components/DateTime/DateTimeField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ export interface DateTimeFieldProps extends React.Props<DateTimeFieldType> {
/** Classname to append to top level element of DatePicker and TimeInput */
inputClassName?: string;

/**
* callback for clicking calendar icon
*/
onExpand?: (expanded: boolean) => void

attr?: DateTimeFieldAttributes & FormFieldAttributes;
}

Expand Down Expand Up @@ -371,6 +376,7 @@ export class DateTimeField extends React.Component<DateTimeFieldProps, Partial<D
required={this.props.required}
onPaste={this.onDatePaste}
onChange={this.onDateChange}
onExpand={this.props.onExpand}
className={css('date-picker', this.props.inputClassName)}
attr={dateAttr}
/>
Expand Down
16 changes: 15 additions & 1 deletion lib/components/Field/FormField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ export class FormField extends React.PureComponent<FormFieldProps, FormFieldStat
constructor(props: FormFieldProps) {
super(props);
this.handleKeyDown = this.handleKeyDown.bind(this);
this.handleBlur = this.handleBlur.bind(this);
this._self = React.createRef();
this.state = {
tooltipVisible: false
Expand All @@ -100,6 +101,14 @@ export class FormField extends React.PureComponent<FormFieldProps, FormFieldStat
}
}

handleBlur(e: FocusEvent) {
if (!!e.relatedTarget && !this._self.current.contains(e.relatedTarget as HTMLElement)) {
this.setState({
tooltipVisible: false
});
}
}

render() {
const props = this.props;
const containerClass = css('input-container', {
Expand All @@ -113,7 +122,12 @@ export class FormField extends React.PureComponent<FormFieldProps, FormFieldStat
}

return (
<Attr.div methodRef={this._self} className={containerClass} attr={props.attr.fieldContainer}>
<Attr.div
methodRef={this._self}
className={containerClass}
attr={props.attr.fieldContainer}
onBlur={this.handleBlur}
>
{(!!props.label) && <FormLabel
name={props.name}
icon='info'
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@azure-iot/ux-fluent-controls",
"version": "3.0.1",
"version": "3.0.2",
"description": "Azure IoT Fluent React Controls",
"main": "./lib/index.js",
"types": "./lib/index.d.ts",
Expand Down

0 comments on commit 11c62ca

Please sign in to comment.