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

[@svelteui/core]: Added Table component #456

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 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
10 changes: 10 additions & 0 deletions packages/svelteui-core/src/components/Table/Table.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { HTMLAttributes } from 'svelte/elements';
import { DefaultProps, SvelteUINumberSize } from '$lib/styles';

export interface TableProps extends DefaultProps, HTMLAttributes<HTMLElement> {
BeeMargarida marked this conversation as resolved.
Show resolved Hide resolved
withBorder?: boolean;
striped?: boolean;
highlightOnHover?: boolean;
cellPadding?: SvelteUINumberSize;
textAlign?: 'center' | 'right' | 'left';
}
12 changes: 12 additions & 0 deletions packages/svelteui-core/src/components/Table/Table.stories.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<script lang="ts">
import { Meta, Template, Story } from '@storybook/addon-svelte-csf';
import { Table } from './index';
</script>

<Meta title="Components/Table" component={Table} />

<Template let:args>
<Table {...args} />
BeeMargarida marked this conversation as resolved.
Show resolved Hide resolved
</Template>

<Story name="Table" id="tableStory" />
42 changes: 42 additions & 0 deletions packages/svelteui-core/src/components/Table/Table.styles.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { createStyles } from '$lib/styles';
import type { SvelteUINumberSize } from '$lib/styles';

export interface TableStyleParams {
withBorder: boolean;
striped: boolean;
highlightOnHover: boolean;
cellPadding: SvelteUINumberSize;
textAlign: 'center' | 'left' | 'right';
}

export const padding = {
sm: 4,
md: 8,
lg: 16
};

export default createStyles(
(theme, { withBorder, highlightOnHover, striped, cellPadding, textAlign }: TableStyleParams) => {
const { themeColor } = theme.fn;
return {
root: {
borderCollapse: 'collapse',
cursor: highlightOnHover ? 'pointer' : 'default',
border: withBorder ? `1px solid ${themeColor('gray', 6)}` : '',
borderStyle: withBorder ? '' : 'hidden',
'& > * > th, & > * > td': {
BeeMargarida marked this conversation as resolved.
Show resolved Hide resolved
padding:
typeof cellPadding === 'number' ? `${cellPadding}px` : `${padding[`${cellPadding}`]}px`,
border: `1px solid ${themeColor('gray', 6)}`,
BeeMargarida marked this conversation as resolved.
Show resolved Hide resolved
textAlign
},
'& > tr:nth-child(even)': {
backgroundColor: striped ? themeColor('gray', 9) : ''
},
'& > tr:hover': {
backgroundColor: highlightOnHover ? themeColor('gray', 9) : ''
}
BeeMargarida marked this conversation as resolved.
Show resolved Hide resolved
}
};
}
);
28 changes: 28 additions & 0 deletions packages/svelteui-core/src/components/Table/Table.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<script lang="ts">
import type { TableProps as $$TableProps } from './Table';
import useStyles from './Table.styles';

interface $$Props extends $$TableProps {}

export let withBorder: $$Props['withBorder'] = false,
striped: $$Props['striped'] = false,
highlightOnHover: $$Props['highlightOnHover'] = false,
className: $$Props['className'] = 'table',
cellPadding: $$Props['cellPadding'] = 'sm',
textAlign: $$Props['textAlign'] = 'center';

$: ({ cx, classes } = useStyles(
{
withBorder,
striped,
highlightOnHover,
cellPadding,
textAlign
},
{ name: 'Table' }
));
</script>

<table class={cx(className, classes.root)}>
BeeMargarida marked this conversation as resolved.
Show resolved Hide resolved
<slot />
</table>
3 changes: 3 additions & 0 deletions packages/svelteui-core/src/components/Table/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export { default as Table } from './Table.svelte';
export type { TableStyleParams } from './Table.styles';
export type { TableProps } from './Table';