Skip to content

Commit

Permalink
Merge f89acc3 into f7e0ec2
Browse files Browse the repository at this point in the history
  • Loading branch information
MariannaMilovanova committed Aug 13, 2018
2 parents f7e0ec2 + f89acc3 commit 4e2ec23
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 3 deletions.
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -126,6 +126,7 @@ Properties marked with an asterisk (`*`) are required.
| | `object` | CSS styles to apply to the parent table tag. |
| | `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. |
| `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. |
| `withClasses` | `bool` | Add unique class names to each row and header cell, respectively row-KEY and header-KEY |


## name
Expand Down
1 change: 1 addition & 0 deletions demo/src/index.js
Expand Up @@ -53,6 +53,7 @@ const Demo = () => (
rows={rows}
keyGetter={keyGetter}
breakpoint={578}
withClasses
tableStyling={({ narrow }) => (narrow ? 'narrowtable' : 'widetable')}
/>
</div>);
Expand Down
17 changes: 14 additions & 3 deletions src/index.js
Expand Up @@ -26,6 +26,14 @@ const getClassNameOrStyleProps = (classNameOrStyle, componentState) => {
return {};
};

function headerClass(withClasses, key) {
return withClasses ? { className: `header-${key}` } : {};
}

function rowClass(withClasses, key) {
return withClasses ? { className: `row-${key}` } : {};
}

const inBrowser = typeof window !== 'undefined';
const matchMedia = inBrowser && window.matchMedia !== null;

Expand All @@ -44,10 +52,12 @@ class HyperResponsiveTable extends Component {
PropTypes.func,
]),
initialNarrow: PropTypes.bool,
withClasses: PropTypes.bool,
};

static defaultProps = {
initialNarrow: false,
withClasses: false,
tableStyling: null,
};

Expand Down Expand Up @@ -113,6 +123,7 @@ class HyperResponsiveTable extends Component {
headers,
rows,
keyGetter,
withClasses,
} = this.props;
const { narrow } = this.state;

Expand All @@ -123,7 +134,7 @@ class HyperResponsiveTable extends Component {
<table {...getClassNameOrStyleProps(tableStyling, this.state)}>
{rows.map(row => (
<tbody key={keyGetter(row)}>
{dataKeys.map(key => <tr key={key}><th scope="row">{headers[key]}</th><td>{row[key]}</td></tr>)}
{dataKeys.map(key => <tr key={key} {...rowClass(withClasses, keyGetter(row))}><th {...headerClass(withClasses, key)} scope="row">{headers[key]}</th><td>{row[key]}</td></tr>)}
</tbody>))
}
</table>);
Expand All @@ -133,12 +144,12 @@ class HyperResponsiveTable extends Component {
<table {...getClassNameOrStyleProps(tableStyling, this.state)}>
<thead>
<tr>
{ dataKeys.map(key => <th key={key} scope="col">{headers[key]}</th>) }
{dataKeys.map(key => <th key={key} {...headerClass(withClasses, key)} scope="col">{headers[key]}</th>)}
</tr>
</thead>
<tbody>
{rows.map(row => (
<tr key={keyGetter(row)}>
<tr key={keyGetter(row)} {...rowClass(withClasses, keyGetter(row))}>
{dataKeys.map(key => <td key={key}>{row[key]}</td>)}
</tr>))}
</tbody>
Expand Down
24 changes: 24 additions & 0 deletions tests/index-test.js
Expand Up @@ -169,4 +169,28 @@ describe('Component', () => {
expect(table.getAttribute('style')).toEqual(null);
});
});

it('add classes to headers and rows if pass the property `withClasses`', () => {
const breakpoint = 0;
const tableStyling = 1234;
const withClasses = true;

const props = {
headers,
rows,
keyGetter,
breakpoint,
tableStyling,
withClasses,
};

render(<Component {...props} />, node, () => {
const th = node.querySelectorAll('th');
const tbody = node.querySelectorAll('tbody');
const tr = tbody[0].querySelectorAll('tr');

expect(th[0].getAttribute('class')).toEqual('header-a');
expect(tr[0].getAttribute('class')).toEqual('row-A 1');
});
});
});

0 comments on commit 4e2ec23

Please sign in to comment.