index.tsx 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import React from 'react';
  2. import { Wrapper } from './styled';
  3. const properties: {[index: string]: string} = {
  4. m: 'margin',
  5. p: 'padding',
  6. d: 'display',
  7. j: 'justify-content',
  8. a: 'align-items',
  9. f: 'flex-direction',
  10. w: 'width',
  11. h: 'height',
  12. };
  13. const directions: {[index: string]: string} = {
  14. t: 'Top',
  15. r: 'Right',
  16. b: 'Bottom',
  17. l: 'Left',
  18. };
  19. type Props = {
  20. [index: string]: React.ReactNode;
  21. children: React.ReactNode;
  22. };
  23. const Box: React.FC<Props> = ({
  24. children,
  25. ...rest
  26. }: Props) => {
  27. const args = Object.keys(rest);
  28. if (!rest.width) {
  29. // eslint-disable-next-line no-param-reassign
  30. rest.width = '100%';
  31. }
  32. const styles = args.map((ele: string) => {
  33. const property = properties[ele[0]];
  34. const direction = directions[ele[1]];
  35. if (direction) {
  36. return `${property}-${direction}: ${rest[ele]}px;`;
  37. }
  38. return `${property}: ${rest[ele]};`;
  39. });
  40. return (
  41. <Wrapper
  42. styles={styles}
  43. >
  44. {children}
  45. </Wrapper>
  46. );
  47. };
  48. export default Box;