Skip to content

Commit d983d0d

Browse files
committed
feat: new getNativePropsForTnode util method
This method is useful for custom renderers where `TDefaultRenderer` cannot be used as a root (e.g., you need a ScrollView). You can now extract the props that would otherwise be passed to the underlying `Text` or `View` from `TDefaultRenderer` thanks to this function.
1 parent bc37d88 commit d983d0d

File tree

2 files changed

+8
-4
lines changed

2 files changed

+8
-4
lines changed

packages/render-html/src/helpers/getNativePropsForTNode.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,15 @@ import { TDefaultRendererProps } from '../shared-types';
66
* Extract React Native props for a given {@link TNode}. Native props target
77
* either `Text` or `View` elements, with an optional `onPress` prop for
88
* interactive elements.
9+
*
10+
* @public
911
*/
10-
export default function getNativePropsForTNode(
11-
props: TDefaultRendererProps<TPhrasing | TText | TBlock>
12-
): TextProps | ViewProps {
12+
export default function getNativePropsForTNode<
13+
T extends TPhrasing | TText | TBlock
14+
>(props: TDefaultRendererProps<T>): T extends TBlock ? ViewProps : TextProps {
1315
const { tnode, style, type, nativeProps, onPress } = props;
1416
const switchProp = type === 'block' ? props.viewProps : props.textProps;
15-
return {
17+
const nextProps: TextProps | ViewProps = {
1618
...(typeof onPress === 'function'
1719
? ({ accessibilityRole: type === 'block' ? 'button' : 'link' } as const)
1820
: null),
@@ -23,4 +25,5 @@ export default function getNativePropsForTNode(
2325
style: [style, nativeProps?.style, switchProp.style],
2426
testID: tnode.tagName || undefined
2527
};
28+
return nextProps as any;
2629
}

packages/render-html/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ export { default as RenderHTMLConfigProvider } from './RenderHTMLConfigProvider'
131131
export { default as RenderHTMLSource } from './RenderHTMLSource';
132132
export { default as useInternalRenderer } from './hooks/useInternalRenderer';
133133
export { default as useNormalizedUrl } from './hooks/useNormalizedUrl';
134+
export { default as getNativePropsForTNode } from './helpers/getNativePropsForTNode';
134135
export type {
135136
InternalSpecialRenderedTag,
136137
InternalRendererConfig

0 commit comments

Comments
 (0)