@yusufalperendumlu/component-library 0.0.17 → 0.0.19

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.d.ts CHANGED
@@ -65,20 +65,21 @@ type InputProps = React.InputHTMLAttributes<HTMLInputElement> & {
65
65
 
66
66
  declare const Input: React$1.FC<InputProps>;
67
67
 
68
- interface TableColumn<T> {
69
- key: keyof T;
70
- label: string;
71
- sortable?: boolean;
72
- render?: (value: any, row: T) => React.ReactNode;
73
- }
74
68
  interface TableProps<T> {
75
- columns: TableColumn<T>[];
69
+ columns: {
70
+ key: keyof T;
71
+ label: string;
72
+ sortable: boolean;
73
+ render?: (value: any, row: T) => React.ReactNode;
74
+ }[];
76
75
  data: T[];
77
76
  onRowClick?: (row: T) => void;
78
77
  className?: string;
78
+ selectedRowId?: string | number | null;
79
+ rowIdKey?: keyof T;
79
80
  }
80
81
 
81
- declare function Table<T extends Record<string, any>>({ columns, data, onRowClick, className, }: TableProps<T>): react_jsx_runtime.JSX.Element;
82
+ declare function Table<T extends Record<string, any>>({ columns, data, onRowClick, className, selectedRowId, rowIdKey, }: TableProps<T>): react_jsx_runtime.JSX.Element;
82
83
 
83
84
  type ToastProps = {
84
85
  title: string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@yusufalperendumlu/component-library",
3
- "version": "0.0.17",
3
+ "version": "0.0.19",
4
4
  "main": "dist/cjs/index.js",
5
5
  "module": "dist/esm/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -1,5 +1,5 @@
1
1
  import clsx from 'clsx';
2
- import React, { useState } from 'react';
2
+ import React, { useState, useMemo } from 'react';
3
3
  import { FaArrowDown, FaArrowUp } from 'react-icons/fa';
4
4
 
5
5
  import { TableProps } from './Table.types';
@@ -9,12 +9,14 @@ function Table<T extends Record<string, any>>({
9
9
  data,
10
10
  onRowClick,
11
11
  className,
12
+ selectedRowId,
13
+ rowIdKey = 'id',
12
14
  }: TableProps<T>) {
13
15
  const [sortKey, setSortKey] = useState<keyof T | null>(null);
14
16
  const [sortOrder, setSortOrder] = useState<'asc' | 'desc'>('asc');
15
17
  const [selectedRowIndex, setSelectedRowIndex] = useState<number | null>(null);
16
18
 
17
- const sortedData = React.useMemo(() => {
19
+ const sortedData = useMemo(() => {
18
20
  if (!sortKey) return data;
19
21
 
20
22
  const sorted = [...data].sort((a, b) => {
@@ -40,7 +42,7 @@ function Table<T extends Record<string, any>>({
40
42
  <div className={clsx('border-none h-full flex flex-col', className)}>
41
43
  <div className='h-full overflow-y-auto custom-scroll bg-[#171215]'>
42
44
  <table className='table-fixed min-w-full text-left text-sm text-zinc-100'>
43
- <thead className='border-b-2 border-[#5876EE] text-[#5876EE] sticky top-0 z-10'>
45
+ <thead className='bg-[#171215] border-b-2 border-[#5876EE] text-[#5876EE] sticky top-0 z-10'>
44
46
  <tr>
45
47
  {columns.map(({ key, label, sortable }) => (
46
48
  <th
@@ -66,32 +68,39 @@ function Table<T extends Record<string, any>>({
66
68
  </tr>
67
69
  </thead>
68
70
  <tbody>
69
- {sortedData.map((row, idx) => (
70
- <tr
71
- key={idx}
72
- onClick={() => {
73
- setSelectedRowIndex(idx);
74
- onRowClick?.(row);
75
- }}
76
- className={clsx(
77
- selectedRowIndex === idx
78
- ? 'bg-[#BDC311]'
79
- : idx % 2 === 0
80
- ? 'bg-[#1D1B1D]'
81
- : 'bg-[#424242]',
82
- 'cursor-pointer'
83
- )}
84
- >
85
- {columns.map(({ key, render }) => (
86
- <td
87
- key={key as string}
88
- className='whitespace-nowrap px-6 py-4'
89
- >
90
- {render ? render(row[key], row) : row[key]}
91
- </td>
92
- ))}
93
- </tr>
94
- ))}
71
+ {sortedData.map((row, idx) => {
72
+ const isSelected =
73
+ selectedRowId !== undefined &&
74
+ selectedRowId !== null &&
75
+ row[rowIdKey] === selectedRowId;
76
+
77
+ return (
78
+ <tr
79
+ key={idx}
80
+ onClick={() => {
81
+ setSelectedRowIndex(idx);
82
+ onRowClick?.(row);
83
+ }}
84
+ className={clsx(
85
+ isSelected
86
+ ? 'bg-[#BDC311]'
87
+ : idx % 2 === 0
88
+ ? 'bg-[#1D1B1D]'
89
+ : 'bg-[#424242]',
90
+ 'cursor-pointer'
91
+ )}
92
+ >
93
+ {columns.map(({ key, render }) => (
94
+ <td
95
+ key={key as string}
96
+ className='whitespace-nowrap px-6 py-4'
97
+ >
98
+ {render ? render(row[key], row) : row[key]}
99
+ </td>
100
+ ))}
101
+ </tr>
102
+ );
103
+ })}
95
104
  </tbody>
96
105
  </table>
97
106
  </div>
@@ -6,8 +6,15 @@ export interface TableColumn<T> {
6
6
  }
7
7
 
8
8
  export interface TableProps<T> {
9
- columns: TableColumn<T>[];
9
+ columns: {
10
+ key: keyof T;
11
+ label: string;
12
+ sortable: boolean;
13
+ render?: (value: any, row: T) => React.ReactNode;
14
+ }[];
10
15
  data: T[];
11
16
  onRowClick?: (row: T) => void;
12
17
  className?: string;
18
+ selectedRowId?: string | number | null;
19
+ rowIdKey?: keyof T;
13
20
  }