baaz-custom-components 3.3.3 → 5.0.0

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.
package/dist/index.mjs CHANGED
@@ -1913,7 +1913,431 @@ var CustomBreadcrumb = ({
1913
1913
  ] }) });
1914
1914
  };
1915
1915
  var breadcrumb_default = CustomBreadcrumb;
1916
+
1917
+ // src/components/custom/grid/index.tsx
1918
+ import { useRef as useRef2 } from "react";
1919
+ import { Grid as SvarGrid, WillowDark } from "@svar-ui/react-grid";
1920
+
1921
+ // src/components/custom/grid/sortableHeaderCell.tsx
1922
+ import { MoveDown, MoveUp } from "lucide-react";
1923
+ import { jsx as jsx23, jsxs as jsxs17 } from "react/jsx-runtime";
1924
+ function SortableHeaderCell({ cell, sortKey, sortOrder, onSortChange }) {
1925
+ if (!(cell == null ? void 0 : cell.id)) return /* @__PURE__ */ jsx23("span", { children: cell == null ? void 0 : cell.text });
1926
+ const active = sortKey === cell.id;
1927
+ const handleClick = () => {
1928
+ if (!onSortChange) return;
1929
+ if (active) {
1930
+ onSortChange(cell.id, sortOrder === "asc" ? "desc" : "asc");
1931
+ } else {
1932
+ onSortChange(cell.id, "asc");
1933
+ }
1934
+ };
1935
+ return /* @__PURE__ */ jsxs17(
1936
+ "div",
1937
+ {
1938
+ onClick: handleClick,
1939
+ className: "cursor-pointer flex items-center gap-2 justify-between select-none",
1940
+ children: [
1941
+ /* @__PURE__ */ jsx23("span", { children: cell.text }),
1942
+ active && (sortOrder === "asc" ? /* @__PURE__ */ jsx23(MoveDown, { className: "w-3 h-4" }) : /* @__PURE__ */ jsx23(MoveUp, { className: "w-3 h-4" }))
1943
+ ]
1944
+ }
1945
+ );
1946
+ }
1947
+ var sortableHeaderCell_default = SortableHeaderCell;
1948
+
1949
+ // src/utils/exportPdf.ts
1950
+ import jsPDF from "jspdf";
1951
+ import autoTable from "jspdf-autotable";
1952
+ function exportPdf(rows, name) {
1953
+ if (!rows.length) return;
1954
+ const doc = new jsPDF();
1955
+ const columns = Object.keys(rows[0]);
1956
+ const body = rows.map((row) => columns.map((col) => {
1957
+ var _a;
1958
+ return String((_a = row[col]) != null ? _a : "");
1959
+ }));
1960
+ autoTable(doc, {
1961
+ head: [columns],
1962
+ body,
1963
+ styles: { fontSize: 8 },
1964
+ headStyles: { fillColor: [40, 40, 40] }
1965
+ });
1966
+ doc.save(`${name != null ? name : "export"}.pdf`);
1967
+ }
1968
+
1969
+ // src/utils/exportExcel.ts
1970
+ import * as XLSX from "xlsx";
1971
+ function exportExcel(rows, name) {
1972
+ if (!rows.length) return;
1973
+ const worksheet = XLSX.utils.json_to_sheet(rows);
1974
+ const workbook = XLSX.utils.book_new();
1975
+ XLSX.utils.book_append_sheet(workbook, worksheet, "Data");
1976
+ XLSX.writeFile(workbook, `${name != null ? name : "export"}.xlsx`);
1977
+ }
1978
+
1979
+ // src/components/custom/grid/gridHeader.tsx
1980
+ import { useEffect as useEffect5, useRef, useState as useState6 } from "react";
1981
+ import {
1982
+ Download,
1983
+ FileText,
1984
+ FileSpreadsheet,
1985
+ Search,
1986
+ ListFilter,
1987
+ X as X2
1988
+ } from "lucide-react";
1989
+ import { useSearchParams, useRouter } from "next/navigation";
1990
+ import { jsx as jsx24, jsxs as jsxs18 } from "react/jsx-runtime";
1991
+ var GridHeader = ({
1992
+ onPdf,
1993
+ onExcel,
1994
+ onSearch,
1995
+ download,
1996
+ search,
1997
+ searchValue,
1998
+ searchkey = "",
1999
+ filterList
2000
+ }) => {
2001
+ var _a, _b, _c, _d, _e, _f, _g, _h;
2002
+ const router = useRouter();
2003
+ const searchParams = useSearchParams();
2004
+ const [downloadMenu, setDownloadMenu] = useState6(false);
2005
+ const [filterMenu, setFilterMenu] = useState6(false);
2006
+ const [filter, setFilter] = useState6({});
2007
+ const downloadRef = useRef(null);
2008
+ const filterRef = useRef(null);
2009
+ const selectedColumn = filterList.find((f) => f.columnName === filter.column);
2010
+ const selectedOperator = selectedColumn == null ? void 0 : selectedColumn.operators.find(
2011
+ (op) => op.label === filter.operator
2012
+ );
2013
+ const updateParams = (next) => {
2014
+ const params = new URLSearchParams(searchParams.toString());
2015
+ if (next.column) params.set("column", next.column);
2016
+ else params.delete("column");
2017
+ if (next.operator) params.set("operator", next.operator);
2018
+ else params.delete("operator");
2019
+ if (next.value) {
2020
+ (selectedOperator == null ? void 0 : selectedOperator.value) === "date" ? params.set("value", String(Date.parse(next.value))) : params.set("value", next.value);
2021
+ } else params.delete("value");
2022
+ if (next.startDate)
2023
+ params.set("startDate", String(Date.parse(next.startDate)));
2024
+ else params.delete("startDate");
2025
+ if (next.endDate) params.set("endDate", String(Date.parse(next.endDate)));
2026
+ else params.delete("endDate");
2027
+ router.replace(`?${params.toString()}`);
2028
+ };
2029
+ const clearFilters = () => {
2030
+ setFilter({});
2031
+ router.replace("?", { scroll: false });
2032
+ };
2033
+ useEffect5(() => {
2034
+ const handleClickOutside = (e) => {
2035
+ if (downloadRef.current && !downloadRef.current.contains(e.target)) {
2036
+ setDownloadMenu(false);
2037
+ }
2038
+ if (filterRef.current && !filterRef.current.contains(e.target)) {
2039
+ setFilterMenu(false);
2040
+ }
2041
+ };
2042
+ document.addEventListener("mousedown", handleClickOutside);
2043
+ return () => document.removeEventListener("mousedown", handleClickOutside);
2044
+ }, []);
2045
+ return /* @__PURE__ */ jsxs18("div", { className: "flex justify-end gap-4 items-center", children: [
2046
+ filterList.length > 0 && /* @__PURE__ */ jsxs18("div", { className: "relative", ref: filterRef, children: [
2047
+ /* @__PURE__ */ jsx24(
2048
+ "button",
2049
+ {
2050
+ onClick: () => setFilterMenu((p) => !p),
2051
+ className: "py-2 px-4 rounded-md bg-input hover:bg-muted transition cursor-pointer",
2052
+ children: /* @__PURE__ */ jsx24(ListFilter, { size: 18 })
2053
+ }
2054
+ ),
2055
+ filterMenu && /* @__PURE__ */ jsxs18("div", { className: "absolute top-full right-0 mt-2 p-3 !min-w-[30rem] rounded-md border bg-background shadow-lg z-50 flex items-center gap-2", children: [
2056
+ /* @__PURE__ */ jsxs18(
2057
+ "select",
2058
+ {
2059
+ className: "px-4 py-2 rounded-md bg-input text-sm focus:outline-none",
2060
+ value: (_a = filter.column) != null ? _a : "",
2061
+ onChange: (e) => {
2062
+ const next = {
2063
+ column: e.target.value,
2064
+ operator: null,
2065
+ value: null
2066
+ };
2067
+ setFilter(next);
2068
+ updateParams(next);
2069
+ },
2070
+ children: [
2071
+ /* @__PURE__ */ jsx24("option", { value: "", disabled: true, children: "Columns" }),
2072
+ filterList.map((f) => /* @__PURE__ */ jsx24("option", { value: f.columnName, children: f.columnName }, f.columnName))
2073
+ ]
2074
+ }
2075
+ ),
2076
+ /* @__PURE__ */ jsxs18(
2077
+ "select",
2078
+ {
2079
+ className: "px-4 py-2 rounded-md bg-input text-sm focus:outline-none",
2080
+ value: (_b = filter.operator) != null ? _b : "",
2081
+ disabled: !selectedColumn,
2082
+ onChange: (e) => {
2083
+ const next = __spreadProps(__spreadValues({}, filter), {
2084
+ operator: e.target.value,
2085
+ value: void 0
2086
+ });
2087
+ setFilter(next);
2088
+ updateParams(next);
2089
+ },
2090
+ children: [
2091
+ /* @__PURE__ */ jsx24("option", { value: "", disabled: true, children: "Operator" }),
2092
+ selectedColumn == null ? void 0 : selectedColumn.operators.map((op) => /* @__PURE__ */ jsx24("option", { value: op.label, children: op.label }, op.label))
2093
+ ]
2094
+ }
2095
+ ),
2096
+ (selectedOperator == null ? void 0 : selectedOperator.value) === "input" && /* @__PURE__ */ jsx24(
2097
+ "input",
2098
+ {
2099
+ className: "px-4 py-2 rounded-md bg-input text-sm focus:outline-none",
2100
+ placeholder: "Filter value",
2101
+ value: (_c = filter.value) != null ? _c : "",
2102
+ onChange: (e) => {
2103
+ const next = __spreadProps(__spreadValues({}, filter), {
2104
+ value: e.target.value
2105
+ });
2106
+ setFilter(next);
2107
+ updateParams(next);
2108
+ }
2109
+ }
2110
+ ),
2111
+ (selectedOperator == null ? void 0 : selectedOperator.value) === "date" && /* @__PURE__ */ jsx24(
2112
+ "input",
2113
+ {
2114
+ type: "datetime-local",
2115
+ className: "px-4 py-2 rounded-md bg-input text-sm focus:outline-none",
2116
+ value: (_d = filter.value) != null ? _d : "",
2117
+ onChange: (e) => setFilter((prev) => __spreadProps(__spreadValues({}, prev), {
2118
+ value: e.target.value
2119
+ }))
2120
+ }
2121
+ ),
2122
+ (selectedOperator == null ? void 0 : selectedOperator.value) === "select" && /* @__PURE__ */ jsxs18(
2123
+ "select",
2124
+ {
2125
+ className: "px-2 py-1 rounded-md bg-input text-sm focus:outline-none",
2126
+ value: (_e = filter.value) != null ? _e : "",
2127
+ onChange: (e) => setFilter((prev) => __spreadProps(__spreadValues({}, prev), {
2128
+ value: e.target.value
2129
+ })),
2130
+ children: [
2131
+ /* @__PURE__ */ jsx24("option", { value: "", disabled: true, children: "Value" }),
2132
+ (_f = selectedOperator.options) == null ? void 0 : _f.map((opt) => /* @__PURE__ */ jsx24("option", { value: opt.value, children: opt.label }, opt.value))
2133
+ ]
2134
+ }
2135
+ ),
2136
+ (selectedOperator == null ? void 0 : selectedOperator.value) === "date-range" && /* @__PURE__ */ jsxs18("div", { className: "flex items-center gap-2", children: [
2137
+ /* @__PURE__ */ jsx24(
2138
+ "input",
2139
+ {
2140
+ type: "datetime-local",
2141
+ className: "px-3 py-2 rounded-md bg-input text-sm",
2142
+ value: (_g = filter.startDate) != null ? _g : "",
2143
+ onChange: (e) => {
2144
+ const nextRange = __spreadProps(__spreadValues({}, filter), {
2145
+ startDate: e.target.value
2146
+ });
2147
+ if (nextRange.startDate) {
2148
+ const next = __spreadProps(__spreadValues({}, filter), {
2149
+ startDate: nextRange.startDate
2150
+ });
2151
+ setFilter(next);
2152
+ updateParams(next);
2153
+ }
2154
+ }
2155
+ }
2156
+ ),
2157
+ /* @__PURE__ */ jsx24("span", { className: "text-sm text-muted-foreground", children: "to" }),
2158
+ /* @__PURE__ */ jsx24(
2159
+ "input",
2160
+ {
2161
+ type: "datetime-local",
2162
+ className: "px-3 py-2 rounded-md bg-input text-sm",
2163
+ value: (_h = filter.endDate) != null ? _h : "",
2164
+ onChange: (e) => {
2165
+ const nextRange = __spreadProps(__spreadValues({}, filter), {
2166
+ endDate: e.target.value
2167
+ });
2168
+ if (nextRange.startDate && nextRange.endDate) {
2169
+ const next = __spreadProps(__spreadValues({}, filter), {
2170
+ endDate: nextRange.endDate
2171
+ });
2172
+ setFilter(next);
2173
+ updateParams(next);
2174
+ }
2175
+ }
2176
+ }
2177
+ )
2178
+ ] }),
2179
+ /* @__PURE__ */ jsx24(
2180
+ "button",
2181
+ {
2182
+ onClick: clearFilters,
2183
+ className: "ml-auto p-2 rounded-md hover:bg-muted transition",
2184
+ title: "Clear filters",
2185
+ children: /* @__PURE__ */ jsx24(X2, { size: 16 })
2186
+ }
2187
+ )
2188
+ ] })
2189
+ ] }),
2190
+ download && /* @__PURE__ */ jsxs18("div", { className: "relative", ref: downloadRef, children: [
2191
+ /* @__PURE__ */ jsx24(
2192
+ "button",
2193
+ {
2194
+ onClick: () => setDownloadMenu((p) => !p),
2195
+ className: "py-2 px-4 rounded-md bg-input hover:bg-muted transition",
2196
+ children: /* @__PURE__ */ jsx24(Download, { size: 18 })
2197
+ }
2198
+ ),
2199
+ downloadMenu && /* @__PURE__ */ jsxs18("div", { className: "absolute top-full right-0 mt-2 w-32 rounded-md border bg-background shadow-lg z-50", children: [
2200
+ /* @__PURE__ */ jsxs18(
2201
+ "button",
2202
+ {
2203
+ onClick: () => {
2204
+ onPdf();
2205
+ setDownloadMenu(false);
2206
+ },
2207
+ className: "flex items-center gap-2 w-full px-3 py-2 text-sm hover:bg-muted",
2208
+ children: [
2209
+ /* @__PURE__ */ jsx24(FileText, { size: 14 }),
2210
+ "PDF"
2211
+ ]
2212
+ }
2213
+ ),
2214
+ /* @__PURE__ */ jsxs18(
2215
+ "button",
2216
+ {
2217
+ onClick: () => {
2218
+ onExcel();
2219
+ setDownloadMenu(false);
2220
+ },
2221
+ className: "flex items-center gap-2 w-full px-3 py-2 text-sm hover:bg-muted",
2222
+ children: [
2223
+ /* @__PURE__ */ jsx24(FileSpreadsheet, { size: 14 }),
2224
+ "Excel"
2225
+ ]
2226
+ }
2227
+ )
2228
+ ] })
2229
+ ] }),
2230
+ search && /* @__PURE__ */ jsxs18("div", { className: "py-2 px-4 w-[20rem] rounded-md border border-input bg-background flex items-center gap-2 text-sm", children: [
2231
+ /* @__PURE__ */ jsx24(Search, { className: "w-4 h-4" }),
2232
+ /* @__PURE__ */ jsx24(
2233
+ "input",
2234
+ {
2235
+ placeholder: `Search by ${searchkey}`,
2236
+ className: "w-full focus:outline-none bg-transparent",
2237
+ onChange: onSearch,
2238
+ value: searchValue
2239
+ }
2240
+ )
2241
+ ] })
2242
+ ] });
2243
+ };
2244
+ var gridHeader_default = GridHeader;
2245
+
2246
+ // src/components/custom/grid/index.tsx
2247
+ import { jsx as jsx25, jsxs as jsxs19 } from "react/jsx-runtime";
2248
+ function Grid(_a) {
2249
+ var _b = _a, {
2250
+ data,
2251
+ columns,
2252
+ sortKey,
2253
+ sortOrder,
2254
+ onSortChange,
2255
+ onExportPdf,
2256
+ onExportExcel,
2257
+ downloadable = true,
2258
+ fileNmae,
2259
+ fonts
2260
+ } = _b, rest = __objRest(_b, [
2261
+ "data",
2262
+ "columns",
2263
+ "sortKey",
2264
+ "sortOrder",
2265
+ "onSortChange",
2266
+ "onExportPdf",
2267
+ "onExportExcel",
2268
+ "downloadable",
2269
+ "fileNmae",
2270
+ "fonts"
2271
+ ]);
2272
+ const apiRef = useRef2(null);
2273
+ const svarColumns = columns.map((col) => {
2274
+ const _a2 = col, { sortable, header } = _a2, columnProps = __objRest(_a2, ["sortable", "header"]);
2275
+ if (!sortable) {
2276
+ return __spreadProps(__spreadValues({}, columnProps), {
2277
+ header
2278
+ });
2279
+ }
2280
+ return __spreadProps(__spreadValues({}, columnProps), {
2281
+ header: {
2282
+ text: header,
2283
+ cell: (props) => /* @__PURE__ */ jsx25(
2284
+ sortableHeaderCell_default,
2285
+ __spreadProps(__spreadValues({}, props), {
2286
+ sortKey,
2287
+ sortOrder,
2288
+ onSortChange
2289
+ })
2290
+ )
2291
+ }
2292
+ });
2293
+ });
2294
+ const init = (api) => {
2295
+ apiRef.current = api;
2296
+ };
2297
+ const getSelectedData = () => {
2298
+ var _a2, _b2;
2299
+ const selected = (_b2 = (_a2 = apiRef.current) == null ? void 0 : _a2.getState().selectedRows) != null ? _b2 : [];
2300
+ let selectedData = data.filter((row) => {
2301
+ return selected.includes(row.id);
2302
+ });
2303
+ return selectedData;
2304
+ };
2305
+ const handleExportPdf = () => {
2306
+ const selected = getSelectedData();
2307
+ if (selected.length) {
2308
+ exportPdf(selected, fileNmae);
2309
+ } else {
2310
+ onExportPdf == null ? void 0 : onExportPdf();
2311
+ }
2312
+ };
2313
+ const handleExportExcel = () => {
2314
+ const selected = getSelectedData();
2315
+ if (selected.length) {
2316
+ exportExcel(selected, fileNmae);
2317
+ } else {
2318
+ onExportExcel == null ? void 0 : onExportExcel();
2319
+ }
2320
+ };
2321
+ return /* @__PURE__ */ jsxs19("div", { className: "w-full h-full overflow-auto flex flex-col gap-4", children: [
2322
+ /* @__PURE__ */ jsx25(
2323
+ gridHeader_default,
2324
+ {
2325
+ onPdf: handleExportPdf,
2326
+ onExcel: handleExportExcel,
2327
+ download: downloadable,
2328
+ search: rest.search,
2329
+ searchkey: rest.searchkey,
2330
+ onSearch: rest.onSearch,
2331
+ searchValue: rest.searchValue,
2332
+ filterList: rest.filterList
2333
+ }
2334
+ ),
2335
+ /* @__PURE__ */ jsx25(WillowDark, { fonts, children: /* @__PURE__ */ jsx25(SvarGrid, __spreadProps(__spreadValues({}, rest), { data, columns: svarColumns, init })) })
2336
+ ] });
2337
+ }
2338
+ var grid_default = Grid;
1916
2339
  export {
1917
2340
  breadcrumb_default as CustomBreadcrumb,
2341
+ grid_default as Grid,
1918
2342
  Navbar
1919
2343
  };