fix: make dropdown height adjust to screen

This commit is contained in:
Antonella Sgarlatta
2021-05-24 18:28:13 -03:00
parent c230cdee81
commit e2585200ac

View File

@@ -18,6 +18,7 @@ export const AutocompleteTagInput: FunctionalComponent<Props> = ({
}) => { }) => {
const [searchQuery, setSearchQuery] = useState(''); const [searchQuery, setSearchQuery] = useState('');
const [dropdownVisible, setDropdownVisible] = useState(false); const [dropdownVisible, setDropdownVisible] = useState(false);
const [dropdownMaxHeight, setDropdownMaxHeight] = useState<number | 'auto'>('auto');
const getActiveNoteTagResults = (query: string) => { const getActiveNoteTagResults = (query: string) => {
const { activeNote } = appState.notes; const { activeNote } = appState.notes;
@@ -28,11 +29,19 @@ export const AutocompleteTagInput: FunctionalComponent<Props> = ({
return getActiveNoteTagResults(''); return getActiveNoteTagResults('');
}); });
const inputRef = useRef<HTMLInputElement>();
const dropdownRef = useRef<HTMLDivElement>(); const dropdownRef = useRef<HTMLDivElement>();
const [closeOnBlur] = useCloseOnBlur(dropdownRef, (visible: boolean) => const [closeOnBlur] = useCloseOnBlur(dropdownRef, (visible: boolean) =>
setDropdownVisible(visible) setDropdownVisible(visible)
); );
const showDropdown = () => {
const { clientHeight } = document.documentElement;
const inputRect = inputRef.current.getBoundingClientRect();
setDropdownMaxHeight(clientHeight - inputRect.bottom - 32*2);
setDropdownVisible(true);
};
const onSearchQueryChange = (event: Event) => { const onSearchQueryChange = (event: Event) => {
const query = (event.target as HTMLInputElement).value; const query = (event.target as HTMLInputElement).value;
const tags = getActiveNoteTagResults(query); const tags = getActiveNoteTagResults(query);
@@ -46,9 +55,10 @@ export const AutocompleteTagInput: FunctionalComponent<Props> = ({
<form onSubmit={(event) => event.preventDefault()} className="mt-2"> <form onSubmit={(event) => event.preventDefault()} className="mt-2">
<Disclosure <Disclosure
open={dropdownVisible} open={dropdownVisible}
onChange={() => setDropdownVisible(true)} onChange={showDropdown}
> >
<input <input
ref={inputRef}
className="min-w-80 text-xs no-border h-7 focus:outline-none focus:shadow-none focus:border-bottom" className="min-w-80 text-xs no-border h-7 focus:outline-none focus:shadow-none focus:border-bottom"
value={searchQuery} value={searchQuery}
onChange={onSearchQueryChange} onChange={onSearchQueryChange}
@@ -56,14 +66,15 @@ export const AutocompleteTagInput: FunctionalComponent<Props> = ({
onBlur={closeOnBlur} onBlur={closeOnBlur}
onFocus={() => { onFocus={() => {
if (tagResults.length > 0) { if (tagResults.length > 0) {
setDropdownVisible(true); showDropdown();
} }
}} }}
/> />
{dropdownVisible && ( {dropdownVisible && (
<DisclosurePanel <DisclosurePanel
ref={dropdownRef} ref={dropdownRef}
className="sn-dropdown flex flex-col py-2 max-h-120 overflow-y-scroll absolute" className="sn-dropdown flex flex-col py-2 overflow-y-scroll absolute"
style={{ maxHeight: dropdownMaxHeight }}
> >
{tagResults.map((tag) => { {tagResults.map((tag) => {
return ( return (