Skip to content

Commit

Permalink
display suggestions on scroll
Browse files Browse the repository at this point in the history
  • Loading branch information
l3satwik committed Oct 12, 2018
1 parent d1ac24d commit 6c20549
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 7 deletions.
1 change: 1 addition & 0 deletions src/components/Search.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class Search extends React.PureComponent<SearchProps, SearchState> {
};

render() {
console.log(document.activeElement)
const {
type,
onChange,
Expand Down
30 changes: 23 additions & 7 deletions src/components/TypeAhead.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,16 @@ import OutsideClick from "./OutsideClick";
import OptionGroupRadio from "./OptionGroupRadio";

class TypeAhead extends React.PureComponent<TypeaheadProps, TypeaheadState> {
typeaheadInputRef: React.RefObject<HTMLInputElement> = React.createRef();
debouncedChange: () => void;

static defaultProps: Partial<TypeaheadProps> = {
debounceTime: 500,
onClear: () => {},
searchBox: ({ registerChange, onFocus, value }, props) => (
searchBox: (
{ registerChange, onFocus, value, typeaheadInputRef, onBlur },
props
) => (
<Input
onChange={registerChange}
placeholder={props.placeholder}
Expand All @@ -26,7 +30,10 @@ class TypeAhead extends React.PureComponent<TypeaheadProps, TypeaheadState> {
registerChange("");
props.onClear();
}
}
},
//@ts-ignore
ref: typeaheadInputRef,
onBlur
}}
value={value}
errorMessage={props.errorMessage}
Expand All @@ -45,7 +52,8 @@ class TypeAhead extends React.PureComponent<TypeaheadProps, TypeaheadState> {

state: TypeaheadState = {
value: this.props.initialValue || "",
showSuggestions: false
showSuggestions: false,
focussedElement: undefined
};

onChange = () => {
Expand All @@ -63,10 +71,15 @@ class TypeAhead extends React.PureComponent<TypeaheadProps, TypeaheadState> {

private onFocus = () => {
this.setState({
showSuggestions: true
showSuggestions: true,
focussedElement: document.activeElement
});
};

private onBlur = () => {
this.setState({ focussedElement: document.activeElement });
};

private onSelect = _value => {
this.props.onSelect(_value, this.props);

Expand All @@ -79,7 +92,7 @@ class TypeAhead extends React.PureComponent<TypeaheadProps, TypeaheadState> {
render() {
const { className, searchBox, dropdownClassName, children } = this.props;

const { showSuggestions, value } = this.state;
const { showSuggestions, value, focussedElement } = this.state;

return (
<OutsideClick
Expand All @@ -95,12 +108,15 @@ class TypeAhead extends React.PureComponent<TypeaheadProps, TypeaheadState> {
{
registerChange: this.registerChange,
onFocus: this.onFocus,
value
onBlur: this.onBlur,
value,
typeaheadInputRef: this.typeaheadInputRef
},
this.props
)}

{showSuggestions && (
{(focussedElement === this.typeaheadInputRef.current ||
showSuggestions) && (
<div className={cx(optionsWrapper, dropdownClassName)}>
<OptionGroupRadio onChange={this.onSelect}>
{children}
Expand Down
3 changes: 3 additions & 0 deletions src/components/typings/Typeahead.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ export interface TypeaheadProps {
registerChange: (text: string) => void;
onFocus: FocusEvent;
value: string;
typeaheadInputRef: React.RefObject<HTMLInputElement>;
onBlur: FocusEvent;
},
props: TypeaheadProps
) => JSX.Element;
Expand All @@ -33,4 +35,5 @@ export interface TypeaheadProps {
export interface TypeaheadState {
value: string;
showSuggestions: boolean;
focussedElement: React.ReactNode;
}

0 comments on commit 6c20549

Please sign in to comment.