Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

issue 781 #782

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions src/module.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,8 @@ export namespace plugins {

var LocalPlugin : GriddlePlugin;

var LocalCustomPlugin : GriddlePlugin;

interface PositionSettings {
// The height of the table
tableHeight?: number|string;
Expand Down
2 changes: 2 additions & 0 deletions src/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@ import utils from './utils';
import CorePlugin from './core';
import LegacyStylePlugin from './plugins/legacyStyle';
import LocalPlugin from './plugins/local';
import LocalCustomPlugin from './plugins/localCustomPlugin';
import PositionPlugin from './plugins/position';

const plugins = {
CorePlugin,
LegacyStylePlugin,
LocalPlugin,
LocalCustomPlugin,
PositionPlugin,
};

Expand Down
16 changes: 16 additions & 0 deletions src/plugins/localCustomPlugin/components/TableBody.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import React from 'react';

const TableBody = ({ rowIds, Row, style, className, extTrigger}) => {
return (
<tbody style={style} className={className}>
{ rowIds && rowIds.map((k, i) => <Row key={k} griddleKey={k} index={i} />) }
<tr>
<td colSpan="3">
This Button will use an external callback, brought in by the TableContainer (set in Griddle props), This trigger will print something and try to set state in the external context, but It fails
<button onClick={extTrigger}> Try me </button>
</td>
</tr>
</tbody>
)};

export default TableBody;
39 changes: 39 additions & 0 deletions src/plugins/localCustomPlugin/components/TableBodyContainer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import React from 'react';
import PropTypes from 'prop-types';
import { connect } from '../../../utils/griddleConnect';
import compose from 'recompose/compose';
import mapProps from 'recompose/mapProps';
import getContext from 'recompose/getContext';

import { classNamesForComponentSelector, stylesForComponentSelector } from '../selectors/localSelectors';

const ComposedTableBodyContainer = OriginalComponent => compose(
getContext({
components: PropTypes.object,
selectors: PropTypes.object,
}),
mapProps(props => ({
Row: props.components.Row,
visibleRowIdsSelector: props.selectors.visibleRowIdsSelector,
...props
})),
connect((state, props) => ({
visibleRowIds: props.visibleRowIdsSelector(state),
className: classNamesForComponentSelector(state, 'TableBody'),
style: stylesForComponentSelector(state, 'TableBody'),
extTrigger: ((state)=>{return state.get('extTrigger')})(state),
})),
// withHandlers({
// Row: props => props.components.Row
// })
)(({ Row, visibleRowIds, style, className, extTrigger }) => (
<OriginalComponent
rowIds={visibleRowIds}
Row={Row}
style={style}
className={className}
extTrigger={extTrigger}
/>
));

export default ComposedTableBodyContainer;
9 changes: 9 additions & 0 deletions src/plugins/localCustomPlugin/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import components from './components';
import * as reducer from './reducers';
// import * as selectors from './selectors/localSelectors';

export default {
components,
// reducer
// selectors
};
39 changes: 38 additions & 1 deletion stories/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import GenericGriddle, { connect, actions, components, selectors, plugins, utils
const { Cell, Row, Table, TableContainer, TableBody, TableHeading, TableHeadingCell } = components;
const { SettingsWrapper, SettingsToggle, Settings } = components;

const { LegacyStylePlugin, LocalPlugin, PositionPlugin } = plugins;
const { LegacyStylePlugin, LocalPlugin, PositionPlugin, LocalCustomPlugin } = plugins;

import fakeData, { FakeData } from './fakeData';
import { person, fakeData2, personClass, fakeData3 } from './fakeData2';
Expand Down Expand Up @@ -105,6 +105,43 @@ storiesOf('Griddle main', module)
</Griddle>
)
})
.add('Controlling external state through a callback', () => {

class Something extends React.Component<{}, any> {
constructor() {
super();

this.extTrigger = this.extTrigger.bind(this)

this.state = {
extVar: 0
};
}

extTrigger(){
console.log("EXT TRIGGER");
this.setState({ extVar: 1 })
}

render() {
const {
extVar
} = this.state

return (
<div>
<p>The magic number is: {extVar}</p>
<Griddle extTrigger={this.extTrigger} data={fakeData} plugins={[LocalPlugin, LocalCustomPlugin]}>
<RowDefinition>
</RowDefinition>
</Griddle>
</div>
)
}
}

return <Something />
})
.add('with local, delayed data', () => {
class DeferredGriddle extends React.Component<GriddleProps<FakeData>, { data?: FakeData[] }> {
private timeout;
Expand Down