浏览代码

[appendClassName] add hoc to append className

mightyplow 6 年之前
父节点
当前提交
755a0ef656

src/dialog/Dialog.jsx → src/components/dialog/Dialog.jsx


src/dialog/dialog.css → src/components/dialog/dialog.css


src/dialog/dialogActions.js → src/components/dialog/dialogActions.js


src/dialog/dialogState.js → src/components/dialog/dialogState.js


src/overlay/Overlay.jsx → src/components/overlay/Overlay.jsx


src/overlay/overlay.css → src/components/overlay/overlay.css


src/overlayMenu/OverlayMenu.jsx → src/components/overlayMenu/OverlayMenu.jsx


src/overlayMenu/overlayMenu.css → src/components/overlayMenu/overlayMenu.css


+ 36 - 0
src/hocs/appendClassName.js

@@ -0,0 +1,36 @@
+import cx from 'classnames';
+
+function addClassNameToNode (node, addedClassName) {
+    const {attributes} = node;
+    const {className} = attributes;
+
+    return {
+        ...node,
+        attributes: {
+            ...attributes,
+            className: cx(className, addedClassName)
+        }
+    };
+}
+
+function appendClassName (component) {
+    return function ({className, ...props}, children) {
+        const rendered = component(props, children);
+
+        // handle lazy component
+        if (typeof rendered === 'function') {
+            return function (state, actions) {
+                return addClassNameToNode(
+                    rendered(state, actions),
+                    className
+                );
+            };
+        }
+
+        return addClassNameToNode(rendered, className);
+    };
+}
+
+export {
+    appendClassName
+};

+ 7 - 5
src/index.js

@@ -1,6 +1,8 @@
-export { Dialog } from './dialog/Dialog.jsx';
-export { dialogActions } from './dialog/dialogActions';
-export { dialogState } from './dialog/dialogState';
+export { Dialog } from './components/dialog/Dialog.jsx';
+export { dialogActions } from './components/dialog/dialogActions';
+export { dialogState } from './components/dialog/dialogState';
 
-export { Overlay } from './overlay/Overlay.jsx';
-export { OverlayMenu } from './overlayMenu/OverlayMenu.jsx';
+export { Overlay } from './components/overlay/Overlay.jsx';
+export { OverlayMenu } from './components/overlayMenu/OverlayMenu.jsx';
+
+export { appendClassName } from './hocs/appendClassName';