-
Notifications
You must be signed in to change notification settings - Fork 55
/
ActionButton.connect.js
67 lines (60 loc) · 1.55 KB
/
ActionButton.connect.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import React from 'react';
import PropTypes from 'prop-types';
import cmf, { cmfConnect } from '@talend/react-cmf';
import ActionButton from '@talend/react-components/lib/Actions/ActionButton';
export function mapStateToProps(state, ownProps) {
let props = {};
if (ownProps.actionId) {
props = cmf.action.getActionInfo(
{
registry: cmf.registry.getRegistry(),
store: {
getState: () => state,
},
},
ownProps.actionId,
);
}
return props;
}
export function mergeProps(stateProps, dispatchProps, ownProps) {
const props = Object.assign({}, ownProps, stateProps, dispatchProps);
delete props.actionId;
return props;
}
export function ContainerActionButton(props) {
const newProps = Object.assign({}, props);
if (!newProps.onClick && (props.actionCreator || props.payload)) {
newProps.onClick = (event, data) => {
if (props.actionCreator) {
props.dispatchActionCreator(props.actionCreator, event, data);
} else {
props.dispatch(
Object.assign(
{
model: props.model,
},
props.payload,
),
);
}
};
}
return <ActionButton {...newProps} />;
}
ContainerActionButton.displayName = 'Container(ActionButton)';
ContainerActionButton.propTypes = {
actionCreator: PropTypes.string,
dispatch: PropTypes.func,
dispatchActionCreator: PropTypes.func,
model: PropTypes.object,
payload: PropTypes.object,
};
export default cmfConnect({
mapStateToProps,
mergeProps,
omitCMFProps: true,
withComponentRegistry: true,
withDispatch: true,
withDispatchActionCreator: true,
})(ContainerActionButton);