mirror of
https://github.com/gosticks/react-bootstrap-table2.git
synced 2026-08-11 11:40:22 +00:00
implement selection consumers
This commit is contained in:
+5
-5
@@ -5,11 +5,11 @@ import React from 'react';
|
||||
import PropTypes from 'prop-types';
|
||||
|
||||
import _ from './utils';
|
||||
import Row from './row';
|
||||
import RowAggregator from './row-aggregator';
|
||||
import RowSection from './row-section';
|
||||
import Row from './row/simple-row';
|
||||
import RowAggregator from './row/aggregate-row';
|
||||
import RowSection from './row/row-section';
|
||||
import Const from './const';
|
||||
import bindSelection from './row-selection/row-binder';
|
||||
import withRowSelection from './row-selection/row-consumer';
|
||||
import bindExpansion from './row-expand/row-binder';
|
||||
|
||||
class Body extends React.Component {
|
||||
@@ -55,7 +55,7 @@ class Body extends React.Component {
|
||||
}
|
||||
|
||||
if (selectRowEnabled) {
|
||||
RowComponent = bindSelection(expandRowEnabled ? RowComponent : RowAggregator);
|
||||
RowComponent = withRowSelection(expandRowEnabled ? RowComponent : RowAggregator);
|
||||
}
|
||||
|
||||
if (cellEdit.createContext) {
|
||||
|
||||
+2
-2
@@ -5,7 +5,7 @@ import PropTypes from 'prop-types';
|
||||
import HeaderCell from './header-cell';
|
||||
import SelectionHeaderCell from './row-selection/selection-header-cell';
|
||||
import ExpandHeaderCell from './row-expand/expand-header-cell';
|
||||
import bindSelection from './row-selection/selection-header-cell-binder';
|
||||
import withHeaderSelection from './row-selection/selection-header-cell-consumer';
|
||||
import bindExpansion from './row-expand/expand-header-cell-binder';
|
||||
|
||||
const Header = (props) => {
|
||||
@@ -30,7 +30,7 @@ const Header = (props) => {
|
||||
}
|
||||
|
||||
if (selectRow) {
|
||||
SelectionHeaderCellComp = bindSelection(SelectionHeaderCell);
|
||||
SelectionHeaderCellComp = withHeaderSelection(SelectionHeaderCell);
|
||||
}
|
||||
|
||||
return (
|
||||
|
||||
@@ -0,0 +1,63 @@
|
||||
/* eslint react/prop-types: 0 */
|
||||
import React from 'react';
|
||||
import cs from 'classnames';
|
||||
import _ from '../utils';
|
||||
import SelectionContext from '../contexts/selection-context';
|
||||
|
||||
export default (Component) => {
|
||||
const renderWithSelection = (props, selectRow) => {
|
||||
const key = props.value;
|
||||
const selected = selectRow.selected.includes(key);
|
||||
const selectable = !selectRow.nonSelectable || !selectRow.nonSelectable.includes(key);
|
||||
|
||||
let {
|
||||
style,
|
||||
className
|
||||
} = props;
|
||||
|
||||
if (selected) {
|
||||
const selectedStyle = _.isFunction(selectRow.style)
|
||||
? selectRow.style(props.row, props.rowIndex)
|
||||
: selectRow.style;
|
||||
|
||||
const selectedClasses = _.isFunction(selectRow.classes)
|
||||
? selectRow.classes(props.row, props.rowIndex)
|
||||
: selectRow.classes;
|
||||
|
||||
style = {
|
||||
...style,
|
||||
...selectedStyle
|
||||
};
|
||||
className = cs(className, selectedClasses) || undefined;
|
||||
|
||||
if (selectRow.bgColor) {
|
||||
style = style || {};
|
||||
style.backgroundColor = _.isFunction(selectRow.bgColor)
|
||||
? selectRow.bgColor(props.row, props.rowIndex)
|
||||
: selectRow.bgColor;
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<Component
|
||||
{ ...props }
|
||||
style={ style }
|
||||
className={ className }
|
||||
selectRow={ selectRow }
|
||||
selected={ selected }
|
||||
selectable={ selectable }
|
||||
/>
|
||||
);
|
||||
};
|
||||
|
||||
function withConsumer(props) {
|
||||
return (
|
||||
<SelectionContext.Consumer>
|
||||
{ selectRow => renderWithSelection(props, selectRow) }
|
||||
</SelectionContext.Consumer>
|
||||
);
|
||||
}
|
||||
|
||||
withConsumer.displayName = 'WithSelectionRowConsumer';
|
||||
return withConsumer;
|
||||
};
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
import React from 'react';
|
||||
import SelectionContext from '../contexts/selection-context';
|
||||
|
||||
export default Component => () => (
|
||||
<SelectionContext.Consumer>
|
||||
{ selectRow => <Component { ...selectRow } /> }
|
||||
</SelectionContext.Consumer>
|
||||
);
|
||||
Reference in New Issue
Block a user