utils.js 1.3 KB

1234567891011121314151617181920212223242526272829
  1. import classNames from 'classnames';
  2. export const flexWrapValues = ['wrap', 'nowrap', 'wrap-reverse'];
  3. export const justifyContentValues = ['flex-start', 'flex-end', 'start', 'end', 'center', 'space-between', 'space-around', 'space-evenly', 'stretch', 'normal', 'left', 'right'];
  4. export const alignItemsValues = ['center', 'start', 'end', 'flex-start', 'flex-end', 'self-start', 'self-end', 'baseline', 'normal', 'stretch'];
  5. const genClsWrap = (prefixCls, props) => {
  6. const wrap = props.wrap === true ? 'wrap' : props.wrap;
  7. return {
  8. [`${prefixCls}-wrap-${wrap}`]: wrap && flexWrapValues.includes(wrap)
  9. };
  10. };
  11. const genClsAlign = (prefixCls, props) => {
  12. const alignCls = {};
  13. alignItemsValues.forEach(cssKey => {
  14. alignCls[`${prefixCls}-align-${cssKey}`] = props.align === cssKey;
  15. });
  16. alignCls[`${prefixCls}-align-stretch`] = !props.align && !!props.vertical;
  17. return alignCls;
  18. };
  19. const genClsJustify = (prefixCls, props) => {
  20. const justifyCls = {};
  21. justifyContentValues.forEach(cssKey => {
  22. justifyCls[`${prefixCls}-justify-${cssKey}`] = props.justify === cssKey;
  23. });
  24. return justifyCls;
  25. };
  26. function createFlexClassNames(prefixCls, props) {
  27. return classNames(Object.assign(Object.assign(Object.assign({}, genClsWrap(prefixCls, props)), genClsAlign(prefixCls, props)), genClsJustify(prefixCls, props)));
  28. }
  29. export default createFlexClassNames;