refactor: replace Select hook logic with ref-function
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
"use client"
|
||||
import { RefObject, useEffect, useRef, useState } from "react"
|
||||
import { useState } from "react"
|
||||
import {
|
||||
Button,
|
||||
type Key,
|
||||
@@ -15,7 +15,7 @@ import SelectChevron from "../SelectChevron"
|
||||
|
||||
import styles from "./select.module.css"
|
||||
|
||||
import type { SelectProps } from "./select"
|
||||
import type { SelectPortalContainer, SelectProps } from "./select"
|
||||
|
||||
export default function Select({
|
||||
"aria-label": ariaLabel,
|
||||
@@ -26,18 +26,20 @@ export default function Select({
|
||||
value,
|
||||
defaultSelectedKey,
|
||||
}: SelectProps) {
|
||||
const ref = useRef<HTMLDivElement>(null)
|
||||
const [rootDiv, setRootDiv] = useState<HTMLDivElement | null>(null)
|
||||
useEffect(() => {
|
||||
setRootDiv(ref.current)
|
||||
}, [setRootDiv])
|
||||
const [rootDiv, setRootDiv] = useState<SelectPortalContainer>(null)
|
||||
|
||||
function setRef(node: SelectPortalContainer) {
|
||||
if (node) {
|
||||
setRootDiv(node)
|
||||
}
|
||||
}
|
||||
|
||||
function handleOnSelect(key: Key) {
|
||||
onSelect(key)
|
||||
}
|
||||
|
||||
return (
|
||||
<div className={styles.container} ref={ref}>
|
||||
<div className={styles.container} ref={setRef}>
|
||||
<ReactAriaSelect
|
||||
defaultSelectedKey={defaultSelectedKey}
|
||||
aria-label={ariaLabel}
|
||||
|
||||
Reference in New Issue
Block a user