Skip to content

Commit

Permalink
feat: add action componentProps and server barrel files (#72)
Browse files Browse the repository at this point in the history
* chore: add server barrel files

* feat: add dynamic children property in actions

* fix: responsive tabs styles

* test: fix test and lint

* docs: add changeset and switch children to componentProps

* fix: types
  • Loading branch information
tonai authored Nov 24, 2023
1 parent fbc68f5 commit c8d3d94
Show file tree
Hide file tree
Showing 20 changed files with 190 additions and 52 deletions.
7 changes: 7 additions & 0 deletions .changeset/cold-terms-pay.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@smile/react-front-kit-shared': minor
'@smile/react-front-kit-table': minor
'@smile/react-front-kit': minor
---

Add dynamic componentProps property in actions
7 changes: 7 additions & 0 deletions .changeset/sharp-bikes-dress.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@smile/react-front-kit-shared': patch
'@smile/react-front-kit-table': patch
'@smile/react-front-kit': patch
---

Add server barrel files
5 changes: 5 additions & 0 deletions .changeset/slow-trainers-fry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@smile/react-front-kit': patch
---

Fix responsiveTabs when layout switch between columns and rows
79 changes: 68 additions & 11 deletions package-lock.json

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions packages/react-front-kit-shared/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,16 @@
"type": "module",
"exports": {
".": "./dist/index.js",
"./server": "./dist/server.js",
"./storybook-utils": "./dist/storybook-utils/index.js",
"./test-utils": "./dist/test-utils/index.js"
},
"types": "./dist/index.d.ts",
"typesVersions": {
"*": {
"server": [
"dist/server.d.ts"
],
"storybook-utils": [
"dist/storybook-utils/index.d.ts"
],
Expand Down
3 changes: 3 additions & 0 deletions packages/react-front-kit-shared/src/server.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/* eslint-disable react-refresh/only-export-components */
export * from './helpers';
export * from './theme';
5 changes: 4 additions & 1 deletion packages/react-front-kit-shared/src/types/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,12 @@ export interface IActionConfirmModalProps<Item>

export interface IAction<Item> {
color?: string;
componentProps?:
| Record<string, unknown>
| ((item: Item) => Record<string, unknown>);
confirmModalProps?: IActionConfirmModalProps<Item>;
confirmation?: boolean;
icon: ReactNode | ((item: Item) => ReactNode);
icon?: ReactNode | ((item: Item) => ReactNode);
id: number | string;
isItemAction?: boolean;
isMassAction?: boolean;
Expand Down
4 changes: 4 additions & 0 deletions packages/react-front-kit-table/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,15 @@
"type": "module",
"exports": {
".": "./dist/index.js",
"./server": "./dist/server.js",
"./mock": "./dist/mock.js"
},
"types": "./dist/index.d.ts",
"typesVersions": {
"*": {
"server": [
"./dist/server.d.ts"
],
"mock": [
"./dist/mock.d.ts"
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,15 @@ export const useStyles = createStyles((theme) => ({
'0px 3.43489px 2.74791px 0px rgba(0, 0, 0, 0.02), 0px 8.6871px 6.94968px 0px rgba(0, 0, 0, 0.02), 0px 17.72087px 14.1767px 0px rgba(0, 0, 0, 0.03), 0px 36.50164px 29.20132px 0px rgba(0, 0, 0, 0.03), 0px 100px 80px 0px rgba(0, 0, 0, 0.05)',
},
rowActions: {
'& > *': {
alignItems: 'center',
display: 'flex',
height: '1.75rem',
justifyContent: 'center',
minHeight: '1.75rem',
minWidth: '1.75rem',
width: '1.75rem',
},
boxShadow: 'none',
display: 'flex',
justifyContent: 'space-between',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@ import {
} from 'mantine-react-table';
import { useState } from 'react';

import { getActionIcon, getActionLabel } from '../../helpers';
import {
getActionComponentProps,
getActionIcon,
getActionLabel,
} from '../../helpers';

import { useStyles } from './Table.style';

Expand Down Expand Up @@ -187,6 +191,7 @@ export function Table<Data extends Record<string, unknown>>(
onClick={() => handleAction(row, action)}
radius={4}
type="button"
{...getActionComponentProps(action, row)}
>
{getActionIcon(action, row)}
</ActionIcon>
Expand Down Expand Up @@ -223,6 +228,7 @@ export function Table<Data extends Record<string, unknown>>(
color={action.color}
icon={getActionIcon(action, row)}
onClick={() => handleAction(row, action)}
{...getActionComponentProps(action, row)}
>
{getActionLabel(action, row)}
</Menu.Item>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -844,7 +844,7 @@ exports[`Table matches snapshot 1`] = `
class="mantine-vej1ag"
>
<div
class="___ref-rowActions mantine-1idxz77"
class="___ref-rowActions mantine-126p8zl"
/>
</td>
</tr>
Expand Down Expand Up @@ -921,7 +921,7 @@ exports[`Table matches snapshot 1`] = `
class="mantine-vej1ag"
>
<div
class="___ref-rowActions mantine-1idxz77"
class="___ref-rowActions mantine-126p8zl"
/>
</td>
</tr>
Expand Down Expand Up @@ -998,7 +998,7 @@ exports[`Table matches snapshot 1`] = `
class="mantine-vej1ag"
>
<div
class="___ref-rowActions mantine-1idxz77"
class="___ref-rowActions mantine-126p8zl"
/>
</td>
</tr>
Expand Down Expand Up @@ -1075,7 +1075,7 @@ exports[`Table matches snapshot 1`] = `
class="mantine-vej1ag"
>
<div
class="___ref-rowActions mantine-1idxz77"
class="___ref-rowActions mantine-126p8zl"
/>
</td>
</tr>
Expand Down Expand Up @@ -1152,7 +1152,7 @@ exports[`Table matches snapshot 1`] = `
class="mantine-vej1ag"
>
<div
class="___ref-rowActions mantine-1idxz77"
class="___ref-rowActions mantine-126p8zl"
/>
</td>
</tr>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -927,7 +927,7 @@ exports[`TableGridView matches snapshot 1`] = `
class="mantine-vej1ag"
>
<div
class="___ref-rowActions mantine-1idxz77"
class="___ref-rowActions mantine-126p8zl"
>
<button
class="mantine-UnstyledButton-root mantine-ActionIcon-root mantine-qf5o1f"
Expand Down Expand Up @@ -1091,7 +1091,7 @@ exports[`TableGridView matches snapshot 1`] = `
class="mantine-vej1ag"
>
<div
class="___ref-rowActions mantine-1idxz77"
class="___ref-rowActions mantine-126p8zl"
>
<button
class="mantine-UnstyledButton-root mantine-ActionIcon-root mantine-qf5o1f"
Expand Down Expand Up @@ -1255,7 +1255,7 @@ exports[`TableGridView matches snapshot 1`] = `
class="mantine-vej1ag"
>
<div
class="___ref-rowActions mantine-1idxz77"
class="___ref-rowActions mantine-126p8zl"
>
<button
class="mantine-UnstyledButton-root mantine-ActionIcon-root mantine-qf5o1f"
Expand Down Expand Up @@ -1419,7 +1419,7 @@ exports[`TableGridView matches snapshot 1`] = `
class="mantine-vej1ag"
>
<div
class="___ref-rowActions mantine-1idxz77"
class="___ref-rowActions mantine-126p8zl"
>
<button
class="mantine-UnstyledButton-root mantine-ActionIcon-root mantine-qf5o1f"
Expand Down Expand Up @@ -1583,7 +1583,7 @@ exports[`TableGridView matches snapshot 1`] = `
class="mantine-vej1ag"
>
<div
class="___ref-rowActions mantine-1idxz77"
class="___ref-rowActions mantine-126p8zl"
>
<button
class="mantine-UnstyledButton-root mantine-ActionIcon-root mantine-qf5o1f"
Expand Down Expand Up @@ -1747,7 +1747,7 @@ exports[`TableGridView matches snapshot 1`] = `
class="mantine-vej1ag"
>
<div
class="___ref-rowActions mantine-1idxz77"
class="___ref-rowActions mantine-126p8zl"
>
<button
class="mantine-UnstyledButton-root mantine-ActionIcon-root mantine-qf5o1f"
Expand Down Expand Up @@ -1911,7 +1911,7 @@ exports[`TableGridView matches snapshot 1`] = `
class="mantine-vej1ag"
>
<div
class="___ref-rowActions mantine-1idxz77"
class="___ref-rowActions mantine-126p8zl"
>
<button
class="mantine-UnstyledButton-root mantine-ActionIcon-root mantine-qf5o1f"
Expand Down
22 changes: 20 additions & 2 deletions packages/react-front-kit-table/src/helpers/table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,33 @@ export function getActionIcon<Data extends Record<string, unknown>>(
rows?: MRT_Row<Data> | MRT_Row<Data>[],
): ReactNode {
if (!action) {
return '';
return null;
}
if (isConfirmAction(action)) {
return typeof action.icon === 'function'
? action.icon(action.item)
: action.icon;
}
if (typeof action.icon === 'function') {
return rows ? action.icon(rows) : '';
return rows ? action.icon(rows) : null;
}
return action.icon;
}

export function getActionComponentProps<Data extends Record<string, unknown>>(
action?: ITableAction<Data> | null,
rows?: MRT_Row<Data> | MRT_Row<Data>[],
): Record<string, unknown> | undefined {
if (!action) {
return undefined;
}
if (isConfirmAction(action)) {
return typeof action.componentProps === 'function'
? action.componentProps(action.item)
: action.componentProps;
}
if (typeof action.componentProps === 'function') {
return rows ? action.componentProps(rows) : undefined;
}
return action.componentProps;
}
2 changes: 2 additions & 0 deletions packages/react-front-kit-table/src/server.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/* eslint-disable react-refresh/only-export-components */
export * from './helpers';
4 changes: 4 additions & 0 deletions packages/react-front-kit/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,15 @@
"type": "module",
"exports": {
".": "./dist/index.js",
"./server": "./dist/server.js",
"./mock": "./dist/mock.js"
},
"types": "./dist/index.d.ts",
"typesVersions": {
"*": {
"server": [
"./dist/server.d.ts"
],
"mock": [
"./dist/mock.d.ts"
]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
'use client';

import type { IDropdownButtonProps } from '../DropdownButton/DropdownButton';
import type { TabsListProps, TabsProps } from '@mantine/core';
import type { ReactElement, ReactNode } from 'react';
Expand Down Expand Up @@ -61,12 +63,17 @@ const useStyles = createStyles((theme) => ({
flexDirection: 'column',
},
hidden: {
'& .mantine-Tabs-tabLabel': {
fontSize: 18,
fontWeight: 600,
},
display: 'flex',
flexDirection: 'row',
flexWrap: 'nowrap',
opacity: 0,
overflow: 'hidden',
position: 'absolute',
top: 0,
visibility: 'hidden',
width: '100%',
},
Expand All @@ -77,7 +84,14 @@ const useStyles = createStyles((theme) => ({
paddingBottom: 16,
},
tabs: {
'& .mantine-Tabs-tabLabel': {
fontSize: 18,
fontWeight: 600,
},
flexWrap: 'nowrap',
position: 'absolute',
top: 0,
width: '100%',
},
}));

Expand All @@ -100,7 +114,7 @@ export function ResponsiveTabs(props: IResponsiveTabs): ReactNode {
tabsListProps,
...tabsProps
} = props;
const { ref, width } = useElementSize<HTMLDivElement>();
const { height, ref, width } = useElementSize<HTMLDivElement>();
const generatedId = useId();
const overflowButtonId = tabsProps.id
? `${tabsProps.id}-overflow-button`
Expand Down Expand Up @@ -140,6 +154,7 @@ export function ResponsiveTabs(props: IResponsiveTabs): ReactNode {
classNames={{ tab: classes.tab }}
onTabChange={handleTabChange}
radius="sm"
style={{ paddingTop: height }}
{...tabsProps}
>
<div ref={ref} aria-hidden className={classes.hidden} hidden>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ exports[`ResponsiveTabs matches snapshot 1`] = `
<div
class="mantine-Tabs-root mantine-1w1w6vj"
id="responsive-tabs"
style="padding-top: 0px;"
>
<div
aria-hidden="true"
class="mantine-18k7ubd"
class="mantine-38kat9"
hidden=""
>
<button
Expand Down Expand Up @@ -93,7 +94,7 @@ exports[`ResponsiveTabs matches snapshot 1`] = `
</div>
<div
aria-orientation="horizontal"
class="mantine-Tabs-tabsList mantine-1c0ih2u"
class="mantine-Tabs-tabsList mantine-1h6xllx"
role="tablist"
>
<button
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,9 @@ export function Thumbnail(props: IThumbnailProps): ReactElement {
: action.icon
}
onClick={() => handleMenuItem(action)}
{...(typeof action.componentProps === 'function'
? action.componentProps(props)
: action.componentProps)}
>
{typeof action.label === 'function'
? action.label(props)
Expand Down
4 changes: 4 additions & 0 deletions packages/react-front-kit/src/server.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/* eslint-disable react-refresh/only-export-components */
export * from '@smile/react-front-kit-shared/server';
// other exports
export * from './helpers';
Loading

0 comments on commit c8d3d94

Please sign in to comment.