refactor: replace Select hook logic with ref-function

This commit is contained in:
Arvid Norlin
2024-05-31 14:50:58 +02:00
parent 12f800913b
commit 342e8cec18
2 changed files with 12 additions and 8 deletions

View File

@@ -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}