From 827b95ce3f44dfe49a6f56dd1b8574da14bba4f2 Mon Sep 17 00:00:00 2001 From: Begench Geldyev Date: Fri, 20 Nov 2026 16:13:08 +0700 Subject: [PATCH] fix: Render interpolated text in native views --- .../modules/createStrictDOMComponent.js | 26 ++++++- .../tests/html/html-test.native.js | 68 +++++++++++++++++++ 2 files changed, 92 insertions(+), 2 deletions(-) diff --git a/packages/react-strict-dom/src/native/modules/createStrictDOMComponent.js b/packages/react-strict-dom/src/native/modules/createStrictDOMComponent.js index 3290355c..380015e7 100644 --- a/packages/react-strict-dom/src/native/modules/createStrictDOMComponent.js +++ b/packages/react-strict-dom/src/native/modules/createStrictDOMComponent.js @@ -34,6 +34,27 @@ const AnimatedPressable = ReactNative.Animated.createAnimatedComponent( ReactNative.Pressable ); +function getTextContent(children: React.Node): ?string { + if (typeof children === 'string') { + return children; + } + if (typeof children === 'number') { + return String(children); + } + if (!Array.isArray(children)) { + return null; + } + let textContent: ?string = null; + for (const child of children) { + if (typeof child === 'string' || typeof child === 'number') { + textContent = (textContent ?? '') + String(child); + } else if (child != null && typeof child !== 'boolean') { + return null; + } + } + return textContent; +} + // Plain (non-hook) helper so it can mutate the caller-owned `nativeProps` // without a defensive copy (a hook may not mutate its own return value). function applyViewProps( @@ -67,8 +88,9 @@ function applyViewProps( // Workaround: React Native doesn't support raw text children of View // Sometimes we can auto-fix this - if (typeof nativeProps.children === 'string') { - nativeProps.children = ; + const textContent = getTextContent(nativeProps.children); + if (textContent != null) { + nativeProps.children = ; } // Polyfill for default of "display:block" diff --git a/packages/react-strict-dom/tests/html/html-test.native.js b/packages/react-strict-dom/tests/html/html-test.native.js index 21981bc3..bce86626 100644 --- a/packages/react-strict-dom/tests/html/html-test.native.js +++ b/packages/react-strict-dom/tests/html/html-test.native.js @@ -46,6 +46,74 @@ describe(' (native polyfills)', () => { expect(root.toJSON()).toMatchSnapshot('auto-wrap raw strings'); }); + test('auto-wraps interpolated text siblings', () => { + const count = 3; + + let root; + act(() => { + root = create(Count: {count}); + }); + expect(root.toJSON()).toMatchObject({ + type: 'ViewNativeComponent', + children: [{ type: 'Text', children: ['Count: 3'] }] + }); + }); + + test('auto-wraps numeric only child', () => { + const count = 3; + + let root; + act(() => { + root = create({count}); + }); + expect(root.toJSON()).toMatchObject({ + type: 'ViewNativeComponent', + children: [{ type: 'Text', children: ['3'] }] + }); + }); + + test('ignores empty interpolated children', () => { + const count = 3; + + let root; + act(() => { + root = create( + + {count} + {null} + {false} + {' items'} + + ); + }); + expect(root.toJSON()).toMatchObject({ + type: 'ViewNativeComponent', + children: [{ type: 'Text', children: ['3 items'] }] + }); + }); + + test('does not wrap mixed text and element children', () => { + const count = 3; + + let root; + act(() => { + root = create( + + Count: {count} + nested + + ); + }); + const children = root.toJSON().children; + expect(children).toHaveLength(3); + expect(children[0]).toBe('Count: '); + expect(children[1]).toBe('3'); + expect(children[2]).toMatchObject({ + type: 'Text', + children: ['nested'] + }); + }); + describe('polyfills: layout', () => { test('default block layout', () => { let root;