mars-v2-frontend/src/hooks/useIsOpenArray.tsx

17 lines
458 B
TypeScript
Raw Normal View History

import { useState } from 'react'
export default function useIsOpenArray(length: number, allowMultiple: boolean) {
const [isOpen, setIsOpen] = useState<boolean[]>(Array.from({ length }, (_, i) => i === 0))
function toggleOpen(index: number) {
setIsOpen((prev) => {
return prev.map((_, i) => {
if (i === index) return !prev[i]
return allowMultiple ? prev[i] : false
})
})
}
return [isOpen, toggleOpen] as const
}