appendClassName.js 840 B

123456789101112131415161718192021222324252627282930313233343536
  1. import cx from 'classnames';
  2. function addClassNameToNode (node, addedClassName) {
  3. const {attributes} = node;
  4. const {className} = attributes;
  5. return {
  6. ...node,
  7. attributes: {
  8. ...attributes,
  9. className: cx(className, addedClassName)
  10. }
  11. };
  12. }
  13. function appendClassName (component) {
  14. return function ({className, ...props}, children) {
  15. const rendered = component(props, children);
  16. // handle lazy component
  17. if (typeof rendered === 'function') {
  18. return function (state, actions) {
  19. return addClassNameToNode(
  20. rendered(state, actions),
  21. className
  22. );
  23. };
  24. }
  25. return addClassNameToNode(rendered, className);
  26. };
  27. }
  28. export {
  29. appendClassName
  30. };