Skip to content

Commit

Permalink
FIX Clicking dropdown actions now closes the actions menu. Move click…
Browse files Browse the repository at this point in the history
… bubbling logic to Element
  • Loading branch information
robbieaverill authored and raissanorth committed Sep 25, 2018
1 parent ba0fd39 commit ed6891a
Show file tree
Hide file tree
Showing 13 changed files with 57 additions and 30 deletions.
2 changes: 1 addition & 1 deletion client/dist/js/bundle.js

Large diffs are not rendered by default.

20 changes: 10 additions & 10 deletions client/src/components/ElementActions/AbstractAction.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
import React, { PropTypes } from 'react';
import classNames from 'classnames';
import { DropdownItem } from 'reactstrap';

/**
* Renders an action item for the "more actions" dropdown on elements
*/
const AbstractAction = (props) => {
const { disabled, extraClass, title, onClick } = props;
const { className, title } = props;

const itemProps = {
className: classNames(className, 'dropdown-item'),
...props,
};

return (
<button
onClick={onClick}
disabled={disabled}
title={title}
type="button"
className={classNames(extraClass, 'dropdown-item')}
>
<DropdownItem {...itemProps}>
{title}
</button>
</DropdownItem>
);
};

AbstractAction.propTypes = {
disabled: PropTypes.bool,
extraClass: PropTypes.string,
className: PropTypes.string,
onClick: PropTypes.func,
title: PropTypes.string,
};
Expand Down
3 changes: 2 additions & 1 deletion client/src/components/ElementActions/ArchiveAction.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@ const ArchiveAction = (MenuComponent) => (props) => {

const newProps = {
title: i18n._t('ArchiveAction.ARCHIVE', 'Archive'),
extraClass: 'element-editor__actions-archive',
className: 'element-editor__actions-archive',
onClick: handleClick,
toggle: props.toggle,
};

return (
Expand Down
3 changes: 2 additions & 1 deletion client/src/components/ElementActions/PublishAction.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,9 @@ const PublishAction = (MenuComponent) => (props) => {

const newProps = {
title: i18n._t('PublishAction.PUBLISH', 'Publish'),
extraClass: 'element-editor__actions-publish',
className: 'element-editor__actions-publish',
onClick: handleClick,
toggle: props.toggle,
};

return (
Expand Down
25 changes: 18 additions & 7 deletions client/src/components/ElementActions/SaveAction.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import React from 'react';
import { compose } from 'redux';
import { connect } from 'react-redux';
import AbstractAction from 'components/ElementActions/AbstractAction';
import backend from 'lib/Backend';
import i18n from 'i18n';
Expand All @@ -13,27 +15,28 @@ const SaveAction = (MenuComponent) => (props) => {
const handleClick = (event) => {
event.stopPropagation();

const { id } = this.props;
const { id, securityId } = props;

const formData = getSerializedFormData(`Form_ElementForm_${id}`);

const endpointSpec = {
url: loadElementSchemaValue('saveUrl', id),
method: loadElementSchemaValue('saveMethod'),
payloadFormat: loadElementSchemaValue('payloadFormat'),
defaultData: {
SecurityID: securityId
},
};

// @todo add CSRF SecurityID token
const endpoint = backend.createEndpointFetcher(endpointSpec);
endpoint(formData).then((result) => {
console.log(result);
// @todo update apollo cache?
endpoint(formData).then(() => {
// @todo update apollo cache? `result` argument is available
});
};

const newProps = {
title: i18n._t('SaveAction.SAVE', 'Save'),
extraClass: 'element-editor__actions-save',
className: 'element-editor__actions-save',
onClick: handleClick,
};

Expand All @@ -46,4 +49,12 @@ const SaveAction = (MenuComponent) => (props) => {
);
};

export default SaveAction;
function mapStateToProps(state) {
return {
securityId: state.config.SecurityID,
};
}

export { SaveAction as Component };

export default compose(connect(mapStateToProps), SaveAction);
3 changes: 2 additions & 1 deletion client/src/components/ElementActions/UnpublishAction.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,9 @@ const UnpublishAction = (MenuComponent) => (props) => {

const newProps = {
title: i18n._t('UnpublishAction.UNPUBLISH', 'Unpublish'),
extraClass: 'element-editor__actions-unpublish',
className: 'element-editor__actions-unpublish',
onClick: handleClick,
toggle: props.toggle,
};

return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import React from 'react';
import AbstractAction from '../AbstractAction';
import Enzyme, { shallow } from 'enzyme';
import Enzyme, { mount } from 'enzyme';
import Adapter from 'enzyme-adapter-react-15.4/build/index';

Enzyme.configure({ adapter: new Adapter() });
Expand All @@ -13,18 +13,19 @@ describe('AbstractAction', () => {
let wrapper = null;

beforeEach(() => {
wrapper = shallow(
wrapper = mount(
<AbstractAction
onClick={clickHandler}
title="My abstract action"
disabled={false}
extraClass="foo-bar"
className="foo-bar"
toggle={false}
/>
);
});

it('renders a button', () => {
expect(wrapper.find('button').length).toBe(1);
it('renders a DropdownItem', () => {
expect(wrapper.find('DropdownItem').length).toBe(1);
});

it('includes the title text', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ describe('ArchiveAction', () => {
id={123}
isPublished
actions={{ handleArchiveBlock: mockMutation }}
toggle={false}
/>
);
});
Expand Down Expand Up @@ -55,6 +56,7 @@ describe('ArchiveAction', () => {
id={123}
isPublished={false}
actions={{ handleArchiveBlock: mockMutation }}
toggle={false}
/>
);
const mockConfirm = jest.fn();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ describe('PublishAction', () => {
version={234}
isLiveVersion={false}
actions={{ handlePublishBlock: mockMutation }}
toggle={false}
/>
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ describe('UnpublishAction', () => {
id={123}
isPublished
actions={{ handleUnpublishBlock: mockMutation }}
toggle={false}
/>
);

Expand Down
10 changes: 8 additions & 2 deletions client/src/components/ElementEditor/Element.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,15 @@ class Element extends Component {
* Expand the element to show the preview
* If the element is not inline-editable, take user to the GridFieldDetailForm to edit the record
*/
handleExpand() {
handleExpand(event) {
const { element, link } = this.props;

if (event.target.type === 'button') {
// Stop bubbling if the click target was a button within this container
event.stopPropagation();
return;
}

if (element.InlineEditable) {
this.setState({
previewExpanded: !this.state.previewExpanded
Expand All @@ -69,7 +75,7 @@ class Element extends Component {
*/
handleKeyUp(event) {
if (event.keyCode === 13) {
this.handleExpand();
this.handleExpand(event);
}
}

Expand Down
2 changes: 0 additions & 2 deletions client/src/components/ElementEditor/ElementActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,12 @@ class ElementActions extends Component {
'font-icon-dot-3',
];

// Remove btn-icon-xl make btn-sm
return (
<ActionMenuComponent
id={`element-editor-actions-${id}`}
className={'element-editor-header__actions-dropdown'}
dropdownMenuProps={{ right: true }}
dropdownToggleClassNames={dropdownToggleClassNames}
toggleCallback={(event) => event.stopPropagation()}
>
{ this.renderEditTabs() }
{ this.renderDivider() }
Expand Down
4 changes: 4 additions & 0 deletions client/src/state/editor/getSerializedFormData.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
*/
export const getSerializedFormData = (formName) => {
const form = document.getElementById(formName);
if (!form) {
return {};
}

const fields = form.querySelectorAll('input, select, textarea, checkbox');

const output = {};
Expand Down

0 comments on commit ed6891a

Please # to comment.