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

refactor(fscomponents): FLAGSHIP-69 Update to a function component #1010

Merged
merged 1 commit into from
Jan 24, 2020
Merged
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
54 changes: 25 additions & 29 deletions packages/fscomponents/src/components/Total.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { PureComponent } from 'react';
import React from 'react';

import {
StyleProp,
Expand Down Expand Up @@ -40,11 +40,14 @@ export interface TotalProps {
style?: StyleProp<ViewStyle>; // Optional styling for the entire total row
}

export class Total extends PureComponent<TotalProps> {
renderData(
function isCurrency(data: JSX.Element | CurrencyValue): data is CurrencyValue {
return !!((data as CurrencyValue).value && (data as CurrencyValue).currencyCode);
}

export const Total = React.memo((props: TotalProps): JSX.Element => {
const renderData = (
data: string | CurrencyValue | JSX.Element,
style?: StyleProp<ViewStyle>
): JSX.Element {
style?: StyleProp<ViewStyle>): JSX.Element => {
if (typeof data === 'string') {
return (
<Text style={style}>{data}</Text>
Expand All @@ -55,30 +58,23 @@ export class Total extends PureComponent<TotalProps> {
<Text style={style}>{FSI18n.currency(data)}</Text>
);
}

return data;
}
};

render(): JSX.Element {
return (
<View style={[styles.row, this.props.style]}>
<View style={styles.leftColumn}>
{this.renderData(
this.props.keyName,
[this.props.keyStyle]
)}
</View>
<View style={styles.rightColumn}>
{this.renderData(
this.props.value,
[styles.rightColumnText as StyleProp<TextStyle>, this.props.valueStyle]
)}
</View>
return (
<View style={[styles.row, props.style]}>
<View style={styles.leftColumn}>
{renderData(
props.keyName,
[props.keyStyle]
)}
</View>
);
}
}

function isCurrency(data: JSX.Element | CurrencyValue): data is CurrencyValue {
return !!((data as CurrencyValue).value && (data as CurrencyValue).currencyCode);
}
<View style={styles.rightColumn}>
{renderData(
props.value,
[styles.rightColumnText as StyleProp<TextStyle>, props.valueStyle]
)}
</View>
</View>
);
});