Skip to content

Commit 338d895

Browse files
committed
feat: integrate with Typescript
1 parent 98a21ae commit 338d895

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+5387
-17252
lines changed

.babelrc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"corejs": "3.12"
88
}
99
],
10-
"@babel/preset-react"
10+
"@babel/preset-react",
11+
"@babel/preset-typescript"
1112
]
1213
}

.npmignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,6 @@ test/
2121

2222
Makefile
2323
README.md
24-
rollup.config.js
24+
rollup.config.js
25+
jest.config.js
26+
tsconfig.json

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ build:
2222
@rm -rf ./dist
2323
@npx rollup -c rollup.config.js
2424
@echo "Copy type into dist..."
25-
@cp react-nice-avatar.d.ts dist/react-nice-avatar.d.ts
25+
@cp src/types.ts dist/index.d.ts
2626
build-demo:
2727
@echo "Building demo..."
2828
@rm -rf ./demo/dist

demo/rollup.config.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import serve from "rollup-plugin-serve";
66
import livereload from "rollup-plugin-livereload";
77
import replace from "@rollup/plugin-replace";
88
import scss from "rollup-plugin-scss";
9+
import typescript from "@rollup/plugin-typescript";
910

1011
let plugins = [
1112
replace({
@@ -15,6 +16,9 @@ let plugins = [
1516
babel({
1617
exclude: "node_modules/**"
1718
}),
19+
typescript({
20+
tsconfig: path.resolve(__dirname, "./tsconfig.json")
21+
}),
1822
resolve(),
1923
commonjs(),
2024
scss()
@@ -28,17 +32,17 @@ if (process.env.NODE_ENV === "development" && process.env.MODE !== "build") {
2832
host: "localhost",
2933
port: 5555
3034
}),
31-
livereload({ watch: path.resolve(__dirname, "./") })
35+
livereload({ watch: path.resolve(__dirname, "./src") })
3236
]);
3337
}
3438

3539
export default {
36-
input: path.resolve(__dirname, "./index.js"),
40+
input: path.resolve(__dirname, "./src/index.tsx"),
3741
output: [
3842
{
3943
file: path.resolve(__dirname, "./dist/index.js"),
4044
format: "iife",
41-
sourcemap: true
45+
sourcemap: false
4246
}
4347
],
4448
plugins

demo/Cases.js renamed to demo/src/Cases.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import React from "react";
22
import classnames from "classnames";
33

4-
import Avatar from "../src";
4+
import Avatar from "../../src";
5+
6+
type GenApplicationMockup = (opt?: { theme?: string }) => React.ReactElement
57

68
export default function (props) {
79
const { config } = props;
@@ -33,7 +35,7 @@ export default function (props) {
3335
</div>
3436
);
3537

36-
const genApplicationMockup = ({ theme } = {}) => (
38+
const genApplicationMockup:GenApplicationMockup = ({ theme } = {}) => (
3739
<div className={classnames("application", theme)}>
3840
<div className="applicationHeader">
3941
<div className="icons">

demo/Single.js renamed to demo/src/Single.tsx

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,17 @@ import Slider from "rc-slider";
55
import domtoimage from "dom-to-image";
66
import { saveAs } from "file-saver";
77

8-
import Avatar, { genConfig } from "../src";
8+
import Avatar, { genConfig } from "../../src";
9+
10+
import {
11+
SingleComponentProps,
12+
SingleComponentState,
13+
Panel
14+
} from './types'
915

1016
import "rc-slider/assets/index.css";
1117

12-
export default class Single extends Component {
18+
export default class Single extends Component<SingleComponentProps, SingleComponentState> {
1319
constructor(props) {
1420
super(props);
1521
this.state = {
@@ -33,7 +39,7 @@ export default class Single extends Component {
3339
window.removeEventListener("click", this.closeAllColorPanel);
3440
}
3541

36-
updateConfig(field, value) {
42+
updateConfig(field: string, value: any) {
3743
const { config, updateConfig } = this.props;
3844
if (config[field] === value) return;
3945
config[field] = value;
@@ -44,17 +50,17 @@ export default class Single extends Component {
4450
}
4551
}
4652

47-
onChangeColor(field, value) {
53+
onChangeColor(field: string, value: { hex: string }) {
4854
const { config, updateConfig } = this.props;
4955
config[field] = value.hex;
5056
updateConfig(config);
5157
}
5258

53-
toggleColorPanel(panelName, e) {
59+
toggleColorPanel(panelName: Panel, e) {
5460
e.nativeEvent.stopImmediatePropagation();
5561
this.setState({
5662
[panelName]: !this.state[panelName]
57-
});
63+
} as any);
5864
}
5965

6066
closeAllColorPanel() {
@@ -87,16 +93,18 @@ export default class Single extends Component {
8793
async download() {
8894
const scale = 2;
8995
const node = document.getElementById("avatar");
90-
const blob = await domtoimage.toBlob(node, {
91-
height: node.offsetHeight * scale,
92-
style: {
93-
transform: `scale(${scale}) translate(${node.offsetWidth / 2 / scale}px, ${node.offsetHeight / 2 / scale}px)`,
94-
"border-radius": 0
95-
},
96-
width: node.offsetWidth * scale
97-
});
96+
if (node) {
97+
const blob = await domtoimage.toBlob(node, {
98+
height: node.offsetHeight * scale,
99+
style: {
100+
transform: `scale(${scale}) translate(${node.offsetWidth / 2 / scale}px, ${node.offsetHeight / 2 / scale}px)`,
101+
"border-radius": 0
102+
},
103+
width: node.offsetWidth * scale
104+
});
98105

99-
saveAs(blob, "avatar.png");
106+
saveAs(blob, "avatar.png");
107+
}
100108
}
101109

102110
render() {
File renamed without changes.

demo/app.js renamed to demo/src/app.tsx

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import React, { Component } from "react";
22
import classnames from "classnames";
33

4-
import { genConfig } from "../src";
5-
4+
import { genConfig } from "../../src";
65
import Single from "./Single";
76
import Cases from "./Cases";
87

9-
export default class App extends Component {
8+
import { AppComponentState } from './types'
9+
import { AvatarFullConfig } from '../../src/types'
10+
11+
export default class App extends Component<{}, AppComponentState> {
1012
constructor(props) {
1113
super(props);
1214
this.state = {
@@ -16,13 +18,13 @@ export default class App extends Component {
1618
};
1719
}
1820

19-
onChangeTab(tab) {
21+
onChangeTab(tab: string) {
2022
this.setState({
2123
currentTab: tab
2224
});
2325
}
2426

25-
updateConfig(config) {
27+
updateConfig(config: AvatarFullConfig) {
2628
this.setState({
2729
config: { ...config, hairColorRandom: true }
2830
});
File renamed without changes.

0 commit comments

Comments
 (0)