-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: refactoring replaces class components with function compone…
…nts.
- Loading branch information
1 parent
f11b8ba
commit 267eb4a
Showing
26 changed files
with
344 additions
and
263 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,28 @@ | ||
import React, { Component } from 'react'; | ||
import React from 'react'; | ||
import type { AnchorHTMLAttributes, ImgHTMLAttributes } from 'react'; | ||
|
||
export interface BaseProps extends ImgHTMLAttributes<HTMLImageElement> { | ||
export interface InternalProps extends ImgHTMLAttributes<HTMLImageElement> { | ||
platform?: 'github' | 'coveralls' | 'npm'; | ||
type?: string; | ||
user?: string; | ||
repo?: string; | ||
base?: string; | ||
imgSrc?: string; | ||
href?: HTMLAnchorElement['href']; | ||
children?: React.ReactNode; | ||
anchor?: Omit<AnchorHTMLAttributes<HTMLAnchorElement>, 'href'>; | ||
} | ||
export interface BaseState extends BaseProps {} | ||
|
||
export default class Base<T> extends Component<BaseProps & T, BaseState & T> { | ||
static defaultProps: BaseProps = { | ||
platform: 'github', | ||
base: 'https://img.shields.io', | ||
}; | ||
constructor(props: BaseProps & T, defaultState: BaseProps & T, forceState?: BaseProps & T) { | ||
super(props); | ||
this.state = Object.assign({}, { ...defaultState, ...props }, forceState); | ||
export const Internal = React.forwardRef<HTMLImageElement, InternalProps>((props, ref) => { | ||
const { href, anchor = {}, imgSrc, platform, type, user, repo, base, children, ...other } = props; | ||
if (href) { | ||
return ( | ||
<a {...anchor} href={href}> | ||
<img alt="" ref={ref} src={imgSrc} {...other} /> | ||
</a> | ||
); | ||
} | ||
componentDidUpdate(prevProps: Readonly<BaseProps & T>, prevState: Readonly<BaseState & T>, snapshot?: any): void { | ||
if (prevProps !== this.props) { | ||
this.setState({ ...this.state, ...this.props }); | ||
} | ||
} | ||
getUrl = () => ''; | ||
render() { | ||
const { href, anchor, ...other } = this.state; | ||
if (href) { | ||
return ( | ||
<a {...anchor} href={href}> | ||
<img alt="" src={this.getUrl()} {...other} /> | ||
</a> | ||
); | ||
} | ||
return <img alt="" src={this.getUrl()} {...other} />; | ||
} | ||
} | ||
return <img alt="" ref={ref} src={imgSrc} {...other} />; | ||
}); | ||
|
||
Internal.displayName = 'Internal'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,13 @@ | ||
import React, { Component, ReactElement } from 'react'; | ||
import { BaseProps } from '../common/Base'; | ||
import React, { ReactElement } from 'react'; | ||
import { InternalProps } from '../common/Base'; | ||
|
||
export interface ContainerProps extends BaseProps {} | ||
export interface ContainerProps extends InternalProps {} | ||
|
||
export default class Container extends Component<ContainerProps> { | ||
render() { | ||
return ( | ||
<> | ||
{React.Children.toArray(this.props.children).map((child: React.ReactNode) => { | ||
if (!React.isValidElement(child)) return null; | ||
return React.cloneElement(child as ReactElement, { ...this.props, ...(child as React.ReactElement).props }); | ||
})} | ||
</> | ||
); | ||
} | ||
export default function Container(props: ContainerProps) { | ||
return React.Children.toArray(props.children).map((child: React.ReactNode) => { | ||
if (!React.isValidElement(child)) return null; | ||
return React.cloneElement(child as ReactElement, { ...props, ...(child as React.ReactElement).props }); | ||
}); | ||
} | ||
|
||
Container.displayName = 'Container'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,15 @@ | ||
import Coverage from './Coverage'; | ||
import Container, { ContainerProps } from '../common/Container'; | ||
import Coverages from './Coverages'; | ||
import Codacy from './Codacy'; | ||
|
||
type CoverageComponent = typeof Container & { | ||
Coverages: typeof Coverages; | ||
Codacy: typeof Codacy; | ||
}; | ||
|
||
const Coverage = (props: ContainerProps) => (<Container {...props} />) as unknown as CoverageComponent; | ||
Coverage.Coverages = Coverages; | ||
Coverage.Codacy = Codacy; | ||
Coverage.displayName = 'Github'; | ||
|
||
export default Coverage; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.