jattac.libs.web.responsive-table 0.0.24 → 0.0.25
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 +165 -48
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,79 +1,196 @@
|
|
|
1
1
|
# ResponsiveTable
|
|
2
2
|
|
|
3
|
-
ResponsiveTable is a
|
|
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
|
-
##
|
|
5
|
+
## Installation
|
|
6
6
|
|
|
7
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
20
|
-
- 'data' is an array of data objects to display in rows
|
|
48
|
+
### Explanation
|
|
21
49
|
|
|
22
|
-
|
|
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
|
-
##
|
|
53
|
+
## Key Features
|
|
25
54
|
|
|
26
|
-
|
|
55
|
+
### 1. Mobile and Desktop Layouts
|
|
27
56
|
|
|
28
|
-
|
|
57
|
+
- On small screens, the table switches to a card-style layout for better readability.
|
|
29
58
|
|
|
30
|
-
|
|
31
|
-
- 'dataKey': Maps column to data property
|
|
32
|
-
- 'cellRenderer': Renders cell value
|
|
59
|
+
### 2. Custom Columns
|
|
33
60
|
|
|
34
|
-
|
|
61
|
+
- Customize how each column looks and behaves. For example, you can add clickable headers or custom styles.
|
|
35
62
|
|
|
36
|
-
|
|
63
|
+
### 3. Dynamic Columns
|
|
37
64
|
|
|
38
|
-
|
|
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
|
-
|
|
67
|
+
#### Example 1: Conditional Columns
|
|
41
68
|
|
|
42
|
-
|
|
69
|
+
You can show or hide columns based on a condition:
|
|
43
70
|
|
|
44
|
-
|
|
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
|
-
|
|
47
|
-
interface IProps<TData> {
|
|
48
|
-
/** Column definitions */
|
|
49
|
-
columnDefinitions: ColumnDefinition<TData>[];
|
|
92
|
+
#### Example 2: Dynamic Column Labels
|
|
50
93
|
|
|
51
|
-
|
|
52
|
-
data: TData[];
|
|
94
|
+
You can change column labels dynamically based on user preferences or locale:
|
|
53
95
|
|
|
54
|
-
|
|
55
|
-
|
|
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
|
-
|
|
58
|
-
onRowClicked?: (row: TData) => void;
|
|
125
|
+
#### Example 3: Columns Based on Data
|
|
59
126
|
|
|
60
|
-
|
|
61
|
-
noDataComponent?: ReactNode;
|
|
62
|
-
}
|
|
63
|
-
```
|
|
127
|
+
You can generate columns dynamically based on the dataset:
|
|
64
128
|
|
|
65
|
-
```
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
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
|
-
|
|
72
|
-
interface IResponsiveTableColumnDefinition<TData> {
|
|
73
|
-
displayLabel: string | ReactNode;
|
|
144
|
+
#### Example 4: Adding Columns Dynamically Using a Function
|
|
74
145
|
|
|
75
|
-
|
|
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
|
-
|
|
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.
|