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

feat: implement falsyNode realisation & update card body component #1386

Open
wants to merge 10 commits into
base: master
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
@@ -0,0 +1,63 @@
import React from "react";
import { PropsService } from '../../services/props/props.service';
import { StyleType } from '../../../theme';

export type RenderComponent = React.ReactElement | React.ReactElement[];

export type FalsyNodeProps<Props = {}> = Props & {
component?: RenderComponent;
style?: StyleType;
};

/**
* Helper component for optional properties that should render cloned component.
*
* Accepts props of a component that is expected to be rendered,
* and `component` which may be React Element only.
*
* If it is a React Element, will call it with props passed to this component.
*
* @property {RenderComponent} component - React jsx component to be rendered.
*
* @example Will render nothing.
* ```
* <FalsyNode />
* ```
*
* @example Will render red title.
* ```
* const Title = () => (
* <FalsyNode
* style={{ color: 'red' }}
* component={<Text>Title</Text>}
* />
* );
* ```
*/

type ChildElement = React.ReactElement;
type ChildrenProp = ChildElement | ChildElement[];

export class FalsyNode<Props = {}> extends React.Component<FalsyNodeProps<Props>> {
private renderChildElement = (source: ChildElement, props: any): ChildElement => {
return React.cloneElement(source, {
...props,
...source.props,
style: PropsService.mergeObjectsWithArrays([this.props?.style, source.props?.style]),
});
};

private renderComponentChildren = (source: ChildrenProp, props: any): ChildElement[] => {
return React.Children.map(source, child => this.renderChildElement(child, props));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we have to apply props to each child.

};

public render(): React.ReactElement {
const { component, ...props } = this.props;

if (!component) {
return null;
}

return <>{this.renderComponentChildren(component, props)}</>;
}
}
71 changes: 71 additions & 0 deletions src/components/devsupport/components/falsyNode/falsyNode.spec.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import React from "react";
import { Text } from "react-native";
import { fireEvent, render } from "react-native-testing-library";
import { FalsyNode } from "./falsyNode.component";

it("should render nothing", function () {
const component = render(<FalsyNode />);
expect(component.toJSON()).toEqual(null);
});

it("should render provided React Element", () => {
const component = render(
<FalsyNode style={{ color: "red" }} component={<Text>I love Babel</Text>} />
);

const textComponent = component.getByText("I love Babel");

expect(textComponent).toBeTruthy();
expect(textComponent.props.style).toEqual({
color: "red",
});
});

it("should render provided React Element with overwritten styles", () => {
const renderComponent = <Text style={{ color: "blue" }}>I love Babel</Text>;

const component = render(
<FalsyNode
style={{ color: "red", backgroundColor: "black" }}
component={renderComponent}
/>
);

const textComponent = component.getByText("I love Babel");

expect(textComponent).toBeTruthy();
expect(textComponent.props.style).toEqual({
color: "blue",
backgroundColor: "black",
});
});

it('should keep props passed in FalsyNode component', function () {
const onPress = jest.fn();

const component = render(
<FalsyNode
onPress={onPress}
component={<Text>I love Babel</Text>}
/>,
);

fireEvent.press(component.queryByText('I love Babel'));
expect(onPress).toBeCalledTimes(1);
});

it('should override props passed in FalsyNode component', function () {
const onPress = jest.fn();
const onInnerPress = jest.fn();

const component = render(
<FalsyNode
onPress={onPress}
component={<Text onPress={onInnerPress}>I love Babel</Text>}
/>,
);

fireEvent.press(component.queryByText('I love Babel'));
expect(onPress).toBeCalledTimes(0);
expect(onInnerPress).toBeCalledTimes(1);
});
1 change: 1 addition & 0 deletions src/components/devsupport/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export {
RenderProp,
} from './components/falsyFC/falsyFC.component';
export { FalsyText } from './components/falsyText/falsyText.component';
export { FalsyNode } from './components/falsyNode/falsyNode.component';
export {
TouchableWithoutFeedback,
TouchableWithoutFeedbackProps,
Expand Down
17 changes: 17 additions & 0 deletions src/components/devsupport/services/props/props.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ export interface RestProps {
export type AllOfProps = Partial<Props>;
export type AllWithRestProps = Partial<Props> & RestProps;

type ObjectsToMerge = Object[] | [Object[]];

class NativePropsService {
/**
* Retrieves all props included in `from` array
Expand Down Expand Up @@ -159,6 +161,21 @@ class NativePropsService {
};
}, {});
}

/**
* Merge objects & array of objects passed to array parameter.
*
* @param {Partial<ObjectsToMerge>} objects - array which needs to be merged.
*
* @return {Object} - merged object with merged values inside.
*/
public mergeObjectsWithArrays = (objects: Partial<ObjectsToMerge>): Object => {
return Object.assign({},
...objects.map(currentObject =>
Array.isArray(currentObject)
? Object.assign({}, ...currentObject.map(obj => obj))
: currentObject));
};
}

export const PropsService = new NativePropsService();
11 changes: 11 additions & 0 deletions src/components/devsupport/services/props/props.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,15 @@ describe('@props: service checks', () => {
});
});

it('should merge object & array of objects in appropriate object', () => {
const stylesArray = [{color: 'blue'}, {backgroundColor: 'black'}];
const styles = {color: 'red'};

const mergedStyles = PropsService.mergeObjectsWithArrays([stylesArray, styles]);
expect(mergedStyles).toEqual({
color: 'red',
backgroundColor: 'black',
})
});

});
10 changes: 6 additions & 4 deletions src/components/ui/card/card.component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
import {
EvaStatus,
FalsyFC,
FalsyNode,
RenderProp,
TouchableWeb,
TouchableWebElement,
Expand All @@ -33,7 +34,7 @@ type CardStyledProps = Overwrite<StyledComponentProps, {
}>;

export interface CardProps extends TouchableWebProps, CardStyledProps {
children?: React.ReactNode;
children?: React.ReactElement | React.ReactElement[];
header?: RenderProp<ViewProps>;
footer?: RenderProp<ViewProps>;
accent?: RenderProp<ViewProps>;
Expand Down Expand Up @@ -150,9 +151,10 @@ export class Card extends React.Component<CardProps> {
component={header}
/>
{header && <Divider/>}
<View style={evaStyle.body}>
{children}
</View>
<FalsyNode
style={evaStyle.body}
component={children}
/>
{footer && <Divider/>}
<FalsyFC
style={[styles.transparent, evaStyle.footer]}
Expand Down