Skip to content

Commit 90a26ef

Browse files
committed
accept renderer properties in separate prop
1 parent 9dd6633 commit 90a26ef

File tree

2 files changed

+40
-17
lines changed

2 files changed

+40
-17
lines changed

src/scripts/Tabs.tsx

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -106,32 +106,37 @@ export type TabItemRendererProps = {
106106
menuIcon?: string;
107107
eventKey?: TabKey;
108108
activeKey?: TabKey;
109+
activeTabRef?: Ref<HTMLAnchorElement>;
110+
children?: ReactNode;
109111
onTabClick?: (eventKey: TabKey) => void;
110112
onTabKeyDown?: (
111113
eventKey: TabKey,
112114
e: React.KeyboardEvent<HTMLAnchorElement>
113115
) => void;
114116
};
115117

116-
const DefaultTabItemRenderer: FC<TabItemRendererProps> = (props) => {
118+
const DefaultTabItemRenderer: FC = (props) => {
117119
const el = React.Children.only(props.children);
118120
return React.isValidElement(el) ? el : <>{el}</>;
119121
};
120122

121123
/**
122124
*
123125
*/
124-
export type TabItemProps = {
125-
tabItemRenderer?: ComponentType<TabItemRendererProps>;
126+
export type TabItemProps<RendererProps extends TabItemRendererProps> = {
127+
tabItemRenderer?: ComponentType<RendererProps>;
128+
rendererProps?: Omit<RendererProps, keyof TabItemRendererProps>;
126129
} & Omit<
127130
TabItemRendererProps,
128-
'type' | 'activeKey' | 'onTabClick' | 'onTabKeyDown'
131+
'type' | 'activeKey' | 'activeTabRef' | 'onTabClick' | 'onTabKeyDown'
129132
>;
130133

131134
/**
132135
*
133136
*/
134-
const TabItem: FC<TabItemProps> = (props) => {
137+
const TabItem = <RendererProps extends TabItemRendererProps>(
138+
props: TabItemProps<RendererProps>
139+
) => {
135140
const { title, eventKey, menu, menuIcon } = props;
136141
const { type, activeTabRef } = useContext(TabsContext);
137142
const activeKey = useContext(TabsActiveKeyContext);
@@ -153,14 +158,21 @@ const TabItem: FC<TabItemProps> = (props) => {
153158
const tabLinkClassName = `slds-tabs_${type}__link`;
154159
const {
155160
tabItemRenderer: TabItemRenderer = DefaultTabItemRenderer,
161+
rendererProps,
156162
...rprops
157163
} = props;
164+
const itemRendererProps = {
165+
...rendererProps,
166+
...rprops,
167+
type,
168+
activeKey,
169+
activeTabRef,
170+
onTabClick,
171+
onTabKeyDown,
172+
} as RendererProps;
158173
return (
159174
<li className={tabItemClassName} role='presentation'>
160-
<TabItemRenderer
161-
{...rprops}
162-
{...{ type, activeKey, onTabClick, onTabKeyDown }}
163-
>
175+
<TabItemRenderer {...itemRendererProps}>
164176
<span className='react-slds-tab-item-content'>
165177
<a
166178
className={tabLinkClassName}
@@ -210,12 +222,17 @@ const TabNav: FC = (props) => {
210222
/**
211223
*
212224
*/
213-
export type TabProps = {
225+
export type TabProps<RendererProps> = {
214226
className?: string;
215227
eventKey?: TabKey;
216-
} & TabItemProps;
228+
children?: ReactNode;
229+
} & TabItemProps<RendererProps>;
217230

218-
export const Tab: FC<TabProps> = (props) => {
231+
export const Tab = <
232+
RendererProps extends TabItemRendererProps = TabItemRendererProps
233+
>(
234+
props: TabProps<RendererProps>
235+
) => {
219236
const { className, eventKey, children } = props;
220237
const activeKey = useContext(TabsActiveKeyContext);
221238
return (

stories/Tabs.stories.tsx

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ function createMenu() {
1818
/**
1919
*
2020
*/
21-
function CustomTabItemContent(props: TabItemRendererProps) {
21+
function CustomTabItemContent(props: TabItemRendererProps & { icon: string }) {
2222
const {
2323
activeKey,
2424
activeTabRef,
@@ -44,7 +44,7 @@ function CustomTabItemContent(props: TabItemRendererProps) {
4444
onTabKeyDown && eventKey != null && onTabKeyDown(eventKey, e)
4545
}
4646
>
47-
<Icon icon={icon as string} size='small' />
47+
<Icon icon={icon} size='small' />
4848
<span className='slds-p-horizontal_x-small'>{title}</span>
4949
</a>
5050
);
@@ -199,24 +199,30 @@ export const CustomTabItem: ComponentStoryObj<typeof Tabs> = {
199199
<Tab
200200
eventKey='1'
201201
title='Tab 1'
202-
icon='standard:account'
203202
tabItemRenderer={CustomTabItemContent}
203+
rendererProps={{
204+
icon: 'standard:account',
205+
}}
204206
>
205207
This is in tab #1
206208
</Tab>
207209
<Tab
208210
eventKey='2'
209211
title='Tab 2'
210-
icon='standard:contact'
211212
tabItemRenderer={CustomTabItemContent}
213+
rendererProps={{
214+
icon: 'standard:contact',
215+
}}
212216
>
213217
This is in tab #2
214218
</Tab>
215219
<Tab
216220
eventKey='3'
217221
title='Tab 3'
218-
icon='standard:opportunity'
219222
tabItemRenderer={CustomTabItemContent}
223+
rendererProps={{
224+
icon: 'standard:opportunity',
225+
}}
220226
>
221227
This is in tab #3
222228
</Tab>

0 commit comments

Comments
 (0)