Skip to content

Commit

Permalink
added ability to hide keys
Browse files Browse the repository at this point in the history
  • Loading branch information
zackify committed Sep 24, 2015
1 parent 2e3d3df commit eba464c
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 14 deletions.
17 changes: 15 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ Optionally, use `modifyAll` to change every item.

~~~js

modifyId(id, row){
modifyId({hidden, value, key, row}){
//row is the current row object
return <a href={id}>{id}</a>
return <a href={value}>{value}</a>
}

render() {
Expand All @@ -67,6 +67,19 @@ modifyAll={this.modify}
/>
~~~

###hide

```js
let modify = ({hidden}) => {
expect(hidden.id).to.be.equal(1)
}
Test(<Table rows={[{id: 1, name: 'zach'}]} hide={['id']}/>)
.find('td')
.element(td => {
expect(td.props.children).to.be.equal('zach');
})
```

`capitalize` Optionally, turn off capitalization of header row. True by default.

~~~js
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-legit-table",
"version": "0.3.0",
"version": "0.4.0",
"description": "the simplest table component out there",
"main": "lib/table.js",
"scripts": {
Expand Down Expand Up @@ -36,7 +36,7 @@
"eslint": "^0.24.1",
"eslint-plugin-react": "^2.7.0",
"expect": "^1.8.0",
"legit-tests": "^0.4.1",
"legit-tests": "^0.4.4",
"mocha": "^2.2.5",
"mocha-babel": "^3.0.0",
"react-hot-loader": "^1.3.0",
Expand Down
5 changes: 5 additions & 0 deletions src/head.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import uniqueId from './uniqueId'
export default class Head extends React.Component{
static propTypes = {
row: React.PropTypes.object.isRequired,
hide: React.PropTypes.array,
capitalize: React.PropTypes.bool
}

Expand All @@ -13,6 +14,10 @@ export default class Head extends React.Component{

headings() {
return Object.keys(this.props.row).map((name) => {
if(this.props.hide){
let result = this.props.hide.map(ignore => ignore === name)
if(result) return true
}
return <th key={uniqueId(name)}>{this.props.capitalize ? this.titleize(name) : name}</th>;
});
}
Expand Down
21 changes: 18 additions & 3 deletions src/rows.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,35 @@ export default class Rows extends React.Component{
static propTypes = {
rows: React.PropTypes.array,
modify: React.PropTypes.object,
hide: React.PropTypes.array,
modifyAll: React.PropTypes.func
}

rows(){
let rows = [];

for(let row of this.props.rows){
let rowList = [];
let rowList = [],
hidden = {}
let id = row[Object.keys(row)[0]]

if(this.props.hide){
this.props.hide.map(ignore => {
hidden[ignore] = row[ignore]
delete row[ignore]
})
}

for(let item in row){
let value = row[item]
if(this.props.modifyAll) value = this.props.modifyAll(row[item], item, row)
else if(this.props.modify[item]) value = this.props.modify[item](row[item], item, row)
let params = {
value: row[item],
key: item,
row,
hidden
}
if(this.props.modifyAll) value = this.props.modifyAll(params)
else if(this.props.modify[item]) value = this.props.modify[item](params)

rowList.push(<td key={uniqueId(row[item])}>{value}</td>)
}
Expand Down
2 changes: 1 addition & 1 deletion src/table.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export default class Table extends React.Component{

return (
<table {...attributes}>
<Head row={rows[0]} capitalize={capitalize} />
<Head row={rows[0]} capitalize={capitalize} {...this.props} />
<Rows rows={rows} modify={modify || {}} {...this.props}/>
</table>
)
Expand Down
23 changes: 17 additions & 6 deletions tests/table.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ describe('Table component', () => {
});

it('should correctly modify row', () => {
let modify = (name) => {
return `Name: ${name}`
let modify = ({value}) => {
return `Name: ${value}`
}

Test(<Table rows={[{name: 'zach'}]} modify={{name: modify}}/>)
Expand All @@ -45,7 +45,7 @@ describe('Table component', () => {
})

it('should correctly modify all rows', () => {
let modify = (value) => {
let modify = ({value}) => {
return `yo ${value}`
}

Expand All @@ -58,11 +58,11 @@ describe('Table component', () => {
})

it('should modify all rows and ignore individual modify ', () => {
let modify = (value) => {
let modify = ({value}) => {
return `yo ${value}`
}
let modifyName = (value) => {

let modifyName = ({value}) => {
return `Name: ${value}`
}

Expand All @@ -74,5 +74,16 @@ describe('Table component', () => {
})
})

it('should hide id row', () => {
let modify = ({hidden}) => {
expect(hidden.id).to.be.equal(1)
}
Test(<Table rows={[{id: 1, name: 'zach'}]} hide={['id']}/>)
.find('td')
.element(td => {
expect(td.props.children).to.be.equal('zach');
})
});

});
});

0 comments on commit eba464c

Please sign in to comment.