index.tsx 996 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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> = ({ children, ...rest }: Props) => {
  24. const args = Object.keys(rest);
  25. if (!rest.width) {
  26. // eslint-disable-next-line no-param-reassign
  27. rest.width = '100%';
  28. }
  29. const styles = args.map((ele: string) => {
  30. const property = properties[ele[0]];
  31. const direction = directions[ele[1]];
  32. if (direction) {
  33. return `${property}-${direction}: ${rest[ele]}px;`;
  34. }
  35. return `${property}: ${rest[ele]};`;
  36. });
  37. return <Wrapper styles={styles}>{children}</Wrapper>;
  38. };
  39. export default Box;