From 2b8ef99d5e683df3ef54cc64531df1c06f6ec515 Mon Sep 17 00:00:00 2001 From: David Craig Date: Wed, 20 Apr 2016 16:51:00 +1200 Subject: [PATCH] Add 'Cancel' button to Campaign creation form --- .../src/components/FormBuilder/FormBuilder.js | 29 +++++++++++++------ .../containers/CampaignAdmin/CampaignAdmin.js | 28 ++++++++++++++++++ admin/code/CampaignAdmin.php | 3 +- 3 files changed, 50 insertions(+), 10 deletions(-) diff --git a/admin/client/src/components/FormBuilder/FormBuilder.js b/admin/client/src/components/FormBuilder/FormBuilder.js index b5b8bc46e..b13b89c37 100644 --- a/admin/client/src/components/FormBuilder/FormBuilder.js +++ b/admin/client/src/components/FormBuilder/FormBuilder.js @@ -295,17 +295,28 @@ export class FormBuilderComponent extends SilverStripeComponent { return actions.map((action, i) => { let props = deepFreeze(action); - if (typeof createFn === 'function') { - return createFn(FormActionComponent, props); + // Add sensible defaults for common actions. + switch (props.name) { + case 'action_save': + props = deepFreeze(Object.assign({}, props, { + type: 'submit', + label: props.title, + icon: 'save', + })); + break; + case 'action_cancel': + props = deepFreeze(Object.assign({}, props, { + type: 'button', + label: props.title, + icon: 'cancel', + })); + break; + default: + break; } - // Add sensible defaults for common actions. - if (props.name === 'action_save') { - props = deepFreeze(Object.assign({}, props, { - type: 'submit', - label: props.title, - icon: 'save', - })); + if (typeof createFn === 'function') { + return createFn(FormActionComponent, props); } return ; diff --git a/admin/client/src/containers/CampaignAdmin/CampaignAdmin.js b/admin/client/src/containers/CampaignAdmin/CampaignAdmin.js index 439387844..3d9a5e649 100644 --- a/admin/client/src/containers/CampaignAdmin/CampaignAdmin.js +++ b/admin/client/src/containers/CampaignAdmin/CampaignAdmin.js @@ -25,6 +25,7 @@ class CampaignAdmin extends SilverStripeComponent { }, }); this.campaignListCreateFn = this.campaignListCreateFn.bind(this); + this.campaignCreationView = this.campaignCreationView.bind(this); } componentDidMount() { @@ -164,6 +165,7 @@ class CampaignAdmin extends SilverStripeComponent { renderCreateView() { const baseSchemaUrl = this.props.sectionConfig.forms.CreateEditForm.schemaUrl; const formBuilderProps = { + createFn: this.campaignCreationView, formId: 'CreateEditForm', schemaUrl: `${baseSchemaUrl}/ChangeSet`, }; @@ -182,6 +184,32 @@ class CampaignAdmin extends SilverStripeComponent { ); } + /** + * Hook to allow customisation of components being constructed + * by the Campaign creation FormBuilder. + * + * @param {Object} Component - Component constructor. + * @param {Object} props - Props passed from FormBuilder. + * + * @return {Object} - Instanciated React component + */ + campaignCreationView(Component, props) { + const indexRoute = this.props.sectionConfig.route; + + // Route to the Campaigns index view when 'Cancel' is clicked. + if (props.name === 'action_cancel') { + const extendedProps = Object.assign({}, props, { + handleClick: () => { + window.ss.router.show(indexRoute); + }, + }); + + return ; + } + + return ; + } + /** * Hook to allow customisation of components being constructed * by the Campaign list FormBuilder. diff --git a/admin/code/CampaignAdmin.php b/admin/code/CampaignAdmin.php index 0e3e952c2..faadc8cfe 100644 --- a/admin/code/CampaignAdmin.php +++ b/admin/code/CampaignAdmin.php @@ -486,7 +486,8 @@ JSON; 'CreateEditForm', ChangeSet::singleton()->getCMSFields(), FieldList::create( - FormAction::create('save', 'Save') + FormAction::create('save', 'Save'), + FormAction::create('cancel', 'Cancel') ) ); }