Skip to content

Commit 7dc5577

Browse files
committed
feat: support onPress from element models native props
1 parent 31241fb commit 7dc5577

File tree

6 files changed

+28
-18
lines changed

6 files changed

+28
-18
lines changed

packages/render-html/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@
9191
"dependencies": {
9292
"@jsamr/counter-style": "^2.0.1",
9393
"@jsamr/react-native-li": "^2.3.0",
94-
"@native-html/transient-render-engine": "10.3.0",
94+
"@native-html/transient-render-engine": "10.4.0",
9595
"@types/ramda": "^0.27.40",
9696
"@types/urijs": "^1.19.15",
9797
"prop-types": "^15.5.7",

packages/render-html/src/TBlockRenderer.tsx

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,11 @@ export const TDefaultBlockRenderer: TDefaultRenderer<TBlock> = ({
2020
propsForChildren={props.propsForChildren}
2121
/>
2222
);
23-
const commonProps = getNativePropsForTNode(props);
24-
if (typeof onPress === 'function') {
25-
return React.createElement(
26-
GenericPressable,
27-
{ onPress, ...commonProps },
28-
children
29-
);
23+
const nativeProps = getNativePropsForTNode(props);
24+
if (typeof nativeProps.onPress === 'function') {
25+
return React.createElement(GenericPressable, nativeProps, children);
3026
}
31-
return React.createElement(View, commonProps, children);
27+
return React.createElement(View, nativeProps, children);
3228
};
3329

3430
const TBlockRenderer = (props: TNodeSubRendererProps<TBlock>) => {

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

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import { TBlock, TPhrasing, TText } from '@native-html/transient-render-engine';
22
import { TextProps, ViewProps } from 'react-native';
33
import { TDefaultRendererProps } from '../shared-types';
44

5+
const empty: any = {};
6+
57
/**
68
* Extract React Native props for a given {@link TNode}. Native props target
79
* either `Text` or `View` elements, with an optional `onPress` prop for
@@ -11,14 +13,20 @@ import { TDefaultRendererProps } from '../shared-types';
1113
*/
1214
export default function getNativePropsForTNode<
1315
T extends TPhrasing | TText | TBlock
14-
>(props: TDefaultRendererProps<T>): T extends TBlock ? ViewProps : TextProps {
16+
>(
17+
props: TDefaultRendererProps<T>
18+
): T extends TBlock ? ViewProps & { onPress?: () => void } : TextProps {
1519
const { tnode, style, type, nativeProps, onPress } = props;
1620
const switchProp = type === 'block' ? props.viewProps : props.textProps;
21+
const propsFromModel =
22+
tnode.getReactNativeProps()?.[type === 'block' ? 'view' : 'text'] || empty;
23+
const syntheticOnPress =
24+
onPress ?? nativeProps?.onPress ?? propsFromModel.onPress;
1725
const nextProps: TextProps | ViewProps = {
18-
...(typeof onPress === 'function'
26+
...(typeof syntheticOnPress === 'function'
1927
? ({ accessibilityRole: type === 'block' ? 'button' : 'link' } as const)
2028
: null),
21-
...tnode.getReactNativeProps()?.[type === 'block' ? 'view' : 'text'],
29+
...propsFromModel,
2230
...nativeProps,
2331
...switchProp,
2432
onPress,

packages/render-html/src/index.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,15 @@ export type {
8484
NativeShortKeys,
8585
NativeTextStyleKey,
8686
NativeTextStyles,
87+
ReactNativeProps,
88+
ReactNativePropsDefinitions,
89+
ReactNativePropsSwitch,
8790
SectioningTagNames,
8891
SetMarkersForTNode,
8992
StylesConfig,
93+
StylessReactNativeProps,
94+
StylessReactNativeTextProps,
95+
StylessReactNativeViewProps,
9096
TabularTagNames,
9197
TagName,
9298
TBlock,

packages/render-html/src/shared-types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1061,8 +1061,8 @@ export interface RendererBaseProps<T extends TNode>
10611061
* nativeProps.style, viewProps.style]}`.
10621062
*
10631063
*/
1064-
10651064
nativeProps?: StylessReactNativeProps & { style?: StyleProp<ViewStyle> };
1065+
10661066
/**
10671067
* Any default renderer should be able to handle press.
10681068
*/

yarn.lock

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4380,9 +4380,9 @@ __metadata:
43804380
languageName: node
43814381
linkType: hard
43824382

4383-
"@native-html/transient-render-engine@npm:10.3.0":
4384-
version: 10.3.0
4385-
resolution: "@native-html/transient-render-engine@npm:10.3.0"
4383+
"@native-html/transient-render-engine@npm:10.4.0":
4384+
version: 10.4.0
4385+
resolution: "@native-html/transient-render-engine@npm:10.4.0"
43864386
dependencies:
43874387
"@native-html/css-processor": 1.11.0
43884388
"@types/ramda": ^0.27.44
@@ -4395,7 +4395,7 @@ __metadata:
43954395
peerDependencies:
43964396
"@types/react-native": "*"
43974397
react-native: ^*
4398-
checksum: 60a642e113ccde5f557e8c5a065b162c830107a8cdaddb2b68b36047dfb350236b95ed9c06858ea92be5354d29360791d0860250c54431c3d6e2754ed43b3d9e
4398+
checksum: 28883f81d5b7aa0af740dec5cb150f96ec23ba388b018e31c71abcee7b545073903f5c8fe93589215129ea2dc0382dbdf913c95ca607a5ca3394787570d04db3
43994399
languageName: node
44004400
linkType: hard
44014401

@@ -20045,7 +20045,7 @@ fsevents@^1.2.7:
2004520045
"@jsamr/counter-style": ^2.0.1
2004620046
"@jsamr/react-native-li": ^2.3.0
2004720047
"@microsoft/api-extractor": ^7.14.0
20048-
"@native-html/transient-render-engine": 10.3.0
20048+
"@native-html/transient-render-engine": 10.4.0
2004920049
"@release-it/conventional-changelog": ^2.0.1
2005020050
"@testing-library/jest-native": ^4.0.2
2005120051
"@testing-library/react-hooks": ^7.0.0

0 commit comments

Comments
 (0)