@bouku/core 0.1.6 → 0.1.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,3 +1,4 @@
1
+ "use client";
1
2
  "use strict";
2
3
  var __importDefault = (this && this.__importDefault) || function (mod) {
3
4
  return (mod && mod.__esModule) ? mod : { "default": mod };
@@ -1,4 +1,4 @@
1
- import { type Row } from "./row";
1
+ import { type Row, type Action } from "./row";
2
2
  type ColumnOptions = {
3
3
  label?: string;
4
4
  pad?: number;
@@ -9,6 +9,7 @@ type Props<T extends Row> = {
9
9
  title?: string;
10
10
  config: Partial<Record<keyof T, boolean | ColumnOptions>>;
11
11
  items: T[];
12
+ actions?: Action<T>[];
12
13
  };
13
- export default function Table<T extends Row>({ style, title, config: initConfig, items }: Props<T>): import("react").JSX.Element;
14
+ export default function Table<T extends Row>({ style, title, config: initConfig, items, actions }: Props<T>): import("react").JSX.Element;
14
15
  export {};
@@ -4,25 +4,30 @@ Object.defineProperty(exports, "__esModule", { value: true });
4
4
  exports.default = Table;
5
5
  const jsx_runtime_1 = require("react/jsx-runtime");
6
6
  const sorter_1 = require("./sorter");
7
+ const ts_helper_kit_1 = require("ts-helper-kit");
8
+ const _1 = require("../../utils/functions/_");
7
9
  const flex_1 = require("../layout/flex");
8
10
  const header_1 = require("./header");
9
11
  const row_1 = require("./row");
10
- const ts_helper_kit_1 = require("ts-helper-kit");
11
- const style_1 = require("../../utils/functions/style");
12
- function Table({ style, title, config: initConfig, items }) {
12
+ function Table({ style, title, config: initConfig, items, actions }) {
13
13
  const { sortedItems, ...sortOptions } = (0, sorter_1.useSort)(items);
14
- const config = parseConfig(initConfig);
14
+ const config = parseConfig(initConfig, !!actions?.length);
15
15
  const columns = Object.keys(config);
16
16
  const condensedItems = (0, ts_helper_kit_1.condenseArray)(sortedItems, columns);
17
- return ((0, jsx_runtime_1.jsxs)(flex_1.ColumnBox, { style: (0, style_1.cn)(s.container, style), children: [title && ((0, jsx_runtime_1.jsx)("span", { className: s.title, children: title })), (0, jsx_runtime_1.jsxs)(flex_1.ColumnBox, { style: s.subcontainer, children: [(0, jsx_runtime_1.jsx)(header_1.Header, { ...sortOptions, columnLabels: Object.fromEntries(columns.map(c => [c, config[c].label])) }), (0, jsx_runtime_1.jsx)(flex_1.ColumnBox, { style: s.rows, children: condensedItems.map(({ item, count }, i) => ((0, jsx_runtime_1.jsx)(row_1.TableRow, { config: config, data: item, count: count }, i))) })] })] }));
17
+ return ((0, jsx_runtime_1.jsxs)(flex_1.ColumnBox, { style: (0, _1.cn)(s.container, style), children: [title && ((0, jsx_runtime_1.jsx)("span", { className: s.title, children: title })), (0, jsx_runtime_1.jsxs)(flex_1.ColumnBox, { style: s.subcontainer, children: [(0, jsx_runtime_1.jsx)(header_1.Header, { ...sortOptions, columnLabels: Object.fromEntries(columns.map(c => [c, config[c].label])) }), (0, jsx_runtime_1.jsx)(flex_1.ColumnBox, { style: s.rows, children: condensedItems.map(({ item, count }, i) => ((0, jsx_runtime_1.jsx)(row_1.TableRow, { config: config, data: item, count: count, actions: actions }, i))) })] })] }));
18
18
  }
19
- function parseConfig(config) {
20
- return Object.entries(config).reduce((acc, [key, value]) => {
19
+ function parseConfig(config, hasActions) {
20
+ const parsed = Object.entries(config).reduce((acc, [key, value]) => {
21
21
  const label = String(key).replaceAll("_", " ");
22
22
  const isObject = typeof value === "object" && value !== null;
23
23
  acc[key] = isObject ? { label, ...value } : { label };
24
24
  return acc;
25
25
  }, {});
26
+ // Automatically append an actions column if actions exist
27
+ if (hasActions) {
28
+ parsed["actions"] = { label: "Actions" };
29
+ }
30
+ return parsed;
26
31
  }
27
32
  const s = {
28
33
  container: "gap-3 overflow-hidden",
@@ -4,12 +4,19 @@ type ColumnOptions = {
4
4
  pad?: number;
5
5
  link?: string;
6
6
  };
7
+ export type Action<T> = {
8
+ variant?: "accent" | "surface" | "error";
9
+ icon?: React.ReactNode;
10
+ style?: string;
11
+ handler: (row: T) => void;
12
+ };
7
13
  interface Props<T> {
8
14
  config: Record<string, ColumnOptions>;
9
15
  data: T;
10
16
  count: number;
17
+ actions?: Action<T>[];
11
18
  }
12
19
  export type Row = Record<string, CellValue>;
13
20
  export type CellValue = string | number | boolean | object | null;
14
- export declare function TableRow<T extends Row>({ config, data, count }: Props<T>): import("react").JSX.Element;
21
+ export declare function TableRow<T extends Row>({ config, data, actions, count }: Props<T>): import("react").JSX.Element;
15
22
  export {};
@@ -7,13 +7,22 @@ exports.isDateLike = void 0;
7
7
  exports.TableRow = TableRow;
8
8
  const jsx_runtime_1 = require("react/jsx-runtime");
9
9
  const flex_1 = require("../layout/flex");
10
- const ts_helper_kit_1 = require("ts-helper-kit");
10
+ const _1 = require("../button/_");
11
11
  const check_circle_svg_1 = __importDefault(require("../../assets/icons/check-circle.svg"));
12
12
  const x_circle_svg_1 = __importDefault(require("../../assets/icons/x-circle.svg"));
13
+ const ts_helper_kit_1 = require("ts-helper-kit");
13
14
  const isDateLike = (val) => !isNaN(Date.parse(val)) && isNaN(Number(val));
14
15
  exports.isDateLike = isDateLike;
15
- function TableRow({ config, data, count }) {
16
+ function TableRow({ config, data, actions, count }) {
16
17
  return ((0, jsx_runtime_1.jsxs)(flex_1.RowBox, { style: s.container, children: [Object.keys(config).map((key, i) => {
18
+ if (key === "actions" && actions) {
19
+ return ((0, jsx_runtime_1.jsx)(flex_1.RowBox, { style: s.cell, children: (0, jsx_runtime_1.jsx)(flex_1.RowBox, { style: "gap-2 items-center", children: actions.map((action, actionIdx) => {
20
+ const variantStyles = action.variant === "error" ? "bg-error/10 text-error hover:bg-error/20" :
21
+ action.variant === "accent" ? "bg-accent/10 text-accent hover:bg-accent/20" :
22
+ "bg-surface-light text-primary hover:bg-surface-light/80";
23
+ return ((0, jsx_runtime_1.jsx)(_1.Button, { size: "xs", variant: "surface", style: action.style, icon: action.icon, action: () => action.handler(data) }, actionIdx));
24
+ }) }) }, i));
25
+ }
17
26
  const rawValue = data[key];
18
27
  const value = rawValue === 0 ? 0 : (data[key] ?? "N/A");
19
28
  const options = config[key];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bouku/core",
3
- "version": "0.1.6",
3
+ "version": "0.1.7",
4
4
  "main": "./dist/index.js",
5
5
  "types": "./dist/index.d.ts",
6
6
  "license": "MIT",