Skip to content

Commit 0849130

Browse files
committed
use ref for dropdown
1 parent d627e1b commit 0849130

1 file changed

Lines changed: 10 additions & 11 deletions

File tree

src/index.tsx

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -121,13 +121,11 @@ const AddressSearch = (props: Props): JSX.Element => {
121121
const [suggestions, setSuggestions] = useState<any[]>([]);
122122
const [value, setValue] = useState('');
123123

124-
const anchorRef = useRef<HTMLInputElement>(null);
125-
const rect = document.body
126-
.getElementsByClassName('react-loqate-list-anchor')[0]
127-
?.getBoundingClientRect();
124+
const anchorRef = useRef<HTMLDivElement>(null);
125+
const rect = anchorRef.current?.getBoundingClientRect();
128126

129127
const classes = useStyles({
130-
top: rect?.y + rect?.height + window.scrollY || 0,
128+
top: rect ? rect.y + rect.height + window.scrollY : 0,
131129
left: rect?.left || 0,
132130
width: rect?.width || undefined,
133131
});
@@ -141,7 +139,6 @@ const AddressSearch = (props: Props): JSX.Element => {
141139
if (res.Items.length) setSuggestions([]);
142140
onSelect(res.Items[0]);
143141
} else {
144-
const value = anchorRef.current?.value || '';
145142
const sugs = await find(value, sug.Id);
146143
setSuggestions(sugs);
147144
}
@@ -194,11 +191,13 @@ const AddressSearch = (props: Props): JSX.Element => {
194191

195192
return (
196193
<>
197-
<Input
198-
className={clsx(inputClassname, 'react-loqate-list-anchor')}
199-
onChange={handleChange}
200-
value={value}
201-
/>
194+
<div ref={anchorRef}>
195+
<Input
196+
className={clsx(inputClassname)}
197+
onChange={handleChange}
198+
value={value}
199+
/>
200+
</div>
202201
<Portal container={document.body}>
203202
<ClickAwayListener onClickAway={() => setSuggestions([])}>
204203
<List

0 commit comments

Comments
 (0)