-
Notifications
You must be signed in to change notification settings - Fork 314
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Forward ref support for TableRow, Tabs, ToggleButton & ToggleButtonGroup #861
Changes from 5 commits
479727d
5d8287e
d118592
0c90d8e
441cf95
14a1ad6
aabc200
257a749
addb5db
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
"@aws-amplify/ui-react": patch | ||
"@aws-amplify/ui": patch | ||
--- | ||
|
||
Forward ref support for TableRow, Tabs, ToggleButton & ToggleButtonGroup |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,24 @@ | ||
import classNames from 'classnames'; | ||
import * as React from 'react'; | ||
|
||
import { ComponentClassNames } from '../shared/constants'; | ||
import { Primitive, TableRowProps } from '../types'; | ||
import { PrimitiveWithForwardRef, TableRowProps } from '../types'; | ||
import { View } from '../View'; | ||
|
||
export const TableRow: Primitive<TableRowProps, 'tr'> = ({ | ||
children, | ||
className, | ||
...rest | ||
}) => ( | ||
const TableRowPrimitive: PrimitiveWithForwardRef<TableRowProps, 'tr'> = ( | ||
{ children, className, ...rest }, | ||
ref | ||
) => ( | ||
<View | ||
as="tr" | ||
className={classNames(ComponentClassNames.TableRow, className)} | ||
ref={ref} | ||
{...rest} | ||
> | ||
{children} | ||
</View> | ||
); | ||
|
||
export const TableRow = React.forwardRef(TableRowPrimitive); | ||
|
||
TableRow.displayName = 'TableRow'; |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -1,16 +1,16 @@ | ||||||
import * as React from 'react'; | ||||||
import classNames from 'classnames'; | ||||||
import { | ||||||
Root, | ||||||
List, | ||||||
Trigger as RadixTab, | ||||||
Content as Panel, | ||||||
} from '@radix-ui/react-tabs'; | ||||||
import { TabsProps, TabItemProps } from '../types'; | ||||||
import * as React from 'react'; | ||||||
|
||||||
import { Flex } from '../Flex'; | ||||||
import { View } from '../View'; | ||||||
import { TabsProps, TabItemProps, PrimitiveWithForwardRef } from '../types'; | ||||||
import { ComponentClassNames } from '../shared/constants'; | ||||||
import classNames from 'classnames'; | ||||||
import { convertStylePropsToStyleObj, prefixer } from '../shared/styleUtils'; | ||||||
import { Primitive } from '../types'; | ||||||
|
||||||
const isTabsType = (child: any): child is React.Component<TabItemProps> => { | ||||||
return ( | ||||||
|
@@ -21,29 +21,26 @@ const isTabsType = (child: any): child is React.Component<TabItemProps> => { | |||||
); | ||||||
}; | ||||||
|
||||||
export const Tabs: Primitive<TabsProps, typeof Flex> = ({ | ||||||
alignContent, | ||||||
alignItems, | ||||||
ariaLabel, | ||||||
children, | ||||||
className, | ||||||
defaultIndex = 0, | ||||||
currentIndex, | ||||||
onChange, | ||||||
indicatorPosition, | ||||||
direction, | ||||||
gap = '0', | ||||||
spacing, | ||||||
justifyContent, | ||||||
wrap, | ||||||
...rest | ||||||
}: TabsProps) => { | ||||||
const TabsPrimitive: PrimitiveWithForwardRef<TabsProps, typeof Flex> = ( | ||||||
{ | ||||||
ariaLabel, | ||||||
children, | ||||||
className, | ||||||
defaultIndex = 0, | ||||||
currentIndex, | ||||||
onChange, | ||||||
indicatorPosition, | ||||||
spacing, | ||||||
...rest | ||||||
}: TabsProps, | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
ref | ||||||
) => { | ||||||
const tabs = React.Children.map(children, (child) => { | ||||||
if (!isTabsType(child)) { | ||||||
console.warn( | ||||||
'Amplify UI: <Tabs> component only accepts <TabItem> as children.' | ||||||
); | ||||||
return null; | ||||||
return {}; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||||||
} | ||||||
|
||||||
return child.props; | ||||||
|
@@ -58,33 +55,26 @@ export const Tabs: Primitive<TabsProps, typeof Flex> = ({ | |||||
value: currentIndex != null ? currentIndex.toString() : undefined, | ||||||
onValueChange: onChange, | ||||||
}; | ||||||
|
||||||
return ( | ||||||
<Root {...rootProps}> | ||||||
<List aria-label={ariaLabel}> | ||||||
<Flex | ||||||
alignContent={alignContent} | ||||||
alignItems={alignItems} | ||||||
className={classNames(ComponentClassNames.Tabs, className)} | ||||||
direction={direction} | ||||||
gap={gap} | ||||||
justifyContent={justifyContent} | ||||||
wrap={wrap} | ||||||
data-indicator-position={indicatorPosition} | ||||||
ref={ref} | ||||||
{...rest} | ||||||
> | ||||||
{tabs.map(({ className, isDisabled, title, ...rest }, index) => ( | ||||||
<RadixTab | ||||||
className={classNames(ComponentClassNames.TabItems, className)} | ||||||
data-spacing={spacing} | ||||||
disabled={isDisabled} | ||||||
key={index} | ||||||
style={prefixer(convertStylePropsToStyleObj(rest))} | ||||||
value={`${index}`} | ||||||
> | ||||||
{title} | ||||||
</RadixTab> | ||||||
))} | ||||||
{React.Children.map(children, (child, index) => { | ||||||
if (!isTabsType(child)) { | ||||||
return null; | ||||||
} | ||||||
|
||||||
return React.cloneElement(child, { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. By cloning, we are able to clone the |
||||||
['data-spacing']: spacing, | ||||||
key: index, | ||||||
value: `${index}`, | ||||||
}); | ||||||
})} | ||||||
</Flex> | ||||||
</List> | ||||||
{tabs.map((tab, index) => ( | ||||||
|
@@ -96,7 +86,22 @@ export const Tabs: Primitive<TabsProps, typeof Flex> = ({ | |||||
); | ||||||
}; | ||||||
|
||||||
export const TabItem: React.FC<TabItemProps> = () => <></>; | ||||||
const TabItemPrimitvie: PrimitiveWithForwardRef<TabItemProps, 'div'> = ( | ||||||
zchenwei marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
{ className, title, ...rest }, | ||||||
ref | ||||||
) => ( | ||||||
<View | ||||||
as={RadixTab} | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Woah, so cool that this works! |
||||||
className={classNames(ComponentClassNames.TabItems, className)} | ||||||
ref={ref} | ||||||
{...rest} | ||||||
> | ||||||
{title} | ||||||
</View> | ||||||
); | ||||||
|
||||||
export const Tabs = React.forwardRef(TabsPrimitive); | ||||||
export const TabItem = React.forwardRef(TabItemPrimitvie); | ||||||
|
||||||
Tabs.displayName = 'Tabs'; | ||||||
TabItem.displayName = 'TabItem'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for cleaning this up