Skip to content

Commit

Permalink
fix: fix props update issue.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaywcjlove committed Aug 26, 2023
1 parent 9d403e2 commit 45a2c99
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 0 deletions.
19 changes: 19 additions & 0 deletions core/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,25 @@ export default function Demo() {
}
```

```jsx mdx:preview
import React from 'react';
import Npm from '@uiw/react-shields/npm';

export default function Demo() {
const [scope, setScope] = React.useState('@uiw');
const [packageName, setPackageName] = React.useState('react-shields');
return (
<>
<Npm.Size scope={scope} packageName={packageName} />
<div>
<button onClick={() => setPackageName('react-shields')}>react-shields</button>
<button onClick={() => setPackageName('react-codemirror')}>react-codemirror</button>
</div>
</>
);
}
```

### Npm.Downloads

```jsx mdx:preview
Expand Down
5 changes: 5 additions & 0 deletions core/src/common/Base.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ export default class Base<T> extends Component<BaseProps & T, BaseState & T> {
super(props);
this.state = Object.assign({}, { ...defaultState, ...props }, forceState);
}
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 } = this.state;
Expand Down
4 changes: 4 additions & 0 deletions core/src/coverages/Codacy.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ export interface CodacyProps extends BaseProps {
}

export default class Codacy extends Base<CodacyProps> {
static defaultProps: BaseProps = {
platform: 'coveralls',
base: 'https://img.shields.io',
};
constructor(props: CodacyProps) {
super(props, {}, { platform: 'coveralls' });
}
Expand Down
4 changes: 4 additions & 0 deletions core/src/coverages/Coverages.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ export interface CoveragesProps extends BaseProps {
}

export default class Coverages extends Base<CoveragesProps> {
static defaultProps: BaseProps = {
platform: 'coveralls',
base: 'https://img.shields.io',
};
constructor(props: CoveragesProps) {
super(props, { type: 'github' }, { platform: 'coveralls' });
}
Expand Down
4 changes: 4 additions & 0 deletions core/src/npm/Downloads.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ export interface DownloadProps extends BaseProps {
*
*/
export default class Downloads extends Base<DownloadProps> {
static defaultProps: BaseProps = {
platform: 'npm',
base: 'https://img.shields.io',
};
constructor(props: DownloadProps) {
super(props, { interval: 'dm' }, { platform: 'npm' });
}
Expand Down
4 changes: 4 additions & 0 deletions core/src/npm/Size.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ export interface SizeProps extends BaseProps {
*
*/
export default class Size extends Base<SizeProps> {
static defaultProps: BaseProps = {
platform: 'npm',
base: 'https://img.shields.io',
};
constructor(props: SizeProps) {
super(props, { format: 'min' }, { platform: 'npm' });
}
Expand Down
4 changes: 4 additions & 0 deletions core/src/npm/Version.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ export interface VersionProps extends BaseProps {
}

export default class Version extends Base<VersionProps> {
static defaultProps: BaseProps = {
platform: 'npm',
base: 'https://img.shields.io',
};
constructor(props: VersionProps) {
super(props, { type: 'version' }, { platform: 'npm' });
}
Expand Down

0 comments on commit 45a2c99

Please sign in to comment.