soapbox/app/soapbox/components/list.tsx

98 wiersze
2.9 KiB
TypeScript
Czysty Zwykły widok Historia

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