Skip to content

Commit 3fbc057

Browse files
wizapparfedulov
authored andcommitted
InputView: forwards onBlur event (#171)
* InputView: forwards onBlur event * invoke onBlur from closePopup method * do not declare on* callbacks inside render method
1 parent 6e3d80d commit 3fbc057

File tree

2 files changed

+52
-32
lines changed

2 files changed

+52
-32
lines changed

src/inputs/BaseInput.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
SemanticICONS,
77
} from 'semantic-ui-react';
88
import * as PropTypes from 'prop-types';
9+
import invoke from 'lodash/invoke';
910

1011
import {
1112
TimeFormat,
@@ -287,6 +288,8 @@ abstract class BaseInput<P extends BaseInputProps,
287288
private inputNode: HTMLElement;
288289

289290
protected closePopup = (): void => {
291+
invoke(this.props, 'onClose');
292+
invoke(this.props, 'onBlur');
290293
this.setState({ popupIsClosed: true });
291294
}
292295

src/views/InputView.tsx

Lines changed: 49 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ interface InputViewProps {
7171
onMount: (e: HTMLElement) => void;
7272
/** Called after input field value has changed. */
7373
onChange: (e: React.SyntheticEvent<HTMLElement>, data: any) => void;
74+
/** Called when component looses focus. */
75+
onBlur?: (e: React.SyntheticEvent<HTMLElement>) => void;
7476
closePopup: () => void;
7577
openPopup: () => void;
7678
/** Called on input focus. */
@@ -141,6 +143,48 @@ class InputView extends React.Component<InputViewProps, any> {
141143
private popupNode: HTMLElement | undefined;
142144
private mouseLeaveTimeout: number | null;
143145

146+
public onBlur = (e, ...args) => {
147+
const {
148+
closePopup,
149+
} = this.props;
150+
151+
if (
152+
e.relatedTarget !== this.popupNode
153+
&& e.relatedTarget !== this.inputNode
154+
&& !checkIE()
155+
) {
156+
invoke(this.props, 'onBlur', e, ...args);
157+
closePopup();
158+
}
159+
}
160+
161+
public onMouseLeave = (e, ...args) => {
162+
const { closeOnMouseLeave, closePopup } = this.props;
163+
164+
if (e.relatedTarget !== this.popupNode && e.relatedTarget !== this.inputNode) {
165+
if (closeOnMouseLeave) {
166+
invoke(this.props, 'onMouseLeave', e, ...args);
167+
this.mouseLeaveTimeout = window.setTimeout(() => {
168+
if (this.mouseLeaveTimeout) {
169+
closePopup();
170+
}
171+
}, 500);
172+
}
173+
}
174+
}
175+
176+
public onMouseEnter = (e, ...args) => {
177+
const { closeOnMouseLeave } = this.props;
178+
179+
invoke(this.props, 'onMouseEnter', e, ...args);
180+
if (e.currentTarget === this.popupNode || e.currentTarget === this.inputNode) {
181+
if (closeOnMouseLeave) {
182+
clearTimeout(this.mouseLeaveTimeout);
183+
this.mouseLeaveTimeout = null;
184+
}
185+
}
186+
}
187+
144188
public render() {
145189
const {
146190
renderPicker,
@@ -169,33 +213,6 @@ class InputView extends React.Component<InputViewProps, any> {
169213
...rest
170214
} = this.props;
171215

172-
const onBlur = (e) => {
173-
if (e.relatedTarget !== this.popupNode && e.relatedTarget !== this.inputNode && !checkIE()) {
174-
closePopup();
175-
}
176-
};
177-
178-
const onMouseLeave = (e) => {
179-
if (e.relatedTarget !== this.popupNode && e.relatedTarget !== this.inputNode) {
180-
if (closeOnMouseLeave) {
181-
this.mouseLeaveTimeout = window.setTimeout(() => {
182-
if (this.mouseLeaveTimeout) {
183-
closePopup();
184-
}
185-
}, 500);
186-
}
187-
}
188-
};
189-
190-
const onMouseEnter = (e) => {
191-
if (e.currentTarget === this.popupNode || e.currentTarget === this.inputNode) {
192-
if (closeOnMouseLeave) {
193-
clearTimeout(this.mouseLeaveTimeout);
194-
this.mouseLeaveTimeout = null;
195-
}
196-
}
197-
};
198-
199216
const inputElement = (
200217
<FormInputWithRef
201218
{...rest}
@@ -212,8 +229,8 @@ class InputView extends React.Component<InputViewProps, any> {
212229
invoke(this.props, 'onFocus', e, this.props);
213230
openPopup();
214231
}}
215-
onBlur={onBlur}
216-
onMouseEnter={onMouseEnter}
232+
onBlur={this.onBlur}
233+
onMouseEnter={this.onMouseEnter}
217234
onChange={onChange} />
218235
);
219236

@@ -255,9 +272,9 @@ class InputView extends React.Component<InputViewProps, any> {
255272
mountNode={mountNode}
256273
>
257274
<div
258-
onBlur={onBlur}
259-
onMouseLeave={onMouseLeave}
260-
onMouseEnter={onMouseEnter}
275+
onBlur={this.onBlur}
276+
onMouseLeave={this.onMouseLeave}
277+
onMouseEnter={this.onMouseEnter}
261278
style={{ outline: 'none' }}
262279
tabIndex={0}
263280
ref={(ref) => this.popupNode = ref}

0 commit comments

Comments
 (0)