Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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 = <TextString children={nativeProps.children} />;
const textContent = getTextContent(nativeProps.children);
if (textContent != null) {
nativeProps.children = <TextString children={textContent} />;
}

// Polyfill for default of "display:block"
Expand Down
68 changes: 68 additions & 0 deletions packages/react-strict-dom/tests/html/html-test.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,74 @@ describe('<html.*> (native polyfills)', () => {
expect(root.toJSON()).toMatchSnapshot('auto-wrap raw strings');
});

test('auto-wraps interpolated text siblings', () => {
const count = 3;

let root;
act(() => {
root = create(<html.div>Count: {count}</html.div>);
});
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(<html.div>{count}</html.div>);
});
expect(root.toJSON()).toMatchObject({
type: 'ViewNativeComponent',
children: [{ type: 'Text', children: ['3'] }]
});
});

test('ignores empty interpolated children', () => {
const count = 3;

let root;
act(() => {
root = create(
<html.div>
{count}
{null}
{false}
{' items'}
</html.div>
);
});
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(
<html.div>
Count: {count}
<html.span>nested</html.span>
</html.div>
);
});
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;
Expand Down