Skip to content

Commit de2780c

Browse files
committed
WIP Omits invalid props
1 parent 08e4be6 commit de2780c

File tree

7 files changed

+92
-2
lines changed

7 files changed

+92
-2
lines changed

packages/atomic-layout-core/src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,5 +52,7 @@ export {
5252
/* Utilities */
5353
export { default as warn } from './utils/functions/warn'
5454
export { default as compose } from './utils/functions/compose'
55+
export { default as omit } from './utils/functions/omit'
56+
export { default as omitProps } from './utils/functions/omit/omitProps'
5557
export { default as throttle } from './utils/functions/throttle'
5658
export { default as transformNumeric } from './utils/math/transformNumeric'
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export { default } from './omit'
2+
export * from './omit'
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import omit from './omit'
2+
3+
describe('omit', () => {
4+
describe('given an object and a set of keys to omit', () => {
5+
let result: ReturnType<typeof omit>
6+
const obj = {
7+
a: 1,
8+
b: 2,
9+
c: 3,
10+
d: 4,
11+
}
12+
13+
beforeAll(() => {
14+
result = omit(['b', 'd'], obj)
15+
})
16+
17+
it('should not include omitted keys in the result', () => {
18+
expect(result).not.toContain(['b', 'c'])
19+
})
20+
21+
it('should preserve unaffected keys', () => {
22+
expect(result).toHaveProperty('a', 1)
23+
expect(result).toHaveProperty('c', 3)
24+
})
25+
})
26+
})
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
export default function omit<R = Record<string, any>>(
2+
keys: string[],
3+
obj: R,
4+
): Omit<R, keyof typeof keys> {
5+
return Object.keys(obj).reduce<any>((acc, key) => {
6+
if (!keys.includes(key)) {
7+
acc[key] = (obj as any)[key]
8+
}
9+
10+
return acc
11+
}, {})
12+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import capitalize from '../../strings/capitalize'
2+
import Layout from '../../../Layout'
3+
import propAliases from '../../../const/propAliases'
4+
import memoizeWith from '../memoizeWith'
5+
6+
const breakpoints = Object.keys(Layout.breakpoints)
7+
const responsiveProps = Object.keys(propAliases)
8+
const allResponsiveProps = responsiveProps.reduce((acc, prop) => {
9+
return acc.concat(
10+
prop,
11+
`${prop}Down`,
12+
`${prop}Only`,
13+
...breakpoints.map((breakpointName) => {
14+
const responsivePropName = `${prop}${capitalize(breakpointName)}`
15+
return [`${responsivePropName}Down`, `${responsivePropName}Only`]
16+
}),
17+
)
18+
}, [])
19+
const regExp = new RegExp(allResponsiveProps.join('|'))
20+
21+
function omitProps<P extends Record<string, any>>(props: P) {
22+
return Object.keys(props).reduce((acc, key) => {
23+
if (!regExp.test(key)) {
24+
acc[key] = props[key]
25+
}
26+
27+
return acc
28+
}, {} as Record<string, any>)
29+
}
30+
31+
const memoizeWithPairs = memoizeWith<typeof omitProps>((props) =>
32+
Object.keys(props)
33+
.map((key) => [key, props[key]])
34+
.join(),
35+
)
36+
37+
export default memoizeWithPairs(omitProps)

packages/atomic-layout/src/components/Box.tsx

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import * as React from 'react'
22
import styled from 'styled-components'
3-
import { BoxProps, applyStyles } from '@atomic-layout/core'
3+
import { BoxProps, omitProps, applyStyles } from '@atomic-layout/core'
44

5-
const Box: React.FC<BoxProps> = styled.div<BoxProps>`
5+
const Box: React.FC<BoxProps> = styled(
6+
({ as: As = 'div', ...rest }: BoxProps) => <As {...omitProps(rest)} />,
7+
)`
68
display: ${({ flex, inline }) =>
79
flex
810
? inline
@@ -17,4 +19,12 @@ const Box: React.FC<BoxProps> = styled.div<BoxProps>`
1719
}
1820
`
1921

22+
/**
23+
* @todo Export a regular Box by default to be used by Emotion,
24+
* which does attributes clean up by default.
25+
* Export a Box with responsive props omitted for styled-components
26+
* version.
27+
*/
28+
// export const BoxWithoutAttributesPolution
29+
2030
export default Box

packages/atomic-layout/src/components/Composition.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
generateComponents,
1010
applyStyles,
1111
warn,
12+
omitProps,
1213
} from '@atomic-layout/core'
1314
import Box from './Box'
1415
import { withPlaceholder } from '../utils/withPlaceholder'

0 commit comments

Comments
 (0)