Skip to content

Commit

Permalink
feat: new getNativePropsForTnode util method
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jsamr committed Sep 11, 2021
1 parent bc37d88 commit d983d0d
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
11 changes: 7 additions & 4 deletions packages/render-html/src/helpers/getNativePropsForTNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ import { TDefaultRendererProps } from '../shared-types';
* Extract React Native props for a given {@link TNode}. Native props target
* either `Text` or `View` elements, with an optional `onPress` prop for
* interactive elements.
*
* @public
*/
export default function getNativePropsForTNode(
props: TDefaultRendererProps<TPhrasing | TText | TBlock>
): TextProps | ViewProps {
export default function getNativePropsForTNode<
T extends TPhrasing | TText | TBlock
>(props: TDefaultRendererProps<T>): T extends TBlock ? ViewProps : TextProps {
const { tnode, style, type, nativeProps, onPress } = props;
const switchProp = type === 'block' ? props.viewProps : props.textProps;
return {
const nextProps: TextProps | ViewProps = {
...(typeof onPress === 'function'
? ({ accessibilityRole: type === 'block' ? 'button' : 'link' } as const)
: null),
Expand All @@ -23,4 +25,5 @@ export default function getNativePropsForTNode(
style: [style, nativeProps?.style, switchProp.style],
testID: tnode.tagName || undefined
};
return nextProps as any;
}
1 change: 1 addition & 0 deletions packages/render-html/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ export { default as RenderHTMLConfigProvider } from './RenderHTMLConfigProvider'
export { default as RenderHTMLSource } from './RenderHTMLSource';
export { default as useInternalRenderer } from './hooks/useInternalRenderer';
export { default as useNormalizedUrl } from './hooks/useNormalizedUrl';
export { default as getNativePropsForTNode } from './helpers/getNativePropsForTNode';
export type {
InternalSpecialRenderedTag,
InternalRendererConfig
Expand Down

0 comments on commit d983d0d

Please sign in to comment.