1
0
mirror of https://github.com/SomboChea/ui synced 2024-11-24 15:04:27 +07:00

fix: PackageList component is converted to functional (#219) #116

This commit is contained in:
Alfonso Austin 2019-10-26 05:36:16 -04:00 committed by Juan Picado @jotadeveloper
parent 2bc49f3ab7
commit ae0222cf65
4 changed files with 18 additions and 28 deletions

View File

@ -1,4 +1,4 @@
import React, { Fragment, ReactElement } from 'react'; import * as React from 'react';
import Divider from '@material-ui/core/Divider'; import Divider from '@material-ui/core/Divider';
import Package from '../Package'; import Package from '../Package';
@ -12,36 +12,26 @@ interface Props {
packages: PackageInterface[]; packages: PackageInterface[];
} }
export default class PackageList extends React.Component<Props, {}> { export const PackageList: React.FC<Props> = props => {
public render(): ReactElement<HTMLElement> { const renderPackages: () => React.ReactElement<HTMLElement>[] = () => {
return ( return props.packages.map((pkg, i) => {
<div className={'package-list-items'}>
<div className={classes.pkgContainer}>{this.hasPackages() ? this.renderPackages() : <Help />}</div>
</div>
);
}
private hasPackages(): boolean {
const { packages } = this.props;
return packages.length > 0;
}
private renderPackages = () => {
return <>{this.renderList()}</>;
};
private renderList = () => {
const { packages } = this.props;
return packages.map((pkg, i) => {
const { name, version, description, time, keywords, dist, homepage, bugs, author } = pkg; const { name, version, description, time, keywords, dist, homepage, bugs, author } = pkg;
// TODO: move format license to API side. // TODO: move format license to API side.
const license = formatLicense(pkg.license); const license = formatLicense(pkg.license);
return ( return (
<Fragment key={i}> <React.Fragment key={i}>
{i !== 0 && <Divider />} {i !== 0 && <Divider />}
<Package {...{ name, dist, version, author, description, license, time, keywords, homepage, bugs }} /> <Package {...{ name, dist, version, author, description, license, time, keywords, homepage, bugs }} />
</Fragment> </React.Fragment>
); );
}); });
}; };
}
const hasPackages: () => boolean = () => props.packages.length > 0;
return (
<div className={'package-list-items'}>
<div className={classes.pkgContainer}>{hasPackages() ? renderPackages() : <Help />}</div>
</div>
);
};

View File

@ -4,7 +4,7 @@ import { BrowserRouter } from 'react-router-dom';
import Help from '../Help'; import Help from '../Help';
import PackageList from './PackageList'; import { PackageList } from './PackageList';
describe('<PackageList /> component', () => { describe('<PackageList /> component', () => {
test('should load the component with no packages', () => { test('should load the component with no packages', () => {

View File

@ -1 +1 @@
export { default } from './PackageList'; export { PackageList } from './PackageList';

View File

@ -1,6 +1,6 @@
import React from 'react'; import React from 'react';
import PackageList from '../../components/PackageList'; import { PackageList } from '../../components/PackageList';
import { PackageInterface } from '../../components/Package/Package'; import { PackageInterface } from '../../components/Package/Package';
interface Props { interface Props {