File tree Expand file tree Collapse file tree 7 files changed +92
-2
lines changed
atomic-layout/src/components Expand file tree Collapse file tree 7 files changed +92
-2
lines changed Original file line number Diff line number Diff line change @@ -52,5 +52,7 @@ export {
5252/* Utilities */
5353export { default as warn } from './utils/functions/warn'
5454export { 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'
5557export { default as throttle } from './utils/functions/throttle'
5658export { default as transformNumeric } from './utils/math/transformNumeric'
Original file line number Diff line number Diff line change 1+ export { default } from './omit'
2+ export * from './omit'
Original file line number Diff line number Diff line change 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+ } )
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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 )
Original file line number Diff line number Diff line change 11import * as React from 'react'
22import 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+
2030export default Box
Original file line number Diff line number Diff line change 99 generateComponents ,
1010 applyStyles ,
1111 warn ,
12+ omitProps ,
1213} from '@atomic-layout/core'
1314import Box from './Box'
1415import { withPlaceholder } from '../utils/withPlaceholder'
You can’t perform that action at this time.
0 commit comments