Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(box): use style-props package, create method to safelySpreadProps
Use style-props package over the types package Create a method to safely spread props onto the Box component without overriding styles from compositional use of Box
- Loading branch information
Showing
5 changed files
with
77 additions
and
7 deletions.
There are no files selected for viewing
26 changes: 26 additions & 0 deletions
26
packages/paste-core/utilities/box/__tests__/SafelySpreadProps.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import {safelySpreadBoxProps} from '../src/SafelySpreadProps'; | ||
|
||
const badProps = { | ||
foo: 'bar', | ||
borderColor: 'borderColorSuccess', | ||
}; | ||
|
||
const goodProps = { | ||
baz: 'foo', | ||
'aria-label': 'test', | ||
}; | ||
|
||
describe('safelySpreadBoxProps', () => { | ||
it('should not filter out good props', () => { | ||
expect(safelySpreadBoxProps(goodProps)).toEqual({ | ||
baz: 'foo', | ||
'aria-label': 'test', | ||
}); | ||
}); | ||
|
||
it('should filter out bad props', () => { | ||
expect(safelySpreadBoxProps(badProps)).toEqual({ | ||
foo: 'bar', | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
packages/paste-core/utilities/box/src/SafelySpreadProps.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { | ||
LAYOUT_PROPS, | ||
SPACE_PROPS, | ||
BACKGROUND_PROPS, | ||
BORDER_PROPS, | ||
SHADOW_PROPS, | ||
POSITION_PROPS, | ||
FLEXBOX_PROPS, | ||
} from '@twilio-paste/style-props'; | ||
|
||
export const BOX_PROPS_TO_BLOCK = [ | ||
...LAYOUT_PROPS, | ||
...SPACE_PROPS, | ||
...BACKGROUND_PROPS, | ||
...BORDER_PROPS, | ||
...SHADOW_PROPS, | ||
...POSITION_PROPS, | ||
...FLEXBOX_PROPS, | ||
'backgroundColor', | ||
'borderColor', | ||
'className', | ||
'style', | ||
]; | ||
|
||
export const safelySpreadBoxProps = (props: {}): {} => { | ||
// https://www.measurethat.net/Benchmarks/Show/6642/0/for-in-vs-reduce-vs-pick#latest_results_block | ||
const newList = Object.keys(props).reduce((newObj, key) => { | ||
if (!BOX_PROPS_TO_BLOCK.includes(key)) { | ||
// eslint-disable-next-line no-param-reassign | ||
newObj[key] = props[key]; | ||
} | ||
return newObj; | ||
}, {}); | ||
return newList; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,7 +9,7 @@ | |
], | ||
"references": [ | ||
{ | ||
"path": "../../../paste-types" | ||
"path": "../../../paste-style-props" | ||
} | ||
] | ||
} |