Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 21 additions & 3 deletions src/components/Table/base/Accordion.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useState } from 'react'
import React, { useEffect, useState } from 'react'
import { composeClasses } from 'lib/classes'
import ChevronUpIcon from '@heroicons/react/outline/ChevronUpIcon'

Expand All @@ -13,15 +13,33 @@ export interface AccordionProps {
* 0 is the position of the first column
*/
iconPosition?: number
/**
* Indicates if the accordion is expanded default false
*/
isExpanded?: boolean
/**
* The callback function that will be called whenever the accordion is expanded or collapsed
*/
onClick?: () => void
}

const Accordion = ({ children, iconPosition = 0 }: AccordionProps) => {
const [toggle, setToggle] = useState(false)
const Accordion = ({
children,
iconPosition = 0,
isExpanded = false,
onClick: onClickFn
}: AccordionProps) => {
const [toggle, setToggle] = useState(isExpanded)

const onClick = () => {
onClickFn?.()
setToggle(!toggle)
}

useEffect(() => {
setToggle(isExpanded)
}, [isExpanded])

const [firstChild, ...childs] = children as React.ReactElement[]

const modifiedCells = React.Children.map(
Expand Down