Skip to content

Commit f3e178f

Browse files
MariannaMilovanovajorrit
authored andcommitted
feature: add new bool property withClasses that add classes to rows (#6)
feature: add new bool property withClasses that adds classes to rows and cells
1 parent f7e0ec2 commit f3e178f

File tree

4 files changed

+40
-3
lines changed

4 files changed

+40
-3
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ Properties marked with an asterisk (`*`) are required.
126126
| | `object` | CSS styles to apply to the parent table tag. |
127127
| | `function` | Function returning one of the above. The function receives a state object with boolean property narrow indicating if the current presentation is narrow or wide. |
128128
| `initialNarrow` | `bool` | Initially render the table in narrow mode when rendering serverside. Set to true when you expect the browser to be narrow to prevent rerendering client side. |
129+
| `withClasses` | `bool` | Add unique class names to each row and header cell, respectively row-KEY and header-KEY |
129130

130131

131132
## name

demo/src/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ const Demo = () => (
5353
rows={rows}
5454
keyGetter={keyGetter}
5555
breakpoint={578}
56+
withClasses
5657
tableStyling={({ narrow }) => (narrow ? 'narrowtable' : 'widetable')}
5758
/>
5859
</div>);

src/index.js

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,14 @@ const getClassNameOrStyleProps = (classNameOrStyle, componentState) => {
2626
return {};
2727
};
2828

29+
function headerClass(withClasses, key) {
30+
return withClasses ? { className: `header-${key}` } : {};
31+
}
32+
33+
function rowClass(withClasses, key) {
34+
return withClasses ? { className: `row-${key}` } : {};
35+
}
36+
2937
const inBrowser = typeof window !== 'undefined';
3038
const matchMedia = inBrowser && window.matchMedia !== null;
3139

@@ -44,10 +52,12 @@ class HyperResponsiveTable extends Component {
4452
PropTypes.func,
4553
]),
4654
initialNarrow: PropTypes.bool,
55+
withClasses: PropTypes.bool,
4756
};
4857

4958
static defaultProps = {
5059
initialNarrow: false,
60+
withClasses: false,
5161
tableStyling: null,
5262
};
5363

@@ -113,6 +123,7 @@ class HyperResponsiveTable extends Component {
113123
headers,
114124
rows,
115125
keyGetter,
126+
withClasses,
116127
} = this.props;
117128
const { narrow } = this.state;
118129

@@ -123,7 +134,7 @@ class HyperResponsiveTable extends Component {
123134
<table {...getClassNameOrStyleProps(tableStyling, this.state)}>
124135
{rows.map(row => (
125136
<tbody key={keyGetter(row)}>
126-
{dataKeys.map(key => <tr key={key}><th scope="row">{headers[key]}</th><td>{row[key]}</td></tr>)}
137+
{dataKeys.map(key => <tr key={key} {...rowClass(withClasses, keyGetter(row))}><th {...headerClass(withClasses, key)} scope="row">{headers[key]}</th><td>{row[key]}</td></tr>)}
127138
</tbody>))
128139
}
129140
</table>);
@@ -133,12 +144,12 @@ class HyperResponsiveTable extends Component {
133144
<table {...getClassNameOrStyleProps(tableStyling, this.state)}>
134145
<thead>
135146
<tr>
136-
{ dataKeys.map(key => <th key={key} scope="col">{headers[key]}</th>) }
147+
{dataKeys.map(key => <th key={key} {...headerClass(withClasses, key)} scope="col">{headers[key]}</th>)}
137148
</tr>
138149
</thead>
139150
<tbody>
140151
{rows.map(row => (
141-
<tr key={keyGetter(row)}>
152+
<tr key={keyGetter(row)} {...rowClass(withClasses, keyGetter(row))}>
142153
{dataKeys.map(key => <td key={key}>{row[key]}</td>)}
143154
</tr>))}
144155
</tbody>

tests/index-test.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,4 +169,28 @@ describe('Component', () => {
169169
expect(table.getAttribute('style')).toEqual(null);
170170
});
171171
});
172+
173+
it('add classes to headers and rows if pass the property `withClasses`', () => {
174+
const breakpoint = 0;
175+
const tableStyling = 1234;
176+
const withClasses = true;
177+
178+
const props = {
179+
headers,
180+
rows,
181+
keyGetter,
182+
breakpoint,
183+
tableStyling,
184+
withClasses,
185+
};
186+
187+
render(<Component {...props} />, node, () => {
188+
const th = node.querySelectorAll('th');
189+
const tbody = node.querySelectorAll('tbody');
190+
const tr = tbody[0].querySelectorAll('tr');
191+
192+
expect(th[0].getAttribute('class')).toEqual('header-a');
193+
expect(tr[0].getAttribute('class')).toEqual('row-A 1');
194+
});
195+
});
172196
});

0 commit comments

Comments
 (0)