Skip to content

Commit d26d8e4

Browse files
committed
add fork button
1 parent cb31545 commit d26d8e4

File tree

6 files changed

+97
-11
lines changed

6 files changed

+97
-11
lines changed

example/src/App.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import React, { Component } from 'react';
22

3-
import { Star } from 'react-github-buttons';
3+
import { Fork, Star } from 'react-github-buttons';
44

55
export default class App extends Component {
66
render () {
77
return (
88
<div>
99
<Star owner='vaibhavhrt' repo='react-github-buttons' />
10+
<Fork owner='vaibhavhrt' repo='react-github-buttons' />
1011
</div>
1112
)
1213
}

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
"prepare": "npm run build",
2121
"predeploy": "cd example && npm install && npm run build",
2222
"deploy": "gh-pages -d example/build",
23-
"lint": "tslint **/*.{ts,tsx} --exclude **/node_modules/**"
23+
"lint": "tslint **/src/**.{ts,tsx} --exclude **/node_modules/**"
2424
},
2525
"dependencies": {},
2626
"peerDependencies": {

src/fork.tsx

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/**
2+
* @function Fork
3+
*/
4+
import * as PropTypes from "prop-types";
5+
import * as React from "react";
6+
7+
import GithubButton from "./lib/githubButton";
8+
9+
export interface IPropTypes {
10+
owner: string;
11+
repo: string;
12+
}
13+
14+
export interface IState {
15+
forks_count: number;
16+
}
17+
18+
export default class Fork extends React.Component<IPropTypes, IState> {
19+
static propTypes = {
20+
owner: PropTypes.string.isRequired,
21+
repo: PropTypes.string.isRequired,
22+
};
23+
24+
constructor(props: IPropTypes) {
25+
super(props);
26+
this.state = {
27+
forks_count: 0,
28+
};
29+
}
30+
31+
componentDidMount() {
32+
const { owner, repo } = this.props;
33+
fetch(`https://api.github.com/repos/${owner}/${repo}`).then((res) => res.json()).then((res) => {
34+
this.setState({ forks_count: res.forks_count });
35+
});
36+
}
37+
38+
render() {
39+
// const { owner, repo } = this.props;
40+
const { forks_count } = this.state;
41+
return <GithubButton variant="fork" count={forks_count} { ...this.props } />;
42+
}
43+
}

src/index.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
import Star from "./star";
2+
import Fork from "./fork";
23

3-
export { Star };
4+
export { Fork, Star };

src/lib/githubButton.tsx

+29-8
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,38 @@ import * as React from "react";
77
import classes from "../styles.css";
88

99
import StarIcon from "./icons/starIcon";
10+
import ForkIcon from "./icons/forkIcon";
1011

11-
function getDataForVariant(variant: string, count: number): { title: string; label: string; Icon: () => JSX.Element; } {
12+
function getDataForVariant(
13+
variant: string,
14+
count: number,
15+
owner: string,
16+
repo: string
17+
): {
18+
title: string;
19+
btnTitle: string;
20+
label: string;
21+
countUrl: string;
22+
Icon: () => JSX.Element;
23+
} {
1224
let title: string;
25+
let btnTitle: string;
1326
let label: string;
27+
let countUrl: string;
1428
if (variant === "star") {
1529
title = "Star";
16-
label = `${count} user starred this repository`;
17-
return { title, label, Icon: StarIcon };
30+
btnTitle = `Star ${owner}/${repo}`;
31+
label = `${count} users starred this repository`;
32+
countUrl = `https://github.com/${owner}/${repo}/stargazers`;
33+
return { title, btnTitle, label, countUrl, Icon: StarIcon };
34+
} else if (variant === "fork") {
35+
title = "Fork";
36+
btnTitle = `Fork your own copy of ${owner}/${repo} to your account`;
37+
label = `${count} users forked this repository`;
38+
countUrl = `https://github.com/vaibhavhrt/react-github-buttons/network/members`;
39+
return { title, btnTitle, label, countUrl, Icon: ForkIcon };
1840
}
19-
// throw new Error("Invalid Variant, supply one of [star, ]");
20-
return { title: "", label: "", Icon: StarIcon };
41+
throw new Error("Invalid Variant, supply one of [star, fork]");
2142
}
2243

2344
export interface IPropTypes {
@@ -29,17 +50,17 @@ export interface IPropTypes {
2950

3051
export default function GithubButton(props: IPropTypes) {
3152
const { owner, repo, variant, count } = props;
32-
const { title, label, Icon } = getDataForVariant(variant, count);
53+
const { title, btnTitle, label, countUrl, Icon } = getDataForVariant(variant, count, owner, repo);
3354

3455
return (
3556
<div className={classes.root}>
36-
<button title={`Star ${owner}/${repo}`} className={classes.button}>
57+
<button title={btnTitle} className={classes.button}>
3758
<Icon />
3859
{title}
3960
</button>
4061
<a
4162
className={classes.count}
42-
href={`https://github.com/${owner}/${repo}/stargazers`}
63+
href={countUrl}
4364
aria-label={label}
4465
target="_blank"
4566
rel="noreferrer noopener"

src/lib/icons/forkIcon.tsx

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/* tslint:disable:max-line-length */
2+
import * as React from "react";
3+
import classes from "../../styles.css";
4+
5+
export default function ForkIcon() {
6+
return (
7+
<svg
8+
className={classes.svgicon}
9+
viewBox="0 0 10 16"
10+
version="1.1"
11+
width="10"
12+
height="16"
13+
>
14+
<path
15+
fillRule="evenodd"
16+
d="M8 1a1.993 1.993 0 0 0-1 3.72V6L5 8 3 6V4.72A1.993 1.993 0 0 0 2 1a1.993 1.993 0 0 0-1 3.72V6.5l3 3v1.78A1.993 1.993 0 0 0 5 15a1.993 1.993 0 0 0 1-3.72V9.5l3-3V4.72A1.993 1.993 0 0 0 8 1zM2 4.2C1.34 4.2.8 3.65.8 3c0-.65.55-1.2 1.2-1.2.65 0 1.2.55 1.2 1.2 0 .65-.55 1.2-1.2 1.2zm3 10c-.66 0-1.2-.55-1.2-1.2 0-.65.55-1.2 1.2-1.2.65 0 1.2.55 1.2 1.2 0 .65-.55 1.2-1.2 1.2zm3-10c-.66 0-1.2-.55-1.2-1.2 0-.65.55-1.2 1.2-1.2.65 0 1.2.55 1.2 1.2 0 .65-.55 1.2-1.2 1.2z"
17+
/>
18+
</svg>
19+
);
20+
}

0 commit comments

Comments
 (0)