jattac.libs.web.responsive-table 0.0.24 → 0.0.26

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/README.md CHANGED
@@ -1,79 +1,196 @@
1
1
  # ResponsiveTable
2
2
 
3
- ResponsiveTable is a reusable React component that displays tabular data in a responsive layout.
3
+ ResponsiveTable is a React component designed to create responsive tables that adapt seamlessly to both desktop and mobile screens. It simplifies the process of building tables with dynamic layouts and custom behaviors.
4
4
 
5
- ## Features
5
+ ## Installation
6
6
 
7
- - Handles mobile and desktop layouts
8
- - Customizable columns
9
- - Dynamic column definitions
10
- - Card-style mobile view
11
- - Generic types for flexible data
7
+ To install ResponsiveTable, use npm:
12
8
 
13
- ## Usage
9
+ ```bash
10
+ npm install jattac.libs.web.responsive-table
11
+ ```
12
+
13
+ ## Why Use ResponsiveTable?
14
+
15
+ - **Mobile-Friendly**: Automatically adjusts to look great on small screens.
16
+ - **Customizable**: Define how each column looks and behaves.
17
+ - **Dynamic**: Supports flexible column setups, even changing them based on the data.
18
+ - **Easy to Use**: Simple API for quick integration into your React project.
19
+
20
+ ## How to Use It
21
+
22
+ Here’s a quick example:
14
23
 
15
24
  ```jsx
16
- <ResponsiveTable columnDefinitions={columns} data={data} />
25
+ import ResponsiveTable from 'jattac.libs.web.responsive-table';
26
+
27
+ const columns = [
28
+ {
29
+ displayLabel: 'Name',
30
+ dataKey: 'name',
31
+ cellRenderer: (row) => row.name,
32
+ },
33
+ {
34
+ displayLabel: 'Age',
35
+ dataKey: 'age',
36
+ cellRenderer: (row) => row.age,
37
+ },
38
+ ];
39
+
40
+ const data = [
41
+ { name: 'Alice', age: 25 },
42
+ { name: 'Bob', age: 30 },
43
+ ];
44
+
45
+ <ResponsiveTable columnDefinitions={columns} data={data} />;
17
46
  ```
18
47
 
19
- - 'columnDefinitions' defines an array of columns, which can be a simple configuration object or dynamic function
20
- - 'data' is an array of data objects to display in rows
48
+ ### Explanation
21
49
 
22
- The component handles switching layout based on screen width to optimize for desktop and mobile.
50
+ - **`columnDefinitions`**: Defines the columns of the table. Each column has a label (`displayLabel`), a key to match the data (`dataKey`), and a function to render the cell (`cellRenderer`).
51
+ - **`data`**: The rows of the table, where each object represents a row.
23
52
 
24
- ## Custom Columns
53
+ ## Key Features
25
54
 
26
- Columns can be configured using the 'IResponsiveTableColumnDefinition' interface.
55
+ ### 1. Mobile and Desktop Layouts
27
56
 
28
- Some key configuration options:
57
+ - On small screens, the table switches to a card-style layout for better readability.
29
58
 
30
- - 'displayLabel': Header label
31
- - 'dataKey': Maps column to data property
32
- - 'cellRenderer': Renders cell value
59
+ ### 2. Custom Columns
33
60
 
34
- See docs for more details on customization.
61
+ - Customize how each column looks and behaves. For example, you can add clickable headers or custom styles.
35
62
 
36
- ## Dynamic Columns
63
+ ### 3. Dynamic Columns
37
64
 
38
- Column definitions can also be a function allowing dynamic configurations per row.
65
+ - Columns can be defined dynamically based on the data or external conditions. This allows you to create tables that adapt to different datasets or user preferences.
39
66
 
40
- ## Props
67
+ #### Example 1: Conditional Columns
41
68
 
42
- Prop definitions provide detailed specification of component contract.
69
+ You can show or hide columns based on a condition:
43
70
 
