Skip to content
This repository was archived by the owner on Oct 24, 2019. It is now read-only.

feat: add reactive options #9

Merged
merged 4 commits into from
Aug 29, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 16 additions & 16 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,6 @@
"xhr-mock": "^2.4.1"
},
"dependencies": {
"tui-grid": "^4.3.0"
"tui-grid": "^4.4.1"
}
}
34 changes: 23 additions & 11 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ const reactivePropSetterMap = {
data: 'resetData',
columns: 'setColumns',
bodyHeight: 'setBodyHeight',
frozenColumnCount: 'setFrozenColumnCount'
frozenColumnCount: 'setFrozenColumnCount',
columnOptions: 'setFrozenColumnCount'
};

export default class Grid extends React.Component {
Expand Down Expand Up @@ -35,15 +36,15 @@ export default class Grid extends React.Component {
}

componentDidMount() {
const {
frozenColumnCount: frozenCount = 0,
columnOptions: columnOptionsProp = {}
} = this.props;
const { frozenColumnCount: frozenCount, columnOptions: columnOptionsProp = {} } = this.props;

const columnOptions = {
...columnOptionsProp,
frozenCount
};
const columnOptions =
typeof frozenCount === 'number'
? {
...columnOptionsProp,
frozenCount
}
: { ...columnOptionsProp };

this.gridInst = new TuiGrid({
el: this.rootEl.current,
Expand All @@ -53,15 +54,26 @@ export default class Grid extends React.Component {
this.bindEventHandlers(this.props);
}

componentWillUnmount() {
this.gridInst.destroy();
}

shouldComponentUpdate(nextProps) {
const { oneTimeBindingProps = [] } = this.props;
const reactiveProps = Object.keys(reactivePropSetterMap).filter(
propName => oneTimeBindingProps.indexOf(propName) === -1
);

reactiveProps.forEach(propName => {
const currentValue = this.props[propName];
const nextValue = nextProps[propName];
let currentValue, nextValue;
if (propName === 'columnOptions' && this.props.columnOptions) {
currentValue = this.props.columnOptions.frozenCount;
nextValue = nextProps.columnOptions.frozenCount;
} else {
currentValue = this.props[propName];
nextValue = nextProps[propName];
}

if (currentValue !== nextValue) {
const setterName = reactivePropSetterMap[propName];
this.gridInst[setterName](nextValue);
Expand Down
2 changes: 2 additions & 0 deletions stories/index.stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ stories.add('Events', () => {
stories.add('Reactive Props', () => {
const dataValue = object('data', data.slice(0, 5));
const columnsValue = object('columns', columns);
const columnOptions = object('columnOptions', {frozenCount: 2});
const bodyHeightValue = number('bodyHeight', 300, {
range: true,
min: 100,
Expand All @@ -137,6 +138,7 @@ stories.add('Reactive Props', () => {
columns={columnsValue}
data={dataValue}
frozenColumnCount={frozenColumnCountValue}
columnOptions={columnOptions}
pagination={false}
bodyHeight={bodyHeightValue}
oneTimeBindingProps={oneTimeBindingProps}
Expand Down