Apply default action props in FormBuilder

Perviously consumers had to props to FormBuilder for styling common actions like 'save'. The props are now automatically passed to FormActions by the FormBuilder. Consumers can override these defaults via the 'createFn' prop.
This commit is contained in:
David Craig 2016-04-20 13:43:13 +12:00 committed by Ingo Schommer
parent ae285f3286
commit 5cc6171f77
2 changed files with 10 additions and 51 deletions

View File

@ -293,12 +293,21 @@ export class FormBuilderComponent extends SilverStripeComponent {
const createFn = this.props.createFn;
return actions.map((action, i) => {
const props = deepFreeze(action);
let props = deepFreeze(action);
if (typeof createFn === 'function') {
return createFn(FormActionComponent, props);
}
// Add sensible defaults for common actions.
if (props.name === 'action_save') {
props = deepFreeze(Object.assign({}, props, {
type: 'submit',
label: props.title,
icon: 'save',
}));
}
return <FormActionComponent key={i} {...props} />;
});
}

View File

@ -25,8 +25,6 @@ class CampaignAdmin extends SilverStripeComponent {
},
});
this.campaignListCreateFn = this.campaignListCreateFn.bind(this);
this.campaignEditCreateFn = this.campaignEditCreateFn.bind(this);
this.campaignCreationCreateFn = this.campaignCreationCreateFn.bind(this);
}
componentDidMount() {
@ -142,7 +140,6 @@ class CampaignAdmin extends SilverStripeComponent {
renderDetailEditView() {
const baseSchemaUrl = this.props.sectionConfig.forms.DetailEditForm.schemaUrl;
const formBuilderProps = {
createFn: this.campaignEditCreateFn,
formId: 'DetailEditForm',
schemaUrl: `${baseSchemaUrl}/ChangeSet/${this.props.campaignId}`,
};
@ -167,7 +164,6 @@ class CampaignAdmin extends SilverStripeComponent {
renderCreateView() {
const baseSchemaUrl = this.props.sectionConfig.forms.CreateEditForm.schemaUrl;
const formBuilderProps = {
createFn: this.campaignCreationCreateFn,
formId: 'CreateEditForm',
schemaUrl: `${baseSchemaUrl}/ChangeSet`,
};
@ -186,28 +182,6 @@ 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
*/
campaignCreationCreateFn(Component, props) {
if (props.name === 'action_save') {
const extendedProps = Object.assign({}, props, {
type: 'submit',
label: props.title,
icon: 'save',
});
return <Component key={props.name} {...extendedProps} />;
}
return <Component key={props.name} {...props} />;
}
/**
* Hook to allow customisation of components being constructed
* by the Campaign list FormBuilder.
@ -266,30 +240,6 @@ class CampaignAdmin extends SilverStripeComponent {
];
}
/*
* Hook to allow customisation of components being constructed
* by the Campaign detail edit FormBuilder.
*
* @param object Component - Component constructor.
* @param object props - Props passed from FormBuilder.
*
* @return object - Instanciated React component
*/
campaignEditCreateFn(Component, props) {
if (props.name === 'action_save') {
const extendedProps = Object.assign({}, props, {
bootstrapButtonStyle: 'success',
type: 'submit',
label: props.title,
icon: 'save',
});
return <Component key={props.name} {...extendedProps} />;
}
return <Component key={props.name} {...props} />;
}
/**
* Gets preview URL for itemid
* @param int id