@saptanshuwanjari/react-component-library 0.1.0 → 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
@@ -1,12 +1,12 @@
1
+ import * as React2 from 'react';
1
2
  import { createContext, memo, useState, useContext, useTransition, useEffect, useMemo } from 'react';
2
3
  import { zodResolver } from '@hookform/resolvers/zod';
3
4
  import { Controller, useForm } from 'react-hook-form';
4
5
  import { jsx, jsxs, Fragment } from 'react/jsx-runtime';
5
- 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';
6
7
  import { clsx } from 'clsx';
7
8
  import { twMerge } from 'tailwind-merge';
8
9
  import { useReactTable, getPaginationRowModel, getSortedRowModel, getFilteredRowModel, getCoreRowModel, flexRender } from '@tanstack/react-table';
9
- import { Slot } from '@radix-ui/react-slot';
10
10
  import { cva } from 'class-variance-authority';
11
11
  import * as SelectPrimitive from '@radix-ui/react-select';
12
12
  import * as PopoverPrimitive from '@radix-ui/react-popover';
@@ -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) => {
@@ -651,6 +656,120 @@ function Input5({ className, type, ...props }) {
651
656
  }
652
657
  );
653
658
  }
659
+ function setRef(ref, value) {
660
+ if (typeof ref === "function") {
661
+ return ref(value);
662
+ } else if (ref !== null && ref !== void 0) {
663
+ ref.current = value;
664
+ }
665
+ }
666
+ function composeRefs(...refs) {
667
+ return (node) => {
668
+ let hasCleanup = false;
669
+ const cleanups = refs.map((ref) => {
670
+ const cleanup = setRef(ref, node);
671
+ if (!hasCleanup && typeof cleanup == "function") {
672
+ hasCleanup = true;
673
+ }
674
+ return cleanup;
675
+ });
676
+ if (hasCleanup) {
677
+ return () => {
678
+ for (let i = 0; i < cleanups.length; i++) {
679
+ const cleanup = cleanups[i];
680
+ if (typeof cleanup == "function") {
681
+ cleanup();
682
+ } else {
683
+ setRef(refs[i], null);
684
+ }
685
+ }
686
+ };
687
+ }
688
+ };
689
+ }
690
+ // @__NO_SIDE_EFFECTS__
691
+ function createSlot(ownerName) {
692
+ const SlotClone = /* @__PURE__ */ createSlotClone(ownerName);
693
+ const Slot2 = React2.forwardRef((props, forwardedRef) => {
694
+ const { children, ...slotProps } = props;
695
+ const childrenArray = React2.Children.toArray(children);
696
+ const slottable = childrenArray.find(isSlottable);
697
+ if (slottable) {
698
+ const newElement = slottable.props.children;
699
+ const newChildren = childrenArray.map((child) => {
700
+ if (child === slottable) {
701
+ if (React2.Children.count(newElement) > 1) return React2.Children.only(null);
702
+ return React2.isValidElement(newElement) ? newElement.props.children : null;
703
+ } else {
704
+ return child;
705
+ }
706
+ });
707
+ return /* @__PURE__ */ jsx(SlotClone, { ...slotProps, ref: forwardedRef, children: React2.isValidElement(newElement) ? React2.cloneElement(newElement, void 0, newChildren) : null });
708
+ }
709
+ return /* @__PURE__ */ jsx(SlotClone, { ...slotProps, ref: forwardedRef, children });
710
+ });
711
+ Slot2.displayName = `${ownerName}.Slot`;
712
+ return Slot2;
713
+ }
714
+ var Slot = /* @__PURE__ */ createSlot("Slot");
715
+ // @__NO_SIDE_EFFECTS__
716
+ function createSlotClone(ownerName) {
717
+ const SlotClone = React2.forwardRef((props, forwardedRef) => {
718
+ const { children, ...slotProps } = props;
719
+ if (React2.isValidElement(children)) {
720
+ const childrenRef = getElementRef(children);
721
+ const props2 = mergeProps(slotProps, children.props);
722
+ if (children.type !== React2.Fragment) {
723
+ props2.ref = forwardedRef ? composeRefs(forwardedRef, childrenRef) : childrenRef;
724
+ }
725
+ return React2.cloneElement(children, props2);
726
+ }
727
+ return React2.Children.count(children) > 1 ? React2.Children.only(null) : null;
728
+ });
729
+ SlotClone.displayName = `${ownerName}.SlotClone`;
730
+ return SlotClone;
731
+ }
732
+ var SLOTTABLE_IDENTIFIER = /* @__PURE__ */ Symbol("radix.slottable");
733
+ function isSlottable(child) {
734
+ return React2.isValidElement(child) && typeof child.type === "function" && "__radixId" in child.type && child.type.__radixId === SLOTTABLE_IDENTIFIER;
735
+ }
736
+ function mergeProps(slotProps, childProps) {
737
+ const overrideProps = { ...childProps };
738
+ for (const propName in childProps) {
739
+ const slotPropValue = slotProps[propName];
740
+ const childPropValue = childProps[propName];
741
+ const isHandler = /^on[A-Z]/.test(propName);
742
+ if (isHandler) {
743
+ if (slotPropValue && childPropValue) {
744
+ overrideProps[propName] = (...args) => {
745
+ const result = childPropValue(...args);
746
+ slotPropValue(...args);
747
+ return result;
748
+ };
749
+ } else if (slotPropValue) {
750
+ overrideProps[propName] = slotPropValue;
751
+ }
752
+ } else if (propName === "style") {
753
+ overrideProps[propName] = { ...slotPropValue, ...childPropValue };
754
+ } else if (propName === "className") {
755
+ overrideProps[propName] = [slotPropValue, childPropValue].filter(Boolean).join(" ");
756
+ }
757
+ }
758
+ return { ...slotProps, ...overrideProps };
759
+ }
760
+ function getElementRef(element) {
761
+ let getter = Object.getOwnPropertyDescriptor(element.props, "ref")?.get;
762
+ let mayWarn = getter && "isReactWarning" in getter && getter.isReactWarning;
763
+ if (mayWarn) {
764
+ return element.ref;
765
+ }
766
+ getter = Object.getOwnPropertyDescriptor(element, "ref")?.get;
767
+ mayWarn = getter && "isReactWarning" in getter && getter.isReactWarning;
768
+ if (mayWarn) {
769
+ return element.props.ref;
770
+ }
771
+ return element.props.ref || element.ref;
772
+ }
654
773
  var buttonVariants = cva(
655
774
  "inline-flex items-center justify-center gap-2 whitespace-nowrap rounded-md text-sm font-medium transition-all disabled:pointer-events-none disabled:opacity-50 [&_svg]:pointer-events-none [&_svg:not([class*='size-'])]:size-4 shrink-0 [&_svg]:shrink-0 outline-none focus-visible:border-ring focus-visible:ring-ring/50 focus-visible:ring-[3px] aria-invalid:ring-destructive/20 dark:aria-invalid:ring-destructive/40 aria-invalid:border-destructive",
656
775
  {
@@ -882,8 +1001,41 @@ function Checkbox2({
882
1001
  }
883
1002
  );
884
1003
  }
885
- function Toolbar() {
886
- 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();
887
1039
  const handleFilterChange = (filterId, value) => {
888
1040
  setActiveFilters((prev) => ({
889
1041
  ...prev,
@@ -900,115 +1052,116 @@ function Toolbar() {
900
1052
  return { ...prev, [filterId]: newValues };
901
1053
  });
902
1054
  };
903
- return /* @__PURE__ */ jsxs("div", { className: "flex flex-col sm:flex-row sm:items-center sm:justify-between gap-4", children: [
904
- /* @__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(
905
1057
  Input5,
906
1058
  {
907
1059
  placeholder: "Search...",
908
1060
  value: table.getState().globalFilter ?? "",
909
1061
  onChange: (e) => table.setGlobalFilter(e.target.value),
910
- className: "w-full"
1062
+ className: "w-full max-w-sm"
911
1063
  }
912
1064
  ) }),
913
- /* @__PURE__ */ jsxs("div", { className: "flex items-center gap-2 flex-wrap", children: [
914
- filters.length > 0 && /* @__PURE__ */ jsxs(Popover, { children: [
915
- /* @__PURE__ */ jsx(PopoverTrigger, { asChild: true, children: /* @__PURE__ */ jsxs(Button6, { variant: "outline", size: "sm", children: [
916
- /* @__PURE__ */ jsx(SlidersHorizontal, { className: "h-4 w-4 mr-2" }),
917
- "Filters"
918
- ] }) }),
919
- /* @__PURE__ */ jsx(PopoverContent, { className: "w-80", children: /* @__PURE__ */ jsxs("div", { className: "space-y-4", children: [
920
- /* @__PURE__ */ jsx("h4", { className: "font-medium text-sm", children: "Filters" }),
921
- filters.map((filter) => /* @__PURE__ */ jsxs("div", { className: "space-y-2", children: [
922
- /* @__PURE__ */ jsx("label", { className: "text-sm font-medium", children: filter.label }),
923
- 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: [
924
- /* @__PURE__ */ jsx(
925
- Checkbox2,
926
- {
927
- id: `${filter.id}-${option.value}`,
928
- checked: Array.isArray(activeFilters[filter.id]) && activeFilters[filter.id].includes(option.value),
929
- onCheckedChange: () => handleMultiFilterToggle(filter.id, option.value)
930
- }
931
- ),
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" }),
932
1101
  /* @__PURE__ */ jsx(
933
- "label",
1102
+ "div",
934
1103
  {
935
- htmlFor: `${filter.id}-${option.value}`,
936
- className: "text-sm cursor-pointer",
937
- 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"
938
1107
  }
939
1108
  )
940
- ] }, option.value)) }) : /* @__PURE__ */ jsxs(
941
- Select2,
942
- {
943
- value: activeFilters[filter.id] || "all",
944
- onValueChange: (value) => handleFilterChange(filter.id, value),
945
- children: [
946
- /* @__PURE__ */ jsx(SelectTrigger, { children: /* @__PURE__ */ jsx(SelectValue2, { placeholder: filter.placeholder || "Select..." }) }),
947
- /* @__PURE__ */ jsxs(SelectContent, { children: [
948
- /* @__PURE__ */ jsx(SelectItem2, { value: "all", children: "All" }),
949
- filter.options.map((option) => /* @__PURE__ */ jsx(SelectItem2, { value: option.value, children: option.label }, option.value))
950
- ] })
951
- ]
952
- }
953
- )
954
- ] }, filter.id))
955
- ] }) })
956
- ] }),
957
- /* @__PURE__ */ jsxs(Popover, { children: [
958
- /* @__PURE__ */ jsx(PopoverTrigger, { asChild: true, children: /* @__PURE__ */ jsxs(Button6, { variant: "outline", size: "sm", children: [
959
- /* @__PURE__ */ jsx(Eye, { className: "h-4 w-4 mr-2" }),
960
- "Columns"
961
- ] }) }),
962
- /* @__PURE__ */ jsx(PopoverContent, { className: "w-56", children: /* @__PURE__ */ jsxs("div", { className: "space-y-2", children: [
963
- /* @__PURE__ */ jsx("h4", { className: "font-medium text-sm", children: "Toggle Columns" }),
964
- table.getAllColumns().filter((column) => column.getCanHide()).map((column) => /* @__PURE__ */ jsxs("div", { className: "flex items-center space-x-2", children: [
965
- /* @__PURE__ */ jsx(
966
- Checkbox2,
967
- {
968
- id: column.id,
969
- checked: column.getIsVisible(),
970
- onCheckedChange: (value) => column.toggleVisibility(!!value)
971
- }
972
- ),
973
- /* @__PURE__ */ jsx("label", { htmlFor: column.id, className: "text-sm cursor-pointer", children: column.id })
974
- ] }, column.id))
975
- ] }) })
976
- ] }),
977
- /* @__PURE__ */ jsxs("div", { className: "flex items-center border rounded-md", children: [
978
- /* @__PURE__ */ jsx(
979
- Button6,
980
- {
981
- variant: viewMode === "table" ? "default" : "ghost",
982
- size: "sm",
983
- onClick: () => setViewMode("table"),
984
- className: "rounded-r-none",
985
- children: /* @__PURE__ */ jsx(Table$1, { className: "h-4 w-4" })
986
- }
987
- ),
988
- /* @__PURE__ */ jsx(
989
- Button6,
990
- {
991
- variant: viewMode === "grid" ? "default" : "ghost",
992
- size: "sm",
993
- onClick: () => setViewMode("grid"),
994
- className: "rounded-l-none",
995
- children: /* @__PURE__ */ jsx(LayoutGrid, { className: "h-4 w-4" })
996
- }
997
- )
998
- ] }),
999
- /* @__PURE__ */ jsxs("div", { className: "flex items-center gap-2", children: [
1000
- /* @__PURE__ */ jsx("label", { className: "text-sm text-muted-foreground", children: "Rows" }),
1001
- /* @__PURE__ */ jsxs(
1109
+ ] })
1110
+ ] }) })
1111
+ ] }) : /* @__PURE__ */ jsxs(
1002
1112
  Select2,
1003
1113
  {
1004
- value: String(table.getState().pagination.pageSize),
1005
- onValueChange: (value) => table.setPageSize(Number(value)),
1114
+ value: activeFilters[filter.id] || "all",
1115
+ onValueChange: (value) => handleFilterChange(filter.id, value),
1006
1116
  children: [
1007
- /* @__PURE__ */ jsx(SelectTrigger, { className: "w-20", children: /* @__PURE__ */ jsx(SelectValue2, {}) }),
1008
- /* @__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
+ ] })
1009
1125
  ]
1010
1126
  }
1011
- )
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
+ ] })
1012
1165
  ] })
1013
1166
  ] })
1014
1167
  ] });
@@ -1206,7 +1359,8 @@ var DataTable = Object.assign(root_default2, {
1206
1359
  Content: content_default,
1207
1360
  TableView: table_view_default,
1208
1361
  GridView: grid_view_default,
1209
- Pagination: pagination_default
1362
+ Pagination: pagination_default,
1363
+ ViewOptions: view_options_default
1210
1364
  });
1211
1365
  var data_table_default = DataTable;
1212
1366