44
- ### IProps
71
+ ```jsx
72
+ const isAdmin = true; // Example condition
73
+
74
+ const columns = [
75
+ {
76
+ displayLabel: 'Name',
77
+ dataKey: 'name',
78
+ cellRenderer: (row) => row.name,
79
+ },
80
+ isAdmin && {
81
+ displayLabel: 'Actions',
82
+ dataKey: 'actions',
83
+ cellRenderer: (row) => <button onClick={() => alert(row.name)}>Edit</button>,
84
+ },
85
+ ].filter(Boolean); // Remove undefined columns
86
+
87
+ const data = [{ name: 'Alice' }, { name: 'Bob' }];
88
+
89
+ <ResponsiveTable columnDefinitions={columns} data={data} />;
90
+ ```
45
91
 
46
- ```ts
47
- interface IProps<TData> {
48
- /** Column definitions */
49
- columnDefinitions: ColumnDefinition<TData>[];
92
+ #### Example 2: Dynamic Column Labels
50
93
 
51
- /** Table data rows */
52
- data: TData[];
94
+ You can change column labels dynamically based on user preferences or locale:
53
95
 
54
- /** Optional styling */
55
- className?: string;
96
+ ```jsx
97
+ const userLocale = 'fr'; // Example locale
98
+
99
+ const columnLabels = {
100
+ en: { name: 'Name', age: 'Age' },
101
+ fr: { name: 'Nom', age: 'Âge' },
102
+ };
103
+
104
+ const columns = [
105
+ {
106
+ displayLabel: columnLabels[userLocale].name,
107
+ dataKey: 'name',
108
+ cellRenderer: (row) => row.name,
109
+ },
110
+ {
111
+ displayLabel: columnLabels[userLocale].age,
112
+ dataKey: 'age',
113
+ cellRenderer: (row) => row.age,
114
+ },
115
+ ];
116
+
117
+ const data = [
118
+ { name: 'Alice', age: 25 },
119
+ { name: 'Bob', age: 30 },
120
+ ];
121
+
122
+ <ResponsiveTable columnDefinitions={columns} data={data} />;
123
+ ```
56
124
 
57
- /** row click handler */
58
- onRowClicked?: (row: TData) => void;
125
+ #### Example 3: Columns Based on Data
59
126
 
60
- /** not data component */
61
- noDataComponent?: ReactNode;
62
- }
63
- ```
127
+ You can generate columns dynamically based on the dataset:
64
128
 
65
- ```ts
66
- type ColumnDefinition<TData> =
67
- | IResponsiveTableColumnDefinition<TData>
68
- | ((rowData: TData, rowIndex?: number) => IResponsiveTableColumnDefinition<TData>);
129
+ ```jsx
130
+ const data = [
131
+ { name: 'Alice', age: 25, city: 'New York' },
132
+ { name: 'Bob', age: 30, city: 'Los Angeles' },
133
+ ];
134
+
135
+ const columns = Object.keys(data[0]).map((key) => ({
136
+ displayLabel: key.charAt(0).toUpperCase() + key.slice(1), // Capitalize key
137
+ dataKey: key,
138
+ cellRenderer: (row) => row[key],
139
+ }));
140
+
141
+ <ResponsiveTable columnDefinitions={columns} data={data} />;
69
142
  ```
70
143
 
71
- ```ts
72
- interface IResponsiveTableColumnDefinition<TData> {
73
- displayLabel: string | ReactNode;
144
+ #### Example 4: Adding Columns Dynamically Using a Function
74
145
 
75
- dataKey?: keyof TData;
146
+ You can define columns dynamically by using a function that takes the row data and optionally the row index. This is useful for creating columns based on specific row properties or conditions.
76
147
 
77
- cellRenderer: (rowData: TData) => ReactNode;
78
- }
148
+ ```jsx
149
+ const columns = [
150
+ {
151
+ displayLabel: 'Name',
152
+ dataKey: 'name',
153
+ cellRenderer: (row) => row.name,
154
+ },
155
+ {
156
+ displayLabel: 'Dynamic Column',
157
+ dataKey: 'dynamic',
158
+ cellRenderer: (row, rowIndex) => {
159
+ if (rowIndex % 2 === 0) {
160
+ return `Even Row: ${row.name}`;
161
+ } else {
162
+ return `Odd Row: ${row.name}`;
163
+ }
164
+ },
165
+ },
166
+ ];
167
+
168
+ const data = [
169
+ { name: 'Alice', age: 25 },
170
+ { name: 'Bob', age: 30 },
171
+ { name: 'Charlie', age: 35 },
172
+ ];
173
+
174
+ <ResponsiveTable columnDefinitions={columns} data={data} />;
79
175
  ```
