-
Notifications
You must be signed in to change notification settings - Fork 356
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3509 from himdel/form-buttons-react
FormButtons component, with redux
- Loading branch information
Showing
6 changed files
with
233 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import React from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import { connect } from 'react-redux'; | ||
import { combineReducers } from 'redux'; | ||
|
||
import FormButtons from './form-buttons'; | ||
|
||
function FormButtonsRedux(props) { | ||
initReducer(); | ||
|
||
return <FormButtons {...props} />; | ||
} | ||
|
||
FormButtonsRedux.propTypes = { | ||
callbackOverrides: PropTypes.object, | ||
}; | ||
|
||
FormButtonsRedux.defaultProps = { | ||
callbackOverrides: {}, | ||
}; | ||
|
||
|
||
function mapStateToProps(state, ownProps) { | ||
let props = {...state.FormButtons}; | ||
|
||
if (state.FormButtons && ownProps.callbackOverrides) { | ||
// allow overriding click handlers | ||
// the original handler is passed as the only argument to the new one | ||
Object.keys(ownProps.callbackOverrides).forEach((name) => { | ||
props[name] = () => { | ||
let origHandler = state.FormButtons[name]; | ||
ownProps.callbackOverrides[name](origHandler); | ||
}; | ||
}); | ||
} | ||
|
||
return props; | ||
} | ||
|
||
export default connect(mapStateToProps)(FormButtonsRedux); | ||
|
||
function initReducer() { | ||
if (! ManageIQ.redux || ! ManageIQ.redux.addReducer) { | ||
// login screen | ||
return; | ||
} | ||
if (initReducer.done) { | ||
// don't init twice | ||
return; | ||
} | ||
initReducer.done = true; | ||
|
||
|
||
const initialState = {...FormButtons.defaultProps}; | ||
|
||
const actions = { | ||
'FormButtons.init': (_state, payload) => ({ ...initialState, ...payload }), | ||
'FormButtons.customLabel': (state, payload) => ({ ...state, customLabel: payload || ''}), | ||
'FormButtons.newRecord': (state, payload) => ({ ...state, newRecord: !! payload }), | ||
'FormButtons.pristine': (state, payload) => ({ ...state, pristine: !! payload }), | ||
'FormButtons.saveable': (state, payload) => ({ ...state, saveable: !! payload }), | ||
|
||
'FormButtons.callbacks': (state, payload) => ({ ...state, ...payload }), | ||
}; | ||
|
||
|
||
ManageIQ.redux.addReducer(combineReducers({ | ||
FormButtons: function FormButtonsReducer(state = initialState, action) { | ||
|
||
if (actions[action.type]) { | ||
return actions[action.type](state, action.payload); | ||
} else if (action.type.match(/^FormButtons\./)) { | ||
console.warn(`FormButtonsReducer - unknown action type: ${action.type}`, action); | ||
} | ||
|
||
return state; | ||
}, | ||
})); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import React from 'react'; | ||
import PropTypes from 'prop-types'; | ||
|
||
import MiqButton from './miq-button'; | ||
|
||
function FormButtons(props) { | ||
let primaryTitle = props.customLabel || (props.newRecord ? __('Add') : __('Save')); | ||
let primaryHandler = (props.newRecord ? props.addClicked : props.saveClicked) || props.addClicked || props.saveClicked; | ||
|
||
let resetTitle = __("Reset"); | ||
let cancelTitle = __("Cancel"); | ||
|
||
return ( | ||
<React.Fragment> | ||
<div className="clearfix"></div> | ||
<div className="pull-right button-group edit_buttons"> | ||
<MiqButton name={primaryTitle} title={primaryTitle} enabled={props.saveable} onClick={primaryHandler} primary={true} /> | ||
{props.newRecord || <MiqButton name={resetTitle} title={resetTitle} enabled={! props.pristine} onClick={props.resetClicked} />} | ||
<MiqButton name={cancelTitle} title={cancelTitle} enabled={true} onClick={props.cancelClicked} /> | ||
|
||
</div> | ||
</React.Fragment> | ||
); | ||
} | ||
|
||
FormButtons.propTypes = { | ||
newRecord: PropTypes.bool, | ||
customLabel: PropTypes.string, | ||
saveable: PropTypes.bool, | ||
pristine: PropTypes.bool, | ||
addClicked: PropTypes.func, | ||
saveClicked: PropTypes.func, | ||
resetClicked: PropTypes.func, | ||
cancelClicked: PropTypes.func, | ||
}; | ||
|
||
const noop = () => null; | ||
|
||
FormButtons.defaultProps = { | ||
newRecord: false, | ||
customLabel: '', | ||
saveable: false, | ||
pristine: true, | ||
addClicked: noop, | ||
saveClicked: noop, | ||
resetClicked: noop, | ||
cancelClicked: noop, | ||
} | ||
|
||
export default FormButtons; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import React from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import ClassNames from 'classnames'; | ||
|
||
function MiqButton(props) { | ||
let title = props.title; | ||
if (props.enabled && props.enabledTitle) { | ||
title = props.enabledTitle; | ||
} | ||
if (! props.enabled && props.disabledTitle) { | ||
title = props.disabledTitle; | ||
} | ||
|
||
let klass = ClassNames({ | ||
'btn': true, | ||
'btn-xs': props.xs, | ||
'btn-primary': props.primary, | ||
'btn-default': !props.primary, | ||
'disabled': !props.enabled, | ||
}); | ||
|
||
let buttonClicked = (event) => { | ||
if (props.enabled) { | ||
props.onClick(); | ||
} | ||
|
||
event.preventDefault(); | ||
event.target.blur(); | ||
}; | ||
|
||
return ( | ||
<button className={klass} | ||
onClick={buttonClicked} | ||
title={title} | ||
alt={title}> | ||
{props.name} | ||
</button> | ||
); | ||
} | ||
|
||
MiqButton.propTypes = { | ||
name: PropTypes.string.isRequired, | ||
enabled: PropTypes.bool.isRequired, | ||
title: PropTypes.string, | ||
enabledTitle: PropTypes.string, | ||
disabledTitle: PropTypes.string, | ||
primary: PropTypes.bool, | ||
xs: PropTypes.bool, | ||
onClick: PropTypes.func.isRequired, | ||
}; | ||
|
||
MiqButton.defaultProps = { | ||
title: '', | ||
enabledTitle: '', | ||
disabledTitle: '', | ||
primary: false, | ||
xs: false, | ||
}; | ||
|
||
export default MiqButton; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters