@saptanshuwanjari/react-component-library 0.1.6 → 0.1.8

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
@@ -3,7 +3,7 @@ import { createContext, memo, useState, useContext, useTransition, useEffect, us
3
3
  import { zodResolver } from '@hookform/resolvers/zod';
4
4
  import { Controller, useForm } from 'react-hook-form';
5
5
  import { jsx, jsxs, Fragment } from 'react/jsx-runtime';
6
- import { Lock, EyeOff, Eye, ChevronDownIcon, Upload, X, Pencil, ArrowUp, ArrowDown, ArrowUpDown, SlidersHorizontal, Table as Table$1, LayoutGrid, CheckIcon, ChevronUpIcon } from 'lucide-react';
6
+ import { Lock, EyeOff, Eye, ChevronDownIcon, Upload, X, Pencil, Table as Table$1, LayoutGrid, ArrowUp, ArrowDown, ArrowUpDown, SlidersHorizontal, CheckIcon, ChevronUpIcon } from 'lucide-react';
7
7
  import { clsx } from 'clsx';
8
8
  import { twMerge } from 'tailwind-merge';
9
9
  import { useReactTable, getPaginationRowModel, getSortedRowModel, getFilteredRowModel, getCoreRowModel, flexRender } from '@tanstack/react-table';
@@ -574,16 +574,21 @@ function Root2({
574
574
  data,
575
575
  filters = [],
576
576
  defaultViewMode = "table",
577
+ viewMode: controlledViewMode,
578
+ onViewModeChange: controlledOnViewModeChange,
577
579
  defaultPageSize = 10,
578
580
  children
579
581
  }) {
580
- const [viewMode, setViewMode] = useState(defaultViewMode);
582
+ const [internalViewMode, setInternalViewMode] = useState(defaultViewMode);
581
583
  const [sorting, setSorting] = useState([]);
582
584
  const [columnFilters, setColumnFilters] = useState([]);
583
585
  const [columnVisibility, setColumnVisibility] = useState({});
584
586
  const [globalFilter, setGlobalFilter] = useState("");
585
587
  const [pagination, setPagination] = useState({ pageIndex: 0, pageSize: defaultPageSize });
586
588
  const [activeFilters, setActiveFilters] = useState({});
589
+ const isControlled = controlledViewMode !== void 0 && controlledOnViewModeChange !== void 0;
590
+ const viewMode = isControlled ? controlledViewMode : internalViewMode;
591
+ const setViewMode = isControlled ? controlledOnViewModeChange : setInternalViewMode;
587
592
  const filteredData = useMemo(() => {
588
593
  if (Object.keys(activeFilters).length === 0) return data;
589
594
  return data.filter((row) => {
@@ -996,8 +1001,41 @@ function Checkbox2({
996
1001
  }
997
1002
  );
998
1003
  }
999
- function Toolbar() {
1000
- const { table, viewMode, setViewMode, filters, activeFilters, setActiveFilters } = useDataTable();
1004
+ function ViewOptions({ viewMode: propViewMode, onViewModeChange: propOnViewModeChange }) {
1005
+ let contextStr;
1006
+ try {
1007
+ contextStr = useDataTable();
1008
+ } catch (e) {
1009
+ }
1010
+ const mode = propViewMode ?? contextStr?.viewMode ?? "table";
1011
+ const setMode = propOnViewModeChange ?? contextStr?.setViewMode ?? (() => {
1012
+ });
1013
+ return /* @__PURE__ */ jsxs("div", { className: "flex items-center border rounded-md bg-background", children: [
1014
+ /* @__PURE__ */ jsx(
1015
+ Button6,
1016
+ {
1017
+ variant: mode === "table" ? "secondary" : "ghost",
1018
+ size: "sm",
1019
+ onClick: () => setMode("table"),
1020
+ className: "rounded-r-none h-8 px-2 lg:px-3",
1021
+ children: /* @__PURE__ */ jsx(Table$1, { className: "h-4 w-4" })
1022
+ }
1023
+ ),
1024
+ /* @__PURE__ */ jsx(
1025
+ Button6,
1026
+ {
1027
+ variant: mode === "grid" ? "secondary" : "ghost",
1028
+ size: "sm",
1029
+ onClick: () => setMode("grid"),
1030
+ className: "rounded-l-none h-8 px-2 lg:px-3",
1031
+ children: /* @__PURE__ */ jsx(LayoutGrid, { className: "h-4 w-4" })
1032
+ }
1033
+ )
1034
+ ] });
1035
+ }
1036
+ var view_options_default = ViewOptions;
1037
+ function Toolbar({ viewOptions } = {}) {
1038
+ const { table, filters, activeFilters, setActiveFilters } = useDataTable();
1001
1039
  const handleFilterChange = (filterId, value) => {
1002
1040
  setActiveFilters((prev) => ({
1003
1041
  ...prev,
@@ -1014,115 +1052,116 @@ function Toolbar() {
1014
1052
  return { ...prev, [filterId]: newValues };
1015
1053
  });
1016
1054
  };
1017
- return /* @__PURE__ */ jsxs("div", { className: "flex flex-col sm:flex-row sm:items-center sm:justify-between gap-4", children: [
1018
- /* @__PURE__ */ jsx("div", { className: "flex-1 max-w-sm", children: /* @__PURE__ */ jsx(
1055
+ return /* @__PURE__ */ jsxs("div", { className: "space-y-4 py-4", children: [
1056
+ /* @__PURE__ */ jsx("div", { className: "w-full", children: /* @__PURE__ */ jsx(
1019
1057
  Input5,
1020
1058
  {
1021
1059
  placeholder: "Search...",
1022
1060
  value: table.getState().globalFilter ?? "",
1023
1061
  onChange: (e) => table.setGlobalFilter(e.target.value),
1024
- className: "w-full"
1062
+ className: "w-full max-w-sm"
1025
1063
  }
1026
1064
  ) }),
1027
- /* @__PURE__ */ jsxs("div", { className: "flex items-center gap-2 flex-wrap", children: [
1028
- filters.length > 0 && /* @__PURE__ */ jsxs(Popover, { children: [
1029
- /* @__PURE__ */ jsx(PopoverTrigger, { asChild: true, children: /* @__PURE__ */ jsxs(Button6, { variant: "outline", size: "sm", children: [
1030
- /* @__PURE__ */ jsx(SlidersHorizontal, { className: "h-4 w-4 mr-2" }),
1031
- "Filters"
1032
- ] }) }),
1033
- /* @__PURE__ */ jsx(PopoverContent, { className: "w-80", children: /* @__PURE__ */ jsxs("div", { className: "space-y-4", children: [
1034
- /* @__PURE__ */ jsx("h4", { className: "font-medium text-sm", children: "Filters" }),
1035
- filters.map((filter) => /* @__PURE__ */ jsxs("div", { className: "space-y-2", children: [
1036
- /* @__PURE__ */ jsx("label", { className: "text-sm font-medium", children: filter.label }),
1037
- filter.multiSelect ? /* @__PURE__ */ jsx("div", { className: "space-y-2", children: filter.options.map((option) => /* @__PURE__ */ jsxs("div", { className: "flex items-center space-x-2", children: [
1038
- /* @__PURE__ */ jsx(
1039
- Checkbox2,
1040
- {
1041
- id: `${filter.id}-${option.value}`,
1042
- checked: Array.isArray(activeFilters[filter.id]) && activeFilters[filter.id].includes(option.value),
1043
- onCheckedChange: () => handleMultiFilterToggle(filter.id, option.value)
1044
- }
1045
- ),
1065
+ /* @__PURE__ */ jsxs("div", { className: "flex flex-col sm:flex-row sm:items-center sm:justify-between gap-4", children: [
1066
+ /* @__PURE__ */ jsxs("div", { className: "flex items-center gap-2 flex-wrap flex-1", children: [
1067
+ filters.map((filter) => /* @__PURE__ */ jsx("div", { children: filter.multiSelect ? /* @__PURE__ */ jsxs(Popover, { children: [
1068
+ /* @__PURE__ */ jsx(PopoverTrigger, { asChild: true, children: /* @__PURE__ */ jsxs(Button6, { variant: "outline", size: "sm", className: "h-8 border-dashed", children: [
1069
+ /* @__PURE__ */ jsx(SlidersHorizontal, { className: "h-4 w-4 mr-2" }),
1070
+ filter.label,
1071
+ activeFilters[filter.id] && (Array.isArray(activeFilters[filter.id]) ? activeFilters[filter.id].length > 0 : true) && /* @__PURE__ */ jsx("span", { className: "ml-1 rounded-sm bg-primary px-1 text-xs text-primary-foreground", children: Array.isArray(activeFilters[filter.id]) ? activeFilters[filter.id].length : 1 })
1072
+ ] }) }),
1073
+ /* @__PURE__ */ jsx(PopoverContent, { className: "w-[200px] p-0", align: "start", children: /* @__PURE__ */ jsxs("div", { className: "p-1", children: [
1074
+ filter.options.map((option) => /* @__PURE__ */ jsxs(
1075
+ "div",
1076
+ {
1077
+ className: "flex items-center space-x-2 rounded-sm px-2 py-1.5 hover:bg-accent cursor-pointer",
1078
+ onClick: () => handleMultiFilterToggle(filter.id, option.value),
1079
+ children: [
1080
+ /* @__PURE__ */ jsx(
1081
+ Checkbox2,
1082
+ {
1083
+ id: `${filter.id}-${option.value}`,
1084
+ checked: Array.isArray(activeFilters[filter.id]) && activeFilters[filter.id].includes(option.value),
1085
+ onCheckedChange: () => handleMultiFilterToggle(filter.id, option.value)
1086
+ }
1087
+ ),
1088
+ /* @__PURE__ */ jsx(
1089
+ "label",
1090
+ {
1091
+ className: "text-sm cursor-pointer flex-1",
1092
+ children: option.label
1093
+ }
1094
+ )
1095
+ ]
1096
+ },
1097
+ option.value
1098
+ )),
1099
+ activeFilters[filter.id] && (Array.isArray(activeFilters[filter.id]) ? activeFilters[filter.id].length > 0 : false) && /* @__PURE__ */ jsxs(Fragment, { children: [
1100
+ /* @__PURE__ */ jsx("div", { className: "h-px bg-border my-1" }),
1046
1101
  /* @__PURE__ */ jsx(
1047
- "label",
1102
+ "div",
1048
1103
  {
1049
- htmlFor: `${filter.id}-${option.value}`,
1050
- className: "text-sm cursor-pointer",
1051
- children: option.label
1104
+ className: "px-2 py-1.5 text-center text-sm cursor-pointer hover:bg-accent rounded-sm",
1105
+ onClick: () => setActiveFilters((prev) => ({ ...prev, [filter.id]: [] })),
1106
+ children: "Clear filters"
1052
1107
  }
1053
1108
  )
1054
- ] }, option.value)) }) : /* @__PURE__ */ jsxs(
1055
- Select2,
1056
- {
1057
- value: activeFilters[filter.id] || "all",
1058
- onValueChange: (value) => handleFilterChange(filter.id, value),
1059
- children: [
1060
- /* @__PURE__ */ jsx(SelectTrigger, { children: /* @__PURE__ */ jsx(SelectValue2, { placeholder: filter.placeholder || "Select..." }) }),
1061
- /* @__PURE__ */ jsxs(SelectContent, { children: [
1062
- /* @__PURE__ */ jsx(SelectItem2, { value: "all", children: "All" }),
1063
- filter.options.map((option) => /* @__PURE__ */ jsx(SelectItem2, { value: option.value, children: option.label }, option.value))
1064
- ] })
1065
- ]
1066
- }
1067
- )
1068
- ] }, filter.id))
1069
- ] }) })
1070
- ] }),
1071
- /* @__PURE__ */ jsxs(Popover, { children: [
1072
- /* @__PURE__ */ jsx(PopoverTrigger, { asChild: true, children: /* @__PURE__ */ jsxs(Button6, { variant: "outline", size: "sm", children: [
1073
- /* @__PURE__ */ jsx(Eye, { className: "h-4 w-4 mr-2" }),
1074
- "Columns"
1075
- ] }) }),
1076
- /* @__PURE__ */ jsx(PopoverContent, { className: "w-56", children: /* @__PURE__ */ jsxs("div", { className: "space-y-2", children: [
1077
- /* @__PURE__ */ jsx("h4", { className: "font-medium text-sm", children: "Toggle Columns" }),
1078
- table.getAllColumns().filter((column) => column.getCanHide()).map((column) => /* @__PURE__ */ jsxs("div", { className: "flex items-center space-x-2", children: [
1079
- /* @__PURE__ */ jsx(
1080
- Checkbox2,
1081
- {
1082
- id: column.id,
1083
- checked: column.getIsVisible(),
1084
- onCheckedChange: (value) => column.toggleVisibility(!!value)
1085
- }
1086
- ),
1087
- /* @__PURE__ */ jsx("label", { htmlFor: column.id, className: "text-sm cursor-pointer", children: column.id })
1088
- ] }, column.id))
1089
- ] }) })
1090
- ] }),
1091
- /* @__PURE__ */ jsxs("div", { className: "flex items-center border rounded-md", children: [
1092
- /* @__PURE__ */ jsx(
1093
- Button6,
1094
- {
1095
- variant: viewMode === "table" ? "default" : "ghost",
1096
- size: "sm",
1097
- onClick: () => setViewMode("table"),
1098
- className: "rounded-r-none",
1099
- children: /* @__PURE__ */ jsx(Table$1, { className: "h-4 w-4" })
1100
- }
1101
- ),
1102
- /* @__PURE__ */ jsx(
1103
- Button6,
1104
- {
1105
- variant: viewMode === "grid" ? "default" : "ghost",
1106
- size: "sm",
1107
- onClick: () => setViewMode("grid"),
1108
- className: "rounded-l-none",
1109
- children: /* @__PURE__ */ jsx(LayoutGrid, { className: "h-4 w-4" })
1110
- }
1111
- )
1112
- ] }),
1113
- /* @__PURE__ */ jsxs("div", { className: "flex items-center gap-2", children: [
1114
- /* @__PURE__ */ jsx("label", { className: "text-sm text-muted-foreground", children: "Rows" }),
1115
- /* @__PURE__ */ jsxs(
1109
+ ] })
1110
+ ] }) })
1111
+ ] }) : /* @__PURE__ */ jsxs(
1116
1112
  Select2,
1117
1113
  {
1118
- value: String(table.getState().pagination.pageSize),
1119
- onValueChange: (value) => table.setPageSize(Number(value)),
1114
+ value: activeFilters[filter.id] || "all",
1115
+ onValueChange: (value) => handleFilterChange(filter.id, value),
1120
1116
  children: [
1121
- /* @__PURE__ */ jsx(SelectTrigger, { className: "w-20", children: /* @__PURE__ */ jsx(SelectValue2, {}) }),
1122
- /* @__PURE__ */ jsx(SelectContent, { children: [10, 20, 30, 40, 50].map((size) => /* @__PURE__ */ jsx(SelectItem2, { value: String(size), children: size }, size)) })
1117
+ /* @__PURE__ */ jsx(SelectTrigger, { className: "h-8 w-[150px] border-dashed", children: /* @__PURE__ */ jsx(SelectValue2, { placeholder: filter.placeholder || filter.label }) }),
1118
+ /* @__PURE__ */ jsxs(SelectContent, { children: [
1119
+ /* @__PURE__ */ jsxs(SelectItem2, { value: "all", children: [
1120
+ "All ",
1121
+ filter.label
1122
+ ] }),
1123
+ filter.options.map((option) => /* @__PURE__ */ jsx(SelectItem2, { value: option.value, children: option.label }, option.value))
1124
+ ] })
1123
1125
  ]
1124
1126
  }
1125
- )
1127
+ ) }, filter.id)),
1128
+ /* @__PURE__ */ jsxs(Popover, { children: [
1129
+ /* @__PURE__ */ jsx(PopoverTrigger, { asChild: true, children: /* @__PURE__ */ jsxs(Button6, { variant: "outline", size: "sm", className: "h-8 ml-auto sm:ml-0", children: [
1130
+ /* @__PURE__ */ jsx(Eye, { className: "h-4 w-4 mr-2" }),
1131
+ "Columns"
1132
+ ] }) }),
1133
+ /* @__PURE__ */ jsx(PopoverContent, { className: "w-56", align: "end", children: /* @__PURE__ */ jsxs("div", { className: "space-y-2", children: [
1134
+ /* @__PURE__ */ jsx("h4", { className: "font-medium text-sm", children: "Toggle Columns" }),
1135
+ table.getAllColumns().filter((column) => column.getCanHide()).map((column) => /* @__PURE__ */ jsxs("div", { className: "flex items-center space-x-2", children: [
1136
+ /* @__PURE__ */ jsx(
1137
+ Checkbox2,
1138
+ {
1139
+ id: column.id,
1140
+ checked: column.getIsVisible(),
1141
+ onCheckedChange: (value) => column.toggleVisibility(!!value)
1142
+ }
1143
+ ),
1144
+ /* @__PURE__ */ jsx("label", { htmlFor: column.id, className: "text-sm cursor-pointer", children: column.id })
1145
+ ] }, column.id))
1146
+ ] }) })
1147
+ ] })
1148
+ ] }),
1149
+ /* @__PURE__ */ jsxs("div", { className: "flex items-center gap-2", children: [
1150
+ viewOptions ? viewOptions : /* @__PURE__ */ jsx(ViewOptions, {}),
1151
+ /* @__PURE__ */ jsxs("div", { className: "flex items-center gap-2", children: [
1152
+ /* @__PURE__ */ jsx("label", { className: "text-sm text-muted-foreground whitespace-nowrap hidden sm:inline-block", children: "Rows" }),
1153
+ /* @__PURE__ */ jsxs(
1154
+ Select2,
1155
+ {
1156
+ value: String(table.getState().pagination.pageSize),
1157
+ onValueChange: (value) => table.setPageSize(Number(value)),
1158
+ children: [
1159
+ /* @__PURE__ */ jsx(SelectTrigger, { className: "w-16 h-8", children: /* @__PURE__ */ jsx(SelectValue2, {}) }),
1160
+ /* @__PURE__ */ jsx(SelectContent, { children: [10, 20, 30, 40, 50].map((size) => /* @__PURE__ */ jsx(SelectItem2, { value: String(size), children: size }, size)) })
1161
+ ]
1162
+ }
1163
+ )
1164
+ ] })
1126
1165
  ] })
1127
1166
  ] })
1128
1167
  ] });
@@ -1320,7 +1359,8 @@ var DataTable = Object.assign(root_default2, {
1320
1359
  Content: content_default,
1321
1360
  TableView: table_view_default,
1322
1361
  GridView: grid_view_default,
1323
- Pagination: pagination_default
1362
+ Pagination: pagination_default,
1363
+ ViewOptions: view_options_default
1324
1364
  });
1325
1365
  var data_table_default = DataTable;
1326
1366