File size: 1,556 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
/* eslint-disable no-nested-ternary */
import clsx from 'clsx';
import React, { cloneElement, isValidElement } from 'react';
import './style.scss';
type Props = {
title: string | React.ReactElement;
subtitle: string | React.ReactElement;
leading?: React.ReactNode | React.ReactElement;
trailing?: React.ReactNode | React.ReactElement;
className?: string;
contentClassName?: string;
};
export const ListTile = ( {
className,
contentClassName,
title,
subtitle,
leading,
trailing,
}: Props ) => {
if ( typeof title === 'string' ) {
title = <h2 className="list-tile__title"> { title } </h2>;
}
if ( typeof subtitle === 'string' ) {
subtitle = <span className="list-tile__subtitle"> { subtitle } </span>;
}
const leadingElement =
typeof leading === 'string' ? (
<div className="list-tile__leading">{ leading }</div>
) : isValidElement< { className: string } >( leading ) ? (
cloneElement( leading, {
className: clsx( 'list-tile__leading', leading.props.className ),
} )
) : null;
const trailingElement =
typeof trailing === 'string' ? (
<div className="list-tile__trailing">{ trailing }</div>
) : isValidElement< { className: string } >( trailing ) ? (
cloneElement( trailing, {
className: clsx( 'list-tile__trailing', trailing.props.className ),
} )
) : null;
return (
<div className={ clsx( 'list-tile', className ) }>
{ leadingElement }
<div className={ clsx( 'list-tile__content', contentClassName ) }>
{ title }
{ subtitle }
</div>
{ trailingElement }
</div>
);
};
|