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

Hover plugin #639

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 @@ -403,6 +403,8 @@ export namespace plugins {
disablePointerEvents?: boolean;
}
var PositionPlugin : (settings: PositionSettings) => GriddlePlugin;

var HoverPlugin : GriddlePlugin;
}

export const ColumnDefinition;
Expand Down
2 changes: 2 additions & 0 deletions src/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ import utils from './utils';

import LocalPlugin from './plugins/local';
import PositionPlugin from './plugins/position';
import HoverPlugin from './plugins/hover';

const plugins = {
LocalPlugin,
PositionPlugin,
HoverPlugin
};

const ColumnDefinition = components.ColumnDefinition;
Expand Down
5 changes: 5 additions & 0 deletions src/plugins/hover/actions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { GRIDDLE_ROW_MOUSE_ENTER, GRIDDLE_ROW_MOUSE_LEAVE } from './constants';

export const rowMouseEnter = griddleKey => ({type: GRIDDLE_ROW_MOUSE_ENTER, griddleKey});

export const rowMouseLeave = () => ({type: GRIDDLE_ROW_MOUSE_LEAVE});
43 changes: 43 additions & 0 deletions src/plugins/hover/components.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import React from 'react';
import compose from 'recompose/compose';
import { connect } from 'react-redux';
import { bindActionCreators } from 'redux';

import components from '../../components';
import { rowMouseEnter, rowMouseLeave } from './actions';

export const RowContainer = compose(
connect(
state => {
return {
hoveredRowKey: state.get('hoveredRowKey', null)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not entirely convinced it should be the plugin's responsibility to inject hoveredRowKey into Cell (or anywhere). It seems to be an implementation detail of this demo. I'm more inclined to avoid customizing Row altogether, letting ButtonsColumn be connected instead:

const ButtonsColumn = connect(
    state => ({ hoveredRowKey: state.get('hoveredRowKey', null) })
  )(
    ({griddleKey,hoveredRowKey}) =>
      (griddleKey === hoveredRowKey) && <button>Edit</button> : null
  );

};
},
(dispatch, {griddleKey}) => bindActionCreators({
focusIn: rowMouseEnter.bind(this, griddleKey),
focusOut: rowMouseLeave
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As of #679, if these were onMouseEnter/onMouseLeave they'd be passed through correctly in Row.

}, dispatch)
),
components.RowContainer
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather than compose this with the default RowContainer, it's probably more correct for this to be a RowEnhancer (like in this example story).

);

export const Row = ({Cell, griddleKey, columnIds, style, className, focusIn, focusOut, hoveredRowKey}) => (
<tr
key={griddleKey}
style={style}
className={className}
onMouseEnter={focusIn}
onMouseLeave={focusOut}
>
{ columnIds && columnIds.map(c => (
<Cell
key={`${c}-${griddleKey}`}
griddleKey={griddleKey}
columnId={c}
style={style}
className={className}
hoveredRowKey={hoveredRowKey}
/>
))}
</tr>
);
2 changes: 2 additions & 0 deletions src/plugins/hover/constants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const GRIDDLE_ROW_MOUSE_ENTER = 'GRIDDLE_ROW_MOUSE_ENTER';
export const GRIDDLE_ROW_MOUSE_LEAVE = 'GRIDDLE_ROW_MOUSE_LEAVE';
7 changes: 7 additions & 0 deletions src/plugins/hover/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import * as components from './components';
import * as reducer from './reducers';

export default {
components,
reducer
};
5 changes: 5 additions & 0 deletions src/plugins/hover/reducers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export const GRIDDLE_ROW_MOUSE_ENTER = (state, {griddleKey}) =>
state.set('hoveredRowKey', griddleKey);

export const GRIDDLE_ROW_MOUSE_LEAVE = (state, action) =>
state.delete('hoveredRowKey');
18 changes: 17 additions & 1 deletion stories/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import GenericGriddle, { actions, components, selectors, plugins, utils, ColumnD
const { Cell, Row, Table, TableContainer, TableBody, TableHeading, TableHeadingCell } = components;
const { SettingsWrapper, SettingsToggle, Settings } = components;

const { LocalPlugin, PositionPlugin } = plugins;
const { LocalPlugin, PositionPlugin, HoverPlugin } = plugins;
const { rowDataSelector } = LocalPlugin.selectors;

import fakeData, { FakeData } from './fakeData';
Expand Down Expand Up @@ -1020,3 +1020,19 @@ storiesOf('Settings', module)
<Griddle data={fakeData} plugins={[LocalPlugin,PageSizeDropDownPlugin(pluginConfig)]} settingsComponentObjects={{ columnChooser: null }} />
);
})

storiesOf('HoverPlugin', module)
.add('base', () => {
const ButtonsColumn = ({griddleKey,hoveredRowKey}) =>
(griddleKey === hoveredRowKey) ? <button>Edit</button> : null;

return <Griddle data={fakeData} plugins={[LocalPlugin,HoverPlugin]}>
<RowDefinition>
<ColumnDefinition id='name'/>
<ColumnDefinition id='city'/>
<ColumnDefinition id='state'/>
<ColumnDefinition id='country'/>
<ColumnDefinition id='button' title=' ' visible={true} locked={true} sortable={false} width='100px' customComponent={ButtonsColumn}/>
</RowDefinition>
</Griddle>;
});