Skip to content

Commit

Permalink
remove mounted flag
Browse files Browse the repository at this point in the history
  • Loading branch information
joduplessis committed Jun 13, 2024
1 parent 0308e7d commit 3809f08
Showing 1 changed file with 28 additions and 27 deletions.
55 changes: 28 additions & 27 deletions packages/core/src/select/select.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,6 @@ export const Select = (props: SelectProps) => {
const listRef = useRef(null)
const popupContentId = useId()
const containerRef = useRef(null)
const mountedRef = useRef(false)
const [offscreen, setOffscreen] = useState(false)
const [cursor, setCursor] = useState(-1)
const [text, setText] = useState('')
Expand Down Expand Up @@ -160,14 +159,26 @@ export const Select = (props: SelectProps) => {
'is-offscreen': offscreen,
})

const openCallback = () => {
if (onOpen) onOpen()
}

const closeCallback = () => {
if (onClose) onClose()
}

const handleChange = (e) => {
setText(e.target.value)
if (!visible) show()
if (!visible) {
show()
openCallback()
}
}

const handleClick = (e) => {
if (!visible) {
show()
openCallback()
// make sure to focus the right element
if (tagInput) {
focusElement(tagInputFieldRef.current)
Expand All @@ -179,7 +190,10 @@ export const Select = (props: SelectProps) => {

const handleFocus = (e) => {
if (dontShowListPopup.current) return
if (!visible && openOnFocus) show()
if (!visible && openOnFocus) {
show()
openCallback()
}
}

const clear = () => {
Expand All @@ -189,6 +203,7 @@ export const Select = (props: SelectProps) => {
const dismiss = (refocus = true) => {
if (!isStatic) {
hide()
closeCallback()
// we simply want to focus the element again
// and not show the list
if (refocus) {
Expand Down Expand Up @@ -239,6 +254,7 @@ export const Select = (props: SelectProps) => {
e.preventDefault()
e.stopPropagation()
show()
openCallback()
}
}

Expand Down Expand Up @@ -284,23 +300,10 @@ export const Select = (props: SelectProps) => {

// manages the onFilter
useEffect(() => {
if (mountedRef.current) {
setTimer(() => {
if (onFilter && !!text) onFilter(text)
}, filterDelay)
}
}, [text])

// callbacks for onOpen & onClose (after mount)
useEffect(() => {
if (mountedRef.current) {
if (visible) {
if (onOpen) onOpen()
} else {
if (onClose) onClose()
}
}
}, [visible])
setTimer(() => {
if (onFilter && !!text) onFilter(text)
}, filterDelay)
}, [text, onFilter, filterDelay])

// resets the cursor
useEffect(() => {
Expand Down Expand Up @@ -328,13 +331,11 @@ export const Select = (props: SelectProps) => {

// opens the list when mounted
useEffect(() => {
if (!visible && openOnMount) show()
}, [openOnMount])

// set the mounted flag
useEffect(() => {
mountedRef.current = true
}, [])
if (!visible && openOnMount) {
show()
openCallback()
}
}, [openOnMount, visible])

return (
<View
Expand Down

0 comments on commit 3809f08

Please sign in to comment.