Skip to content

Commit

Permalink
Enhancement rename action props with on prefix, and action methods …
Browse files Browse the repository at this point in the history
…with `handle` prefix
  • Loading branch information
Christopher Joe committed Sep 19, 2017
1 parent d477850 commit f607547
Show file tree
Hide file tree
Showing 28 changed files with 295 additions and 328 deletions.
6 changes: 3 additions & 3 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ module.exports = {
"vars": "local"
}
],
"react/no-danger": [
"error"
],
"react/forbid-prop-types": [
"off"
],
Expand All @@ -60,9 +63,6 @@ module.exports = {
"no-useless-escape": [
"off"
],
"react/no-danger": [
"error"
],
}),
"settings": {
"import/extensions": [
Expand Down
14 changes: 11 additions & 3 deletions client/src/components/Breadcrumb/Breadcrumb.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { Component } from 'react';
import React, { Component, PropTypes } from 'react';
import { connect } from 'react-redux';

class Breadcrumb extends Component {
Expand Down Expand Up @@ -44,7 +44,7 @@ class Breadcrumb extends Component {
className={iconClassNames.join(' ')}
role="button"
tabIndex={0}
onClick={crumb.icon.action}
onClick={crumb.icon.onClick}
/>
)}
</h2>
Expand All @@ -67,7 +67,15 @@ class Breadcrumb extends Component {
}

Breadcrumb.propTypes = {
crumbs: React.PropTypes.array,
crumbs: PropTypes.arrayOf(PropTypes.shape({
onClick: PropTypes.func,
text: PropTypes.string,
icon: PropTypes.shape({
className: PropTypes.string,
onClick: PropTypes.func,
action: (props) => { if (props.action) { throw new Error('action: no longer used'); } },
})
})),
};

function mapStateToProps(state) {
Expand Down
2 changes: 1 addition & 1 deletion client/src/components/Breadcrumb/tests/breadcrumb-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ describe('BreadcrumbsComponent', () => {
href: 'href3',
icon: {
className: 'breadcrumb3icon',
action: jest.genMockFunction(),
onClick: jest.fn(),
},
},
];
Expand Down
16 changes: 7 additions & 9 deletions client/src/components/CheckboxField/CheckboxField.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
import React, { Component } from 'react';
import React from 'react';
import OptionField from '../OptionsetField/OptionField';
import fieldHolder from 'components/FieldHolder/FieldHolder';

class CheckboxField extends Component {
render() {
// Build standard checkbox with fieldholder
const FieldHolder = fieldHolder(OptionField);
const CheckboxField = () => {
// Build standard checkbox with fieldholder
const FieldHolder = fieldHolder(OptionField);

// set to not show field holder labels, as checkbox already generates a label
return <FieldHolder {...this.props} type="checkbox" hideLabels />;
}
}
// set to not show field holder labels, as checkbox already generates a label
return <FieldHolder {...this.props} type="checkbox" hideLabels />;
};

export default CheckboxField;
48 changes: 24 additions & 24 deletions client/src/components/CheckboxSetField/CheckboxSetField.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,30 +44,6 @@ class CheckboxSetField extends Component {
return [];
}

/**
* Handler for sorting what the value of the field will be, this flows on from the
* OptionField (single checkbox) event handler and adding or removing the corresponding value the
* single checkbox represented to suit the multiple checkbox group.
*
* @param {Event} event
* @param {object} field
*/
handleChange(event, field) {
if (typeof this.props.onChange === 'function') {
const oldValue = this.getValues();
const newValue = this.props.source
.filter((item, index) => {
if (this.getItemKey(item, index) === field.id) {
return field.value === 1;
}
return oldValue.indexOf(`${item.value}`) > -1;
})
.map((item) => `${item.value}`);

this.props.onChange(newValue);
}
}

/**
* Fetches properties for an item
*
Expand All @@ -93,6 +69,30 @@ class CheckboxSetField extends Component {
};
}

/**
* Handler for sorting what the value of the field will be, this flows on from the
* OptionField (single checkbox) event handler and adding or removing the corresponding value the
* single checkbox represented to suit the multiple checkbox group.
*
* @param {Event} event
* @param {object} field
*/
handleChange(event, field) {
if (typeof this.props.onChange === 'function') {
const oldValue = this.getValues();
const newValue = this.props.source
.filter((item, index) => {
if (this.getItemKey(item, index) === field.id) {
return field.value === 1;
}
return oldValue.indexOf(`${item.value}`) > -1;
})
.map((item) => `${item.value}`);

this.props.onChange(newValue);
}
}

render() {
if (!this.props.source) {
return null;
Expand Down
62 changes: 31 additions & 31 deletions client/src/components/FieldHolder/FieldHolder.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,36 @@ function fieldHolder(Field) {
return message;
}

/**
* Generates the properties for the field holder
*
* @returns {object}
*/
getHolderProps() {
// The extraClass property is defined on both the holder and element
// for legacy reasons (same behaviour as PHP rendering)
const classNames = [
'field',
this.props.extraClass,
];
if (this.props.readOnly) {
classNames.push('readonly');
}

return {
bsClass: this.props.bsClass,
bsSize: this.props.bsSize,
validationState: this.props.validationState,
className: classNames.join(' '),
controlId: this.props.id,
id: this.props.holderId,
};
}

/**
* Build description
*
* @returns {Component}
* @returns {React}
*/
renderDescription() {
if (this.props.description === null) {
Expand All @@ -47,7 +73,7 @@ function fieldHolder(Field) {
/**
* Build a FormAlert
*
* @returns {Component}
* @returns {React}
*/
renderMessage() {
const message = this.getMessage();
Expand All @@ -66,7 +92,7 @@ function fieldHolder(Field) {
/**
* Build title label
*
* @returns {Component}
* @returns {React}
*/
renderLeftTitle() {
const labelText = this.props.leftTitle !== null
Expand All @@ -87,7 +113,7 @@ function fieldHolder(Field) {
/**
* Build title label
*
* @returns {Component}
* @returns {React}
*/
renderRightTitle() {
if (!this.props.rightTitle || this.props.hideLabels) {
Expand All @@ -101,32 +127,6 @@ function fieldHolder(Field) {
);
}

/**
* Generates the properties for the field holder
*
* @returns {object}
*/
getHolderProps() {
// The extraClass property is defined on both the holder and element
// for legacy reasons (same behaviour as PHP rendering)
const classNames = [
'field',
this.props.extraClass,
];
if (this.props.readOnly) {
classNames.push('readonly');
}

return {
bsClass: this.props.bsClass,
bsSize: this.props.bsSize,
validationState: this.props.validationState,
className: classNames.join(' '),
controlId: this.props.id,
id: this.props.holderId,
};
}

renderField() {
const hasMessage = Boolean(this.getMessage());
const props = {
Expand All @@ -137,7 +137,7 @@ function fieldHolder(Field) {
),
};

const field = <Field { ...props} />;
const field = <Field {...props} />;
const prefix = this.props.data.prefix;
const suffix = this.props.data.suffix;
if (!prefix && !suffix) {
Expand Down
1 change: 1 addition & 0 deletions client/src/components/Form/Form.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ Form.propTypes = {
method: PropTypes.string.isRequired,
}),
fields: PropTypes.array.isRequired,
// props is named `handleSubmit` as it is recieved from redux-form
handleSubmit: PropTypes.func,
mapActionsToComponents: PropTypes.func.isRequired,
mapFieldsToComponents: PropTypes.func.isRequired,
Expand Down
51 changes: 26 additions & 25 deletions client/src/components/FormAction/FormAction.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,6 @@ class FormAction extends Component {
this.handleClick = this.handleClick.bind(this);
}

render() {
const title = this.props.title;

return (
<button {...this.getButtonProps()}>
{this.getLoadingIcon()}
{castStringToElement('span', title, { className: 'btn__title' })}
</button>
);
}

/**
* Get props for the button
*
Expand Down Expand Up @@ -71,17 +60,6 @@ class FormAction extends Component {
return classnames(buttonClasses);
}

/**
* @return {boolean}
*/
isPrimary() {
const extraClasses = this.props.extraClass.split(' ');
return (
this.props.name === 'action_save' ||
extraClasses.find(className => className === 'ss-ui-action-constructive')
);
}

/**
* Gets the bootstrap classname for this action
*
Expand Down Expand Up @@ -140,23 +118,46 @@ class FormAction extends Component {
return null;
}

/**
* @return {boolean}
*/
isPrimary() {
const extraClasses = this.props.extraClass.split(' ');
return (
this.props.name === 'action_save' ||
extraClasses.find(className => className === 'ss-ui-action-constructive')
);
}

/**
* Event handler triggered when a user clicks the button.
*
* @param {Object} event
* @return undefined
*/
handleClick(event) {
if (typeof this.props.handleClick === 'function') {
this.props.handleClick(event, this.props.name || this.props.id);
if (typeof this.props.onClick === 'function') {
this.props.onClick(event, this.props.name || this.props.id);
}
}

render() {
const title = this.props.title;

return (
<button {...this.getButtonProps()}>
{this.getLoadingIcon()}
{castStringToElement('span', title, { className: 'btn__title' })}
</button>
);
}
}

FormAction.propTypes = {
id: React.PropTypes.string,
name: React.PropTypes.string,
handleClick: React.PropTypes.func,
onClick: React.PropTypes.func,
handleClick: () => { throw new Error('no longer used'); },
title: React.PropTypes.string,
type: React.PropTypes.string,
loading: React.PropTypes.bool,
Expand Down
22 changes: 11 additions & 11 deletions client/src/components/FormAlert/FormAlert.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,6 @@ class FormAlert extends Component {
};
}

/**
* Handler for when the message box is dismissed and hidden
*/
handleDismiss() {
if (typeof this.props.onDismiss === 'function') {
this.props.onDismiss();
} else {
this.setState({ visible: false });
}
}

/**
* Determines the style for the alert box to be shown based on messageType or other property
* by default use "danger".
Expand Down Expand Up @@ -71,6 +60,17 @@ class FormAlert extends Component {
};
}

/**
* Handler for when the message box is dismissed and hidden
*/
handleDismiss() {
if (typeof this.props.onDismiss === 'function') {
this.props.onDismiss();
} else {
this.setState({ visible: false });
}
}

render() {
// @todo default this.props.visible as null
if ((typeof this.props.visible !== 'boolean' && this.state.visible) || this.props.visible) {
Expand Down
Loading

0 comments on commit f607547

Please sign in to comment.