Skip to content

Commit

Permalink
Merge pull request #1010 from huizhuz/FLAGSHIP-69
Browse files Browse the repository at this point in the history
refactor(fscomponents): FLAGSHIP-69 Update to a function component
  • Loading branch information
Cauldrath authored Jan 24, 2020
2 parents d44c837 + 82e4801 commit 685bfad
Showing 1 changed file with 25 additions and 29 deletions.
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>
);
});

0 comments on commit 685bfad

Please sign in to comment.