Skip to content

Commit

Permalink
cell onCreate, props as second param
Browse files Browse the repository at this point in the history
  • Loading branch information
mbasso committed Jul 7, 2017
1 parent 958e7ef commit 485337b
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 10 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-tablify",
"version": "0.0.9",
"version": "0.0.10",
"description": "A simple, lightweight and highly extensible datagrid for React",
"main": "lib/index.js",
"jsnext:main": "es/index.js",
Expand Down
2 changes: 1 addition & 1 deletion src/cells/Cell.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export default class Cell extends React.Component {
} else if (typeof field === 'number') {
newData = data[field];
}
if (typeof onCreate === 'function') newData = onCreate(newData);
if (typeof onCreate === 'function') newData = onCreate(newData, this.props);
const Component = component;
return (
<Component {...others}>
Expand Down
2 changes: 1 addition & 1 deletion src/cells/LookupCell.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export default class LookupCell extends React.Component {
value = result[0][displayField];
}
if (typeof onCreate === 'function') {
value = onCreate(value);
value = onCreate(value, this.props);
}
return value;
}}
Expand Down
33 changes: 26 additions & 7 deletions test/cells.spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// @flow

import React from 'react';
import expect from 'expect';
import { expectComponentToMatch } from './utils';
import { Cell, LookupCell } from '../src/';

Expand Down Expand Up @@ -41,7 +42,18 @@ describe('cells', () => {

it('should modify data in onCreate', () => {
expectComponentToMatch(
<Cell onCreate={x => x.toUpperCase()} data="foo" />,
<Cell
data="foo"
foo="bar"
onCreate={(x, props) => {
expect(x).toEqual('foo');
expect(props).toMatch({
data: 'foo',
foo: 'bar',
});
return x.toUpperCase();
}}
/>,
<td>FOO</td>,
);
});
Expand Down Expand Up @@ -80,14 +92,21 @@ describe('cells', () => {
});

it('should handle onCreate', () => {
const props = {
data: orders[0],
field: 'customerCode',
displayField: 'description',
valueField: 'code',
dataSource: customers,
};
expectComponentToMatch(
<LookupCell
data={orders[0]}
field="customerCode"
displayField="description"
valueField="code"
dataSource={customers}
onCreate={description => description.toUpperCase()}
{...props}
onCreate={(description, lookupProps) => {
expect(description).toEqual('foo1');
expect(lookupProps).toMatch(props);
return description.toUpperCase();
}}
/>,
<td>FOO1</td>,
);
Expand Down

0 comments on commit 485337b

Please sign in to comment.