Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix StyleSheet.flatten returning undefined for falsy values #46294

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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 @@ -62,14 +62,6 @@ describe('flattenStyle', () => {
expect(flatStyle.height).toBe(20);
});

it('should not allocate an object when there is no style', () => {
const nullStyle = flattenStyle(null);
const nullStyleAgain = flattenStyle(null);

expect(nullStyle).toBe(nullStyleAgain);
expect(nullStyle).toBe(undefined);
});

it('should not allocate an object when there is a style', () => {
const style = {a: 'b'};
const nullStyle = flattenStyle(style);
Expand Down Expand Up @@ -143,10 +135,10 @@ describe('flattenStyle', () => {
});
});

it('should ignore invalid class names', () => {
const invalid = flattenStyle(1234, null);

expect(invalid).toEqual(undefined);
// Invalid class name 1234 skipping ...
it('should flatten falsy values', () => {
expect(flattenStyle(null)).toEqual({});
expect(flattenStyle(undefined)).toEqual({});
expect(flattenStyle(false)).toEqual({});
expect(flattenStyle('')).toEqual({});
});
});
3 changes: 2 additions & 1 deletion packages/react-native/Libraries/StyleSheet/flattenStyle.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ function flattenStyle<TStyleProp: DangerouslyImpreciseStyleProp>(
// $FlowFixMe[underconstrained-implicit-instantiation]
): ?____FlattenStyleProp_Internal<TStyleProp> {
if (style === null || typeof style !== 'object') {
return undefined;
// $FlowFixMe[incompatible-return]
return {};
}

if (!Array.isArray(style)) {
Expand Down
3 changes: 3 additions & 0 deletions packages/react-native/Libraries/Text/__tests__/Text-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ describe('Text', () => {
ellipsizeMode="tail"
isHighlighted={false}
selectionColor={null}
style={Object {}}
/>
`);
});
Expand Down Expand Up @@ -65,6 +66,7 @@ describe('Text compat with web', () => {
isHighlighted={false}
nativeID="id"
selectionColor={null}
style={Object {}}
tabIndex={0}
testID="testID"
/>
Expand Down Expand Up @@ -181,6 +183,7 @@ describe('Text compat with web', () => {
isHighlighted={false}
role="main"
selectionColor={null}
style={Object {}}
/>
`);
});
Expand Down
Loading