2023-02-06 10:01:03 -08:00
|
|
|
import clsx from 'clsx';
|
2022-11-25 09:04:11 -08:00
|
|
|
import React from 'react';
|
2022-03-21 11:09:01 -07:00
|
|
|
import { v4 as uuidv4 } from 'uuid';
|
|
|
|
|
2022-07-22 10:30:16 -07:00
|
|
|
import { SelectDropdown } from '../features/forms';
|
|
|
|
|
2023-03-29 12:41:49 -07:00
|
|
|
import { Icon, HStack, Select } from './ui';
|
2022-03-21 11:09:01 -07:00
|
|
|
|
2023-01-10 15:03:15 -08:00
|
|
|
interface IList {
|
|
|
|
children: React.ReactNode
|
|
|
|
}
|
|
|
|
|
|
|
|
const List: React.FC<IList> = ({ children }) => (
|
2022-03-21 11:09:01 -07:00
|
|
|
<div className='space-y-0.5'>{children}</div>
|
|
|
|
);
|
|
|
|
|
2022-04-27 18:01:31 -07:00
|
|
|
interface IListItem {
|
2023-02-15 13:26:27 -08:00
|
|
|
label: React.ReactNode
|
|
|
|
hint?: React.ReactNode
|
|
|
|
onClick?(): void
|
2022-10-26 10:08:02 -07:00
|
|
|
onSelect?(): void
|
|
|
|
isSelected?: boolean
|
2023-01-10 15:03:15 -08:00
|
|
|
children?: React.ReactNode
|
2022-04-27 18:01:31 -07:00
|
|
|
}
|
2022-03-21 11:09:01 -07:00
|
|
|
|
2022-10-26 10:08:02 -07:00
|
|
|
const ListItem: React.FC<IListItem> = ({ label, hint, children, onClick, onSelect, isSelected }) => {
|
2022-03-21 11:09:01 -07:00
|
|
|
const id = uuidv4();
|
|
|
|
const domId = `list-group-${id}`;
|
|
|
|
|
2022-11-24 15:03:22 -08:00
|
|
|
const onKeyDown = (e: React.KeyboardEvent) => {
|
|
|
|
if (e.key === 'Enter') {
|
|
|
|
onClick!();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-03-21 11:09:01 -07:00
|
|
|
const Comp = onClick ? 'a' : 'div';
|
2022-10-26 10:08:02 -07:00
|
|
|
const LabelComp = onClick || onSelect ? 'span' : 'label';
|
2022-11-27 13:30:00 -08:00
|
|
|
const linkProps = onClick || onSelect ? { onClick: onClick || onSelect, onKeyDown, tabIndex: 0, role: 'link' } : {};
|
2022-03-21 11:09:01 -07:00
|
|
|
|
2022-07-22 10:30:16 -07:00
|
|
|
const renderChildren = React.useCallback(() => {
|
|
|
|
return React.Children.map(children, (child) => {
|
2022-03-21 11:09:01 -07:00
|
|
|
if (React.isValidElement(child)) {
|
2022-07-22 10:30:16 -07:00
|
|
|
const isSelect = child.type === SelectDropdown || child.type === Select;
|
|
|
|
|
2022-03-21 11:09:01 -07:00
|
|
|
return React.cloneElement(child, {
|
2023-06-13 14:25:39 -07:00
|
|
|
// @ts-ignore
|
2022-03-21 11:09:01 -07:00
|
|
|
id: domId,
|
2023-02-06 10:01:03 -08:00
|
|
|
className: clsx({
|
2022-07-22 10:30:16 -07:00
|
|
|
'w-auto': isSelect,
|
2022-09-11 15:32:34 -07:00
|
|
|
}, child.props.className),
|
2022-03-21 11:09:01 -07:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
2022-07-22 10:30:16 -07:00
|
|
|
});
|
|
|
|
}, [children, domId]);
|
2022-03-21 11:09:01 -07:00
|
|
|
|
|
|
|
return (
|
|
|
|
<Comp
|
2023-05-02 10:47:16 -07:00
|
|
|
className={clsx('flex items-center justify-between overflow-hidden bg-gradient-to-r from-gradient-start/20 to-gradient-end/20 px-4 py-2 first:rounded-t-lg last:rounded-b-lg dark:from-gradient-start/10 dark:to-gradient-end/10', {
|
2023-03-29 12:41:49 -07:00
|
|
|
'cursor-pointer hover:from-gradient-start/30 hover:to-gradient-end/30 dark:hover:from-gradient-start/5 dark:hover:to-gradient-end/5': typeof onClick !== 'undefined' || typeof onSelect !== 'undefined',
|
2022-03-21 11:09:01 -07:00
|
|
|
})}
|
|
|
|
{...linkProps}
|
|
|
|
>
|
2022-11-25 09:04:11 -08:00
|
|
|
<div className='flex flex-col py-1.5 pr-4 rtl:pl-4 rtl:pr-0'>
|
2023-04-03 09:17:29 -07:00
|
|
|
<LabelComp className='text-gray-900 dark:text-gray-100' htmlFor={domId}>{label}</LabelComp>
|
2022-03-21 11:09:01 -07:00
|
|
|
|
|
|
|
{hint ? (
|
2022-07-22 10:30:16 -07:00
|
|
|
<span className='text-sm text-gray-700 dark:text-gray-600'>{hint}</span>
|
2022-03-21 11:09:01 -07:00
|
|
|
) : null}
|
|
|
|
</div>
|
|
|
|
|
|
|
|
{onClick ? (
|
2023-05-02 10:47:16 -07:00
|
|
|
<HStack space={1} alignItems='center' className='overflow-hidden text-gray-700 dark:text-gray-600'>
|
2022-03-21 11:09:01 -07:00
|
|
|
{children}
|
|
|
|
|
2022-12-04 17:40:09 -08:00
|
|
|
<Icon src={require('@tabler/icons/chevron-right.svg')} className='ml-1 rtl:rotate-180' />
|
2022-11-25 09:04:11 -08:00
|
|
|
</HStack>
|
2022-10-26 10:28:50 -07:00
|
|
|
) : null}
|
2022-10-26 10:08:02 -07:00
|
|
|
|
|
|
|
{onSelect ? (
|
|
|
|
<div className='flex flex-row items-center text-gray-700 dark:text-gray-600'>
|
|
|
|
{children}
|
|
|
|
|
2023-03-29 12:41:49 -07:00
|
|
|
<div
|
|
|
|
className={
|
|
|
|
clsx({
|
|
|
|
'flex h-6 w-6 items-center justify-center rounded-full border-2 border-solid border-primary-500 dark:border-primary-400 transition': true,
|
|
|
|
'bg-primary-500 dark:bg-primary-400': isSelected,
|
|
|
|
'bg-transparent': !isSelected,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
>
|
2023-03-06 10:42:13 -08:00
|
|
|
<Icon
|
2023-03-29 12:41:49 -07:00
|
|
|
src={require('@tabler/icons/check.svg')}
|
|
|
|
className={
|
|
|
|
clsx({
|
|
|
|
'h-4 w-4 text-white dark:text-white transition-all duration-500': true,
|
|
|
|
'opacity-0 scale-50': !isSelected,
|
|
|
|
'opacity-100 scale-100': isSelected,
|
|
|
|
})
|
|
|
|
}
|
2023-03-06 10:42:13 -08:00
|
|
|
/>
|
2023-03-29 12:41:49 -07:00
|
|
|
</div>
|
2022-10-26 10:08:02 -07:00
|
|
|
</div>
|
2022-10-26 10:28:50 -07:00
|
|
|
) : null}
|
|
|
|
|
|
|
|
{typeof onClick === 'undefined' && typeof onSelect === 'undefined' ? renderChildren() : null}
|
2022-03-21 11:09:01 -07:00
|
|
|
</Comp>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export { List as default, ListItem };
|