176
+
177
+ ### Explanation
178
+
179
+ - **Dynamic Column Logic**: The `cellRenderer` function for the dynamic column checks the row index and displays different content for even and odd rows.
180
+ - **Flexibility**: This approach allows you to create highly customized columns based on row-specific data or conditions.
181
+
182
+ ### 4. No Data? No Problem!
183
+
184
+ - If there’s no data to display, you can show a custom message or graphic.
185
+
186
+ ## Why It’s Designed This Way
187
+
188
+ - **Flexibility**: Works for both simple and complex data tables.
189
+ - **User-Friendly**: Automatically adapts to different screen sizes.
190
+ - **Customizable**: Lets you control how your data is displayed.
191
+
192
+ ## Advantages
193
+
194
+ - Saves time: No need to build responsive tables from scratch.
195
+ - Easy to maintain: Clear and simple API.
196
+ - Works out of the box: Handles common table features like empty states and dynamic layouts.
@@ -1,6 +1,6 @@
1
1
  import React, { Component, ReactNode } from 'react';
2
2
  import IResponsiveTableColumnDefinition from '../Data/IResponsiveTableColumnDefinition';
3
- type ColumnDefinition<TData> = IResponsiveTableColumnDefinition<TData> | ((data: TData, rowIndex?: number) => IResponsiveTableColumnDefinition<TData>);
3
+ export type ColumnDefinition<TData> = IResponsiveTableColumnDefinition<TData> | ((data: TData, rowIndex?: number) => IResponsiveTableColumnDefinition<TData>);
4
4
  interface IProps<TData> {
5
5
  columnDefinitions: ColumnDefinition<TData>[];
6
6
  data: TData[];
package/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import IResponsiveTableColumnDefinition from "./Data/IResponsiveTableColumnDefinition";
2
- import ResponsiveTable from "./UI/ResponsiveTable";
3
- export { IResponsiveTableColumnDefinition };
1
+ import IResponsiveTableColumnDefinition from './Data/IResponsiveTableColumnDefinition';
2
+ import ResponsiveTable, { ColumnDefinition } from './UI/ResponsiveTable';
3
+ export { IResponsiveTableColumnDefinition, ColumnDefinition };
4
4
  export default ResponsiveTable;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "jattac.libs.web.responsive-table",
3
- "version": "0.0.24",
3
+ "version": "0.0.26",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.es.js",
@@ -2,7 +2,7 @@ import React, { CSSProperties, Component, ReactNode } from 'react';
2
2
  import styles from '../Styles/ResponsiveTable.module.css';
3
3
  import IResponsiveTableColumnDefinition from '../Data/IResponsiveTableColumnDefinition';
4
4
 
5
- type ColumnDefinition<TData> =
5
+ export type ColumnDefinition<TData> =
6
6
  | IResponsiveTableColumnDefinition<TData>
7
7
  | ((data: TData, rowIndex?: number) => IResponsiveTableColumnDefinition<TData>);
8
8
  interface IProps<TData> {
package/src/index.tsx CHANGED
@@ -1,5 +1,5 @@
1
- import IResponsiveTableColumnDefinition from "./Data/IResponsiveTableColumnDefinition";
2
- import ResponsiveTable from "./UI/ResponsiveTable";
1
+ import IResponsiveTableColumnDefinition from './Data/IResponsiveTableColumnDefinition';
2
+ import ResponsiveTable, { ColumnDefinition } from './UI/ResponsiveTable';
3
3
 
4
- export {IResponsiveTableColumnDefinition}
5
- export default ResponsiveTable
4
+ export { IResponsiveTableColumnDefinition, ColumnDefinition };
5
+ export default ResponsiveTable;