Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changes in the title bar #121

Merged
merged 6 commits into from
Oct 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/components/DataTable/Row.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ import clsx from 'clsx';

import { LinkWrapper } from '../../utils/misc';
import DSRContext from '../../contexts/DSRContext';
import Icon from '../Icon';
import Icon, { IconInputType } from '../Icon';
import SkeletonItem from '../SkeletonItem';
import Checkbox from '../Checkbox';

import SelectionContext from './SelectionContext';

export type ItemListerProperty<Type> = {
id: string,
icon?: IconInputType,
label: ReactNode,
labelClassName?: string,
value: (self: Type, index?: number) => ReactNode,
Expand Down Expand Up @@ -55,7 +56,7 @@ const ItemListerItem = <Type extends { id: string }>({
'dsr-h-full dsr-text-color',
variant === 'grid' ? grid : 'dsr-border-b dsr-border-gray-500/20',
isPinned ? 'dsr-bg-background' : '',
isPinned ? 'group-hover:dsr-bg-background' : isDarkTheme ? 'group-hover:dsr-bg-neutral-800 ' : 'group-hover:dsr-bg-gray-500/20',
isPinned ? 'group-hover:dsr-bg-background' : isDarkTheme ? 'group-hover:dsr-bg-neutral-800' : 'group-hover:dsr-bg-gray-500/20',
]);

return (
Expand Down
148 changes: 85 additions & 63 deletions src/components/DataTable/TitleBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,38 +11,50 @@ import { ItemListerProperty } from './Row';
import SelectionContext from './SelectionContext';

type ItemListerTitleBarProps<Type> = {
properties: ItemListerProperty<Type>[],
onSort: (attribute: string, order?: ('asc' | 'desc')) => void,
currentSortAttribute?: string,
sortOrder?: 'asc' | 'desc',
colsWidth: (string | number)[],
isAccordionsOpen?: boolean,
toggleAccordions?: (state: boolean) => void,
properties: ItemListerProperty<Type>[];
onSort: (attribute: string, order?: 'asc' | 'desc') => void;
currentSortAttribute?: string;
sortOrder?: 'asc' | 'desc';
colsWidth: (string | number)[];
isAccordionsOpen?: boolean;
toggleAccordions?: (state: boolean) => void;
};

const thClasses = 'dsr-h-full dsr-bg-primary dsr-text-primaryTextColor';

const ItemListerTitleBar = <Type extends { id: string }>({
properties, currentSortAttribute, sortOrder, isAccordionsOpen = undefined, toggleAccordions = () => {},
colsWidth, onSort = () => null,
properties,
currentSortAttribute,
sortOrder,
isAccordionsOpen = undefined,
toggleAccordions = () => {},
colsWidth,
onSort = () => null,
}: ItemListerTitleBarProps<Type>) => {

const { theme } = useContext(DSRContext);
const { isEnabled: isSelectEnabled, selectAll, deselectAll, isAllSelected } = useContext(SelectionContext);
const {
isEnabled: isSelectEnabled,
selectAll,
deselectAll,
isAllSelected,
} = useContext(SelectionContext);

let i = 0;

return (
<tr className="dsr-transition-transform">
<React.Fragment>
{isAccordionsOpen != null && (
<th
style={{ borderBottomColor: Color(theme?.color).fade(0.85).toString(), width: colsWidth[i++] }}
className={clsx([
'dsr-relative dsr-px-2 dsr-py-3',
thClasses,
])}
style={{
borderBottomColor: Color(theme?.color).fade(0.85).toString(),
width: colsWidth[i++],
}}
className={clsx(['dsr-relative dsr-px-2 dsr-py-3', thClasses])}
>
<button onClick={() => toggleAccordions(!isAccordionsOpen)} className="dsr-flex">
<button
onClick={() => toggleAccordions(!isAccordionsOpen)}
className="dsr-flex"
>
<Icon
icon="chevron-right"
size={18}
Expand All @@ -57,65 +69,75 @@ const ItemListerTitleBar = <Type extends { id: string }>({
{isSelectEnabled && (
<th
className={clsx(['dsr-py-3', thClasses])}
style={{ borderBottomColor: Color(theme?.color).fade(0.85).toString(), width: colsWidth[i++] }}
style={{
borderBottomColor: Color(theme?.color).fade(0.85).toString(),
width: colsWidth[i++],
}}
>
<div className="dsr-flex dsr-justify-center dsr-h-full dsr-items-center dsr-text-center">
<Checkbox
label=""
value=""
isChecked={isAllSelected?.()}
onChange={() => isAllSelected?.() ? deselectAll?.() : selectAll?.()}
onChange={() =>
isAllSelected?.() ? deselectAll?.() : selectAll?.()
}
/>
</div>
</th>
)}
{properties?.length > 0 && (
properties.filter((p) => !p.isHidden).map((p) => (
<th
className={clsx(['dsr-py-3', thClasses])}
key={p.id}
style={{
textAlign: p.textAlign,
borderBottomColor: Color(theme?.color).fade(0.85).toString(),
width: colsWidth[i++],
}}
>
{p?.allowSort ? (
<div
className={clsx(['dsr-flex dsr-items-center dsr-w-full dsr-gap-1', p?.labelClassName])}
>
{properties?.length > 0 &&
properties
.filter((p) => !p.isHidden)
.map((p) => (
<th
className={clsx(['dsr-py-3', thClasses])}
key={p.id}
style={{
textAlign: p.textAlign,
borderBottomColor: Color(theme?.color).fade(0.85).toString(),
width: colsWidth[i++],
}}
>
{p?.allowSort ? (
<div
className="dsr-px-2 dsr-m-auto dsr-font-semibold dsr-w-full"
style={{ textAlign: p.textAlign ?? 'left' }}
className={clsx([
'dsr-flex dsr-items-center dsr-w-full dsr-gap-1',
p?.labelClassName,
])}
>
{p.label}
<div
className="dsr-px-2 dsr-m-auto dsr-font-semibold dsr-flex dsr-items-center dsr-gap-2 dsr-w-full"
style={{ textAlign: p.textAlign ?? 'left' }}
>

{p.icon ? <Icon icon={p.icon} /> : null}
{p.label}
</div>
<div className="dsr-w-[30px] dsr-opacity-75 dsr-text-[90%] dsr-w-[14px]">
<SortButton
attribute={p.id}
currentAttribute={currentSortAttribute}
currentOrder={sortOrder}
onSort={onSort}
/>
</div>
</div>
<div className="dsr-w-[30px] dsr-opacity-75 dsr-text-[90%] dsr-w-[14px]">
<SortButton
attribute={p.id}
currentAttribute={currentSortAttribute}
currentOrder={sortOrder}
onSort={onSort}
/>
) : (
<div
className={clsx([
'dsr-flex dsr-font-semibold dsr-w-full dsr-items-center dsr-px-3 dsr-m-auto',
p?.labelClassName,
p.textAlign == 'right' ? 'dsr-justify-end' : p.textAlign == 'center' ? 'dsr-justify-center' : null,
])}
>
{p.label}
</div>
</div>
) : (
<div
className={clsx([
'dsr-flex dsr-font-semibold dsr-w-full dsr-items-center dsr-px-3 dsr-m-auto',
p?.labelClassName,
])}
style={{ textAlign: p.textAlign }}
>
{p.label}
</div>
)}
</th>
))
)}
</tr>
)}
</th>
))}
</React.Fragment>
);

};

export default ItemListerTitleBar;
export default ItemListerTitleBar;
16 changes: 12 additions & 4 deletions src/stories/components/display/DataTable.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -251,17 +251,17 @@ const Template: Story<DataTableProps<ItemType>> = (args) => {
{...args}
page={page}
setPage={setPage}
totalCount={ITEMS.length}
items={ITEMS.slice((page - 1) * 10, (page - 1) * 10 + 10)}
totalCount={args?.items ? args?.items.length : ITEMS.length}
items={args?.items ? args.items : ITEMS.slice((page - 1) * 10, (page - 1) * 10 + 10)}
maxHeight="380px"
properties={columns}
properties={args?.properties ? args?.properties : columns}
/>
);
};

export const Default = Template.bind({});

Default.args = {};

export const Grid = Template.bind({});
Grid.args = { variant:'grid' };

Expand All @@ -271,6 +271,14 @@ StripedRow.args = { variant:'striped-row' };
export const StripedColumn = Template.bind({});
StripedColumn.args = { variant:'striped-column' };

export const WithTitleIcon = Template.bind({});
WithTitleIcon.args = {
properties: columns.map((i) => ({
...i,
icon: 'home',
})),
};

export const SelectableTable = Template.bind({});
SelectableTable.args = {
allowSelection: true,
Expand Down
Loading