Skip to content
This repository was archived by the owner on Jan 16, 2022. It is now read-only.

Commit ae0222c

Browse files
alfonsoarjuanpicado
authored andcommitted
fix: PackageList component is converted to functional (#219) #116
1 parent 2bc49f3 commit ae0222c

File tree

4 files changed

+18
-28
lines changed

4 files changed

+18
-28
lines changed
Lines changed: 15 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { Fragment, ReactElement } from 'react';
1+
import * as React from 'react';
22
import Divider from '@material-ui/core/Divider';
33

44
import Package from '../Package';
@@ -12,36 +12,26 @@ interface Props {
1212
packages: PackageInterface[];
1313
}
1414

15-
export default class PackageList extends React.Component<Props, {}> {
16-
public render(): ReactElement<HTMLElement> {
17-
return (
18-
<div className={'package-list-items'}>
19-
<div className={classes.pkgContainer}>{this.hasPackages() ? this.renderPackages() : <Help />}</div>
20-
</div>
21-
);
22-
}
23-
24-
private hasPackages(): boolean {
25-
const { packages } = this.props;
26-
return packages.length > 0;
27-
}
28-
29-
private renderPackages = () => {
30-
return <>{this.renderList()}</>;
31-
};
32-
33-
private renderList = () => {
34-
const { packages } = this.props;
35-
return packages.map((pkg, i) => {
15+
export const PackageList: React.FC<Props> = props => {
16+
const renderPackages: () => React.ReactElement<HTMLElement>[] = () => {
17+
return props.packages.map((pkg, i) => {
3618
const { name, version, description, time, keywords, dist, homepage, bugs, author } = pkg;
3719
// TODO: move format license to API side.
3820
const license = formatLicense(pkg.license);
3921
return (
40-
<Fragment key={i}>
22+
<React.Fragment key={i}>
4123
{i !== 0 && <Divider />}
4224
<Package {...{ name, dist, version, author, description, license, time, keywords, homepage, bugs }} />
43-
</Fragment>
25+
</React.Fragment>
4426
);
4527
});
4628
};
47-
}
29+
30+
const hasPackages: () => boolean = () => props.packages.length > 0;
31+
32+
return (
33+
<div className={'package-list-items'}>
34+
<div className={classes.pkgContainer}>{hasPackages() ? renderPackages() : <Help />}</div>
35+
</div>
36+
);
37+
};

src/components/PackageList/Packagelist.test.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { BrowserRouter } from 'react-router-dom';
44

55
import Help from '../Help';
66

7-
import PackageList from './PackageList';
7+
import { PackageList } from './PackageList';
88

99
describe('<PackageList /> component', () => {
1010
test('should load the component with no packages', () => {

src/components/PackageList/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export { default } from './PackageList';
1+
export { PackageList } from './PackageList';

src/pages/home/Home.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React from 'react';
22

3-
import PackageList from '../../components/PackageList';
3+
import { PackageList } from '../../components/PackageList';
44
import { PackageInterface } from '../../components/Package/Package';
55

66
interface Props {

0 commit comments

Comments
 (0)