forked from deephaven/web-client-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
separateSpectrumProps util (deephaven#1890)
- Loading branch information
Showing
4 changed files
with
196 additions
and
49 deletions.
There are no files selected for viewing
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
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,74 @@ | ||
import { AriaLabelingProps, StyleProps } from '@react-types/shared'; | ||
import { separateSpectrumProps } from './propsUtils'; | ||
|
||
describe('separateSpectrumProps', () => { | ||
const mockAriaLabelProps: AriaLabelingProps = { | ||
'aria-label': 'test', | ||
'aria-labelledby': 'testId', | ||
'aria-describedby': 'testDesc', | ||
'aria-details': 'testDetails', | ||
}; | ||
|
||
const mockStyleProps: StyleProps = { | ||
marginX: '10px', | ||
marginY: '20px', | ||
width: '100px', | ||
height: '200px', | ||
minWidth: '50px', | ||
minHeight: '100px', | ||
maxWidth: '150px', | ||
maxHeight: '200px', | ||
flex: '1 1 auto', | ||
flexGrow: 1, | ||
flexShrink: 1, | ||
flexBasis: 'auto', | ||
justifySelf: 'center', | ||
alignSelf: 'center', | ||
order: 1, | ||
gridArea: '1 / 1 / 2 / 2', | ||
gridColumn: '1 / span 2', | ||
gridRow: '1 / span 2', | ||
gridColumnStart: '1', | ||
gridColumnEnd: '3', | ||
gridRowStart: '1', | ||
gridRowEnd: '3', | ||
position: 'relative', | ||
zIndex: 1, | ||
top: '10px', | ||
bottom: '10px', | ||
start: '10px', | ||
end: '10px', | ||
left: '10px', | ||
right: '10px', | ||
isHidden: false, | ||
}; | ||
|
||
const mockComponentProps = { | ||
otherPropA: 'otherValue', | ||
otherPropB: 999, | ||
} as const; | ||
|
||
it('should separate aria, style, and component properties', () => { | ||
const props = { | ||
...mockAriaLabelProps, | ||
...mockStyleProps, | ||
...mockComponentProps, | ||
}; | ||
|
||
const { ariaLabelProps, styleProps, componentProps } = | ||
separateSpectrumProps(props); | ||
|
||
expect(ariaLabelProps).toEqual(mockAriaLabelProps); | ||
expect(styleProps).toEqual(mockStyleProps); | ||
expect(componentProps).toEqual(mockComponentProps); | ||
}); | ||
|
||
it('should return empty objects if no props are passed', () => { | ||
const { ariaLabelProps, styleProps, componentProps } = | ||
separateSpectrumProps({}); | ||
|
||
expect(ariaLabelProps).toEqual({}); | ||
expect(styleProps).toEqual({}); | ||
expect(componentProps).toEqual({}); | ||
}); | ||
}); |
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,103 @@ | ||
import type { AriaLabelingProps, StyleProps } from '@react-types/shared'; | ||
|
||
/** | ||
* Separate props for Spectrum components into AriaLabelingProps, StyleProps, and | ||
* any remaining props. | ||
* @param props The props to separate | ||
* @returns The separated props | ||
*/ | ||
export function separateSpectrumProps<T extends AriaLabelingProps & StyleProps>( | ||
props: T | ||
): { | ||
ariaLabelProps: AriaLabelingProps; | ||
styleProps: StyleProps; | ||
componentProps: Omit<T, keyof (AriaLabelingProps & StyleProps)>; | ||
} { | ||
const { | ||
'aria-label': ariaLabel, | ||
'aria-labelledby': ariaLabelledby, | ||
'aria-describedby': ariaDescribedby, | ||
'aria-details': ariaHidden, | ||
|
||
marginX, | ||
marginY, | ||
width, | ||
height, | ||
minWidth, | ||
minHeight, | ||
maxWidth, | ||
maxHeight, | ||
flex, | ||
flexGrow, | ||
flexShrink, | ||
flexBasis, | ||
justifySelf, | ||
alignSelf, | ||
order, | ||
gridArea, | ||
gridColumn, | ||
gridRow, | ||
gridColumnStart, | ||
gridColumnEnd, | ||
gridRowStart, | ||
gridRowEnd, | ||
position, | ||
zIndex, | ||
top, | ||
bottom, | ||
start, | ||
end, | ||
left, | ||
right, | ||
isHidden, | ||
...restProps | ||
} = props; | ||
|
||
return { | ||
ariaLabelProps: { | ||
'aria-label': ariaLabel, | ||
'aria-labelledby': ariaLabelledby, | ||
'aria-describedby': ariaDescribedby, | ||
'aria-details': ariaHidden, | ||
}, | ||
styleProps: { | ||
marginX, | ||
marginY, | ||
width, | ||
height, | ||
minWidth, | ||
minHeight, | ||
maxWidth, | ||
maxHeight, | ||
flex, | ||
flexGrow, | ||
flexShrink, | ||
flexBasis, | ||
justifySelf, | ||
alignSelf, | ||
order, | ||
gridArea, | ||
gridColumn, | ||
gridRow, | ||
gridColumnStart, | ||
gridColumnEnd, | ||
gridRowStart, | ||
gridRowEnd, | ||
position, | ||
zIndex, | ||
top, | ||
bottom, | ||
start, | ||
end, | ||
left, | ||
right, | ||
isHidden, | ||
}, | ||
componentProps: restProps as Omit< | ||
T, | ||
keyof (AriaLabelingProps & StyleProps) | ||
>, | ||
}; | ||
} | ||
|
||
export default separateSpectrumProps; |