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

Fix table re-rendering by showing/hiding actions column via CSS instead #665

Merged
merged 6 commits into from
Apr 13, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
98 changes: 78 additions & 20 deletions src-docs/src/views/tables/actions/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
EuiFlexItem,
EuiSwitch,
EuiSpacer,
EuiText,
} from '../../../../../src/components';

/*
Expand Down Expand Up @@ -47,7 +48,8 @@ export class Table extends Component {
sortField: 'firstName',
sortDirection: 'asc',
selectedItems: [],
multiAction: false
multiAction: false,
customAction: false,
};
}

Expand Down Expand Up @@ -79,6 +81,10 @@ export class Table extends Component {
});
};

onSelectionChange = (selectedItems) => {
this.setState({ selectedItems });
};

renderDeleteButton() {
const { selectedItems } = this.state;

Expand All @@ -101,6 +107,10 @@ export class Table extends Component {
this.setState(prevState => ({ multiAction: !prevState.multiAction }));
};

toggleCustomAction = () => {
this.setState(prevState => ({ customAction: !prevState.customAction }));
};

deleteUser = user => {
store.deleteUsers(user.id);
this.setState({ selectedItems: [] });
Expand All @@ -117,6 +127,8 @@ export class Table extends Component {
pageSize,
sortField,
sortDirection,
multiAction,
customAction,
} = this.state;

const {
Expand All @@ -126,6 +138,63 @@ export class Table extends Component {

const deleteButton = this.renderDeleteButton();

let actions = null;

if(multiAction) {
actions = customAction
? [{
render: (item) => {
return (
<EuiText color="secondary" onClick={() => this.cloneUser(item)}>
Clone
</EuiText>
);
}
}, {
render: (item) => {
return (
<EuiText color="danger" onClick={() => this.deleteUser(item)}>
Delete
</EuiText>
);
}
}]
: [{
name: 'Clone',
description: 'Clone this person',
icon: 'copy',
onClick: this.cloneUser
}, {
name: 'Delete',
description: 'Delete this person',
icon: 'trash',
color: 'danger',
onClick: this.deleteUser
}];
} else {
actions = customAction
? [{
render: (item) => {
return (
<EuiLink
onClick={() => this.deleteUser(item)}
color="danger"
>
Delete
</EuiLink>
);
}
}]
: [{
name: 'Delete',
description: 'Delete this person',
icon: 'trash',
color: 'danger',
type: 'icon',
onClick: this.deleteUser
}];
}

const columns = [{
field: 'firstName',
name: 'First Name',
Expand Down Expand Up @@ -166,25 +235,7 @@ export class Table extends Component {
sortable: true
}, {
name: 'Actions',
actions: this.state.multiAction ? [{
name: 'Clone',
description: 'Clone this person',
icon: 'copy',
onClick: this.cloneUser
}, {
name: 'Delete',
description: 'Delete this person',
icon: 'trash',
color: 'danger',
onClick: this.deleteUser
}] : [{
name: 'Delete',
type: 'icon',
description: 'Delete this person',
icon: 'trash',
color: 'danger',
onClick: this.deleteUser
}]
actions
}];

const pagination = {
Expand Down Expand Up @@ -219,6 +270,13 @@ export class Table extends Component {
onChange={this.toggleMultiAction}
/>
</EuiFlexItem>
<EuiFlexItem grow={false}>
<EuiSwitch
label="Custom Actions"
checked={this.state.customAction}
onChange={this.toggleCustomAction}
/>
</EuiFlexItem>
</EuiFlexGroup>

<EuiSpacer size="l" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ exports[`CollapsedItemActions render 1`] = `
button={
<EuiButtonIcon
aria-label="actions"
className={undefined}
color="text"
iconType="gear"
isDisabled={false}
Expand All @@ -32,7 +33,9 @@ exports[`CollapsedItemActions render 1`] = `
>
default1
</EuiContextMenuItem>,
<EuiContextMenuItem />,
<EuiContextMenuItem
onClick={[Function]}
/>,
]
}
/>
Expand Down
7 changes: 5 additions & 2 deletions src/components/basic_table/collapsed_item_actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export class CollapsedItemActions extends Component {

render() {

const { actions, itemId, item, actionEnabled, onFocus } = this.props;
const { actions, itemId, item, actionEnabled, onFocus, className } = this.props;

const isOpen = this.state.popoverOpen;

Expand All @@ -60,8 +60,9 @@ export class CollapsedItemActions extends Component {
allDisabled = allDisabled && !enabled;
if (action.render) {
const actionControl = action.render(item, enabled);
const actionControlOnClick = actionControl && actionControl.props && actionControl.props.onClick;
controls.push(
<EuiContextMenuItem key={key}>
<EuiContextMenuItem key={key} onClick={actionControlOnClick ? actionControlOnClick.bind(null, item) : () => {}}>
{actionControl}
</EuiContextMenuItem>
);
Expand All @@ -82,6 +83,7 @@ export class CollapsedItemActions extends Component {

const popoverButton = (
<EuiButtonIcon
className={className}
aria-label="actions"
iconType="gear"
color="text"
Expand All @@ -93,6 +95,7 @@ export class CollapsedItemActions extends Component {

return (
<EuiPopover
className={className}
popoverRef={this.registerPopoverDiv}
id={`${itemId}-actions`}
isOpen={isOpen}
Expand Down
4 changes: 2 additions & 2 deletions src/components/basic_table/custom_item_action.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,12 @@ export class CustomItemAction extends Component {
};

render() {
const { action, enabled, item } = this.props;
const { action, enabled, item, className } = this.props;
const tool = action.render(item, enabled);
const clonedTool = cloneElement(tool, { onFocus: this.onFocus, onBlur: this.onBlur });
const style = this.hasFocus() ? { opacity: 1 } : null;
return (
<div style={style}>
<div style={style} className={className}>
{clonedTool}
</div>
);
Expand Down
4 changes: 3 additions & 1 deletion src/components/basic_table/default_item_action.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export class DefaultItemAction extends Component {
}

render() {
const { action, enabled, item } = this.props;
const { action, enabled, item, className } = this.props;
if (!action.onClick) {
throw new Error(`Cannot render item action [${action.name}]. Missing required 'onClick' callback. If you want
to provide a custom action control, make sure to define the 'render' callback`);
Expand All @@ -28,6 +28,7 @@ export class DefaultItemAction extends Component {
}
return (
<EuiButtonIcon
className={className}
aria-label={action.name}
isDisabled={!enabled}
color={color}
Expand All @@ -40,6 +41,7 @@ export class DefaultItemAction extends Component {

return (
<EuiButton
className={className}
size="s"
isDisabled={!enabled}
color={color}
Expand Down
4 changes: 3 additions & 1 deletion src/components/basic_table/expanded_item_actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React from 'react';
import { DefaultItemAction } from './default_item_action';
import { CustomItemAction } from './custom_item_action';

export const ExpandedItemActions = ({ actions, itemId, item, actionEnabled }) => {
export const ExpandedItemActions = ({ actions, itemId, item, actionEnabled, className }) => {

return actions.reduce((tools, action, index) => {
const available = action.available ? action.available(item) : true;
Expand All @@ -16,6 +16,7 @@ export const ExpandedItemActions = ({ actions, itemId, item, actionEnabled }) =>
tools.push(
<CustomItemAction
key={key}
className={className}
index={index}
action={action}
enabled={enabled}
Expand All @@ -27,6 +28,7 @@ export const ExpandedItemActions = ({ actions, itemId, item, actionEnabled }) =>
tools.push(
<DefaultItemAction
key={key}
className={className}
index={index}
action={action}
enabled={enabled}
Expand Down
2 changes: 1 addition & 1 deletion src/components/table/_table.scss
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@
}

.euiTableCellContent--showOnHover {
> * {
.euiTableCellContent__hoverItem {
opacity: 0;

.euiTableRow:hover > .euiTableRowCell > &,
Copy link
Contributor

Choose a reason for hiding this comment

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

The specificity of this selector is too high. Remove the > .euiTableRowCell > and it should still work with just .euiTableRow:hover &

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated!

Expand Down
21 changes: 15 additions & 6 deletions src/components/table/table_row_cell.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,26 @@ export const EuiTableRowCell = ({
'euiTableCellContent--truncateText': truncateText,
// We're doing this rigamarole instead of creating `euiTableCellContent--textOnly` for BWC
// purposes for the time-being.
'euiTableCellContent--overflowingContent': !textOnly,
'euiTableCellContent--overflowingContent': textOnly !== true,
});

const childClasses = classNames({
'euiTableCellContent__text': textOnly === true,
'euiTableCellContent__hoverItem': showOnHover,
});

let modifiedChildren = children;

if(textOnly === true) {
modifiedChildren = <span className={childClasses}>{children}</span>;
} else if(React.isValidElement(modifiedChildren)) {
modifiedChildren = React.Children.map(children, child => React.cloneElement(child, { className: childClasses }));
}

return (
<td className="euiTableRowCell" colSpan={colSpan}>
<div className={contentClasses} {...rest}>
{
textOnly === true
? <span className="euiTableCellContent__text">{children}</span>
: children
}
{modifiedChildren}
</div>
</td>
);
Expand Down