Skip to content

Commit 4f7ea59

Browse files
Add support children for Custom component, Add setSubscribePropsHandler in HOC withStore
1 parent 7db0393 commit 4f7ea59

File tree

12 files changed

+193
-23
lines changed

12 files changed

+193
-23
lines changed

CHANGELOG.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
1+
## 0.0.62
2+
3+
- Add support children for `Custom` component
4+
- Add `setSubscribePropsHandler` in HOC withStore
5+
16
## 0.0.61
27

3-
- Remove def padding for cell.
8+
- Remove def padding for cell.
49
Need add `rt-table-cell` in `cellRenderer` for add padding
510
- Add `Table` props `cellBordered`, `rowBordered`, `editMode`, `value` and `onChange`
611
- Add `setSubscribeProps` from `Table`
7-
- Add dispatch new events to `${dispatchPath}.events.${nameEvent}`:
12+
- Add dispatch new events to `${dispatchPath}.events.${nameEvent}`:
813
- `onAddRows`
914
- `onAddRow`
1015
- `onAddRowAsCopy`

dist/index.es.js

Lines changed: 52 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.es.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.js

Lines changed: 52 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/components/Base/index.js

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,27 @@ var Button = function Button(props) {
6565

6666
/** Custom компонент */
6767
var Custom = function Custom(props) {
68-
return renderClassic(props.render)(_extends({}, props, { componentType: 'Custom' }));
68+
var children = props.children;
69+
70+
var childNode = null;
71+
var childProps = null;
72+
73+
if (Array.isArray(children)) {
74+
console.warn('Custom component: `children` is array. Don\'t render component');
75+
return null;
76+
} else if (props.render) {
77+
// console.log('childNode = props.render')
78+
childNode = props.render;
79+
childProps = _extends({}, props, { componentType: 'Custom' });
80+
return renderClassic(childNode)(childProps);
81+
} else if (React.isValidElement(children)) {
82+
// console.log('childNode = children')
83+
childProps = _extends({}, children.props, props, { componentType: 'Custom' });
84+
return React.cloneElement(children, childProps);
85+
} else {
86+
console.warn('Custom component: not exist valid render');
87+
return null;
88+
}
6989
};
7090

7191
/** Компонент заголовка формы */

lib/components/Base/withStore.js

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ var _extends = Object.assign || function (target) { for (var i = 1; i < argument
44

55
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
66

7+
function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; }
8+
79
function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }
810

911
import React, { useState, useEffect } from 'react';
@@ -13,7 +15,7 @@ import objectPath from "object-path";
1315
import { setDateStore } from "../../redux/rtd.actions";
1416
import { dispatchToStore, getObjectExcludedProps, useMounted } from "../utils/baseUtils";
1517

16-
export var withStore = function withStore(Component, antFormItemProps) {
18+
var withStore = function withStore(Component, antFormItemProps) {
1719

1820
var mapStateToProps = function mapStateToProps(store, ownProps) {
1921
var subscribe = ownProps.subscribe,
@@ -75,12 +77,21 @@ export var withStore = function withStore(Component, antFormItemProps) {
7577

7678
var isMounted = useMounted();
7779

80+
var setSubscribePropsHandler = function setSubscribePropsHandler(value) {
81+
return setSubscribeProps(function (prevState) {
82+
return _extends({}, prevState, value);
83+
});
84+
};
85+
7886
/** Подписка на изменение props[subscribe.name] в сторе */
7987
subscribe.map(function (item) {
8088
return useEffect(function () {
8189
if (isMounted && item.name) {
8290
// console.log("storeHOC => subscribe: ", props[subscribe.name]);
83-
item.onChange && item.onChange({ value: props[item.name], extraData: props[item.name + 'ExtraData'], setSubscribeProps: setSubscribeProps });
91+
item.onChange && item.onChange({
92+
value: props[item.name],
93+
extraData: props[item.name + 'ExtraData'],
94+
setSubscribeProps: setSubscribePropsHandler });
8495
}
8596
// console.log("Change Props[2]: ", props.subscribeЗф);
8697
}, [props[item.name]]);
@@ -107,13 +118,20 @@ export var withStore = function withStore(Component, antFormItemProps) {
107118
}, [subscribeProps.value]);
108119

109120
var onChange = function onChange() {
110-
// console.log('withStore [trigger] ', props.componentType)
121+
// console.log('withStore [trigger] ', subscribeProps)
111122
// const newValue = getValue(...args);
112123
// dispatchPath && props.setDateStore && props.setDateStore(dispatchPath, newValue);
113124
if (componentType === 'Button') dispatchToStore({ dispatch: dispatch, setDateStore: setDateStore, value: arguments.length <= 0 ? undefined : arguments[0], extraData: dispatchExtraData });
114125
// else if(componentType === 'Search')
115126
// args[1].preventDefault();
116127

128+
if (subscribeProps && subscribeProps.hasOwnProperty('value')) {
129+
var value = subscribeProps.value,
130+
_subscribeProps = _objectWithoutProperties(subscribeProps, ['value']);
131+
132+
setSubscribePropsHandler(_subscribeProps);
133+
}
134+
117135
props[trigger] && props[trigger].apply(props, arguments);
118136
};
119137

@@ -132,4 +150,5 @@ export var withStore = function withStore(Component, antFormItemProps) {
132150
props.children
133151
);
134152
});
135-
};
153+
};
154+
export { withStore };

lib/redux/rtd.reducer.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ var rtdReducer = function rtdReducer() {
2727
var _newState = _extends({}, state);
2828
objectPath.set(_newState, _path, row); // obj.a is now {}
2929

30-
console.log("Store change: ", _path, row);
30+
console.debug("Store change: ", _path, row);
3131
// console.group("Store");
3232
// console.log("Store: ", newState);
3333
// console.log("New Data: ", path, row);

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "rt-design",
3-
"version": "0.0.61",
3+
"version": "0.0.62",
44
"description": "React technical design library",
55
"author": "Iron tech space",
66
"license": "MIT",

src/components/Base/index.js

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,26 @@ const Button = (props) => {
4343

4444
/** Custom компонент */
4545
const Custom = (props) => {
46-
return renderClassic(props.render)({...props, componentType: 'Custom' })
46+
const {children} = props
47+
let childNode = null;
48+
let childProps = null;
49+
50+
if (Array.isArray(children)) {
51+
console.warn('Custom component: `children` is array. Don\'t render component')
52+
return null;
53+
} else if(props.render) {
54+
// console.log('childNode = props.render')
55+
childNode = props.render;
56+
childProps = { ...props, componentType: 'Custom' };
57+
return renderClassic(childNode)(childProps)
58+
} else if(React.isValidElement(children)) {
59+
// console.log('childNode = children')
60+
childProps = { ...children.props, ...props, componentType: 'Custom' };
61+
return React.cloneElement(children, childProps);
62+
} else {
63+
console.warn('Custom component: not exist valid render')
64+
return null;
65+
}
4766
}
4867

4968
/** Компонент заголовка формы */

0 commit comments

Comments
 (0)