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 6 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,65 @@
import React from "react";
import { StyleType } from '../../../theme';

export type RenderProp<Props = {}> = (props?: Props) => React.ReactElement;

export type FalsyNodeProps<Props = {}> = Props & {
fallback?: React.ReactElement;
children?: React.ReactElement;
style?: StyleType;
};

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

if (!children) {
return fallback || null;
}

return <>{this.renderComponentChildren(children, props)}</>;
}
}
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
7 changes: 4 additions & 3 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;
header?: RenderProp<ViewProps>;
footer?: RenderProp<ViewProps>;
accent?: RenderProp<ViewProps>;
Expand Down Expand Up @@ -150,9 +151,9 @@ export class Card extends React.Component<CardProps> {
component={header}
/>
{header && <Divider/>}
<View style={evaStyle.body}>
<FalsyNode style={evaStyle.body}>
{children}
</View>
</FalsyNode>
{footer && <Divider/>}
<FalsyFC
style={[styles.transparent, evaStyle.footer]}
Expand Down