Accordions are collapsible sections that are toggled by interacting with a permanently visible header.
You should have the chayns-components
package installed. If that is not the
case already, run
yarn add chayns-components
or
npm i chayns-components
After the chayns-components
package is installed, you can import the component
and use it with React:
import React from 'react'
import { Accordion } from 'chayns-components';
// ...
<Accordion {...} />
The Accordion
-component takes the following props:
Name | Type | Default | Required |
---|---|---|---|
head | ReactNode | { open: ReactNode, close: ReactNode } |
✓ | |
headMultiline | boolean |
false |
|
headClassNames | string | Array<string> | object |
||
headCustomAttributes | object |
||
children | ReactNode |
✓ | |
right | ReactNode | { open: ReactNode, close: ReactNode } |
||
renderClosed | boolean |
false |
|
isWrapped | boolean |
false |
|
dataGroup | string |
||
className | string |
'' |
|
id | string |
||
style | { [key: string]: string | number } |
||
styleBody | { [key: string]: string | number } |
||
onOpen | function |
||
onClose | function |
||
defaultOpened | boolean |
||
reference | function |
||
autogrow | boolean |
true |
|
open | boolean |
||
icon | object | string | ReactNode |
'ts-angle-right' |
|
noRotate | boolean |
false |
|
fixed | boolean |
false |
|
noIcon | boolean |
false |
|
onSearch | function |
||
onSearchEnter | function |
||
searchPlaceholder | string |
'' |
|
searchValue | string |
||
removeContentClosed | boolean |
false |
|
onClick | function |
||
disabled | boolean |
false |
|
controlled | boolean |
false |
head: ReactNode | { open: ReactNode, close: ReactNode };
The component that should be displayed in the accordion head when it is closed.
Can be a string
, React node or object like this:
{ open: OpenComponent, close: CloseComponent }
. If an object is provided, the
components will be swapped based on the opening state.
headMultiline?: boolean
Allows text to wrap inside of the head.
headClassNames?: string | Array<string> | object
Additional classnames to be applied to the head. Can be specified as a string
,
string[]
or {[key: string]: boolean}
, which will be passed to the
classnames
function.
headCustomAttributes?: object
Custom HTML attributes that will be added to the <div>
that contains the head
component.
children: ReactNode;
The content the Accordion reveals when it is open. To get proper spacing inside
of the Accordion body, supply a <div>
with the classname accordion__content
applied to it.
right?: ReactNode | { open: ReactNode, close: ReactNode }
Component that will be shown on the right side of the component. Typically a
badge. If you want different components for the open and closed state, supply an
object: { open: ..., closed: ... }
.
renderClosed?: boolean
Render the Accordion content, even if it is closed.
isWrapped?: boolean
Enables the wrapped Accordion style. Use this if the Accordion is nested inside of another Accordion.
dataGroup?: string
A string identifier for a group of Accordions. Only one Accordion of a group
that have the same dataGroup
-prop can be open.
className?: string
A classname that will be applied to the outer most <div>
-wrapper of the
Accordion.
id?: string
An HTML id that will be applied to the outer most <div>
-wrapper.
style?: { [key: string]: string | number }
A React style object that will be applied to the outer most <div>
-wrapper of
the Accordion.
styleBody?: { [key: string]: string | number }
A React style object that will be applied to the body of the Accordion.
onOpen?: function
A callback that is called when the Accordion gets opened.
onClose?: function
A callback that is called when the Accordion gets closed.
defaultOpened?: boolean
Wether the Accordion should be opened by default (when it first gets rendered).
reference?: function
A function that receives the ref of the outer most <div>
-element of the
Accordion.
autogrow?: boolean
Wether the Accordion should adjust its height in the opened state.
open?: boolean
Control the open state.
icon?: object | string | ReactNode
The icon that is displayed to the left of the Accordion head. Supply a
FontAwesome string like "fa fa-plane"
or a React component.
noRotate?: boolean
Disable the icon rotation.
fixed?: boolean
Disable the opening and closing logic. The Accordion will be stuck in one state.
noIcon?: boolean
Remove the Accordion to the left of the head.
onSearch?: function
A callback that will be called when the text in the search field on the right changes. This will also enable the search field.
onSearchEnter?: function
A callback that will be called when the enter-key is pressed in the search field.
searchPlaceholder?: string
The placeholder for the search field.
searchValue?: string
The value for the search field (for making a controlled input).
removeContentClosed?: boolean
Remove content from the Accordion body when it is closing.
onClick?: function
Add a click listener for the Accordion head.
disabled?: boolean
Disables the Accordion, which changes the style and removes any interactions.
controlled?: boolean
When set, the open-prop updates and onChange does not update the internal state.