Skip to content

Commit b3a3b8d

Browse files
authored
fix: attach onBlur handler correctly (#182)
closes #178
1 parent 569d6e3 commit b3a3b8d

File tree

2 files changed

+29
-8
lines changed

2 files changed

+29
-8
lines changed

src/Textarea.jsx

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -875,12 +875,7 @@ class ReactTextareaAutocomplete extends React.Component<
875875
}
876876
};
877877

878-
_onClickAndBlurHandler = (e: SyntheticFocusEvent<*>) => {
879-
const { onBlur } = this.props;
880-
881-
// If this is a click: e.target is the textarea, and e.relatedTarget is the thing
882-
// that was actually clicked. If we clicked inside the autoselect dropdown, then
883-
// that's not a blur, from the autoselect's point of view, so then do nothing.
878+
_shouldStayOpen = (e: SyntheticFocusEvent<*>) => {
884879
let el = e.relatedTarget;
885880
// IE11 doesn't know about `relatedTarget` // https://stackoverflow.com/a/49325196/2719917
886881
if (el === null) {
@@ -892,15 +887,39 @@ class ReactTextareaAutocomplete extends React.Component<
892887
el instanceof Node &&
893888
this.dropdownRef.contains(el)
894889
) {
890+
return true;
891+
}
892+
893+
return false;
894+
};
895+
896+
_onClick = (e: SyntheticFocusEvent<*>) => {
897+
const { onClick } = this.props;
898+
899+
if (onClick) {
900+
e.persist();
901+
onClick(e);
902+
}
903+
904+
if (this._shouldStayOpen(e)) {
895905
return;
896906
}
897907

898908
this._closeAutocomplete();
909+
};
910+
911+
_onBlur = (e: SyntheticFocusEvent<*>) => {
912+
const { onBlur } = this.props;
899913

900914
if (onBlur) {
901915
e.persist();
902916
onBlur(e);
903917
}
918+
919+
if (this._shouldStayOpen(e)) {
920+
return;
921+
}
922+
this._closeAutocomplete();
904923
};
905924

906925
_onScrollHandler = () => {
@@ -1012,9 +1031,9 @@ class ReactTextareaAutocomplete extends React.Component<
10121031
onScroll={this._onScrollHandler}
10131032
onClick={
10141033
// The textarea itself is outside the autoselect dropdown.
1015-
this._onClickAndBlurHandler
1034+
this._onClick
10161035
}
1017-
onBlur={this._onClickAndBlurHandler}
1036+
onBlur={this._onBlur}
10181037
value={value}
10191038
style={style}
10201039
{...extraAttrs}
@@ -1135,6 +1154,7 @@ ReactTextareaAutocomplete.propTypes = {
11351154
onChange: PropTypes.func,
11361155
onSelect: PropTypes.func,
11371156
onBlur: PropTypes.func,
1157+
onClick: PropTypes.func,
11381158
textAreaComponent: PropTypes.oneOfType([PropTypes.string, PropTypes.object]),
11391159
movePopupAsYouType: PropTypes.bool,
11401160
onCaretPositionChange: PropTypes.func,

src/types.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ export type TextareaProps = {
7272
onChange: ?(SyntheticEvent<*> | Event) => void,
7373
onSelect: ?(SyntheticEvent<*> | Event) => void,
7474
onBlur: ?(SyntheticEvent<*> | Event) => void,
75+
onClick: ?(SyntheticEvent<*> | Event) => void,
7576
onCaretPositionChange: ?(number) => void,
7677
innerRef: ?(HTMLTextAreaElement) => void,
7778
scrollToItem:

0 commit comments

Comments
 (0)