// Copyright 2017-2020 @polkadot/react-components authors & contributors // This software may be modified and distributed under the terms // of the Apache-2.0 license. See the LICENSE file for details. import { BareProps } from './types'; import React, { useMemo } from 'react'; import styled from 'styled-components'; import { useToggle } from '@polkadot/react-hooks'; import Icon from './Icon'; export interface Props extends BareProps { children?: React.ReactNode; isOpen?: boolean; summary: React.ReactNode; withHidden?: boolean; } function Expander ({ children, className, isOpen, summary, withHidden }: Props): React.ReactElement { const [isExpanded, toggleExpanded] = useToggle(isOpen); const hasContent = useMemo( (): boolean => !!children && (!Array.isArray(children) || children.length !== 0), [children] ); return (
{hasContent && ( )}{summary}
{hasContent && (isExpanded || withHidden) && (
{children}
)}
); } export default React.memo(styled(Expander)` &:not(.isExpanded) .ui--Expander-contents { display: none; } .ui--Expander-summary { cursor: pointer; display: block; white-space: nowrap; i.icon { margin-right: 0.5rem; } } `);