Skip to content

Commit 51fce10

Browse files
committed
Refactor: typescript
1 parent f602e25 commit 51fce10

File tree

24 files changed

+337
-154
lines changed

24 files changed

+337
-154
lines changed

src/components/image/index.tsx

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,20 @@
11
import React, { useState } from 'react'
22
import ImageWrapper from './style'
3-
import PropTypes from 'prop-types'
43

5-
const Image = props => {
4+
const defaultProps = {
5+
alt: '',
6+
width: 50,
7+
height: 50,
8+
radius: 8,
9+
onClick: (e: React.MouseEvent) => {}
10+
}
11+
12+
export interface ImageProps extends Partial<typeof defaultProps>{
13+
src: string;
14+
style?: React.CSSProperties
15+
}
16+
17+
const Image: React.FC<ImageProps> = (props: ImageProps) => {
618
const { src, alt, width, height, radius, style = {}, onClick } = props
719

820
const [status, setStatus] = useState('loading')
@@ -26,22 +38,6 @@ const Image = props => {
2638
)
2739
}
2840

29-
Image.defaultProps = {
30-
alt: '',
31-
width: 50,
32-
height: 50,
33-
radius: 8,
34-
onClick: () => {}
35-
}
36-
37-
Image.propTypes = {
38-
src: PropTypes.string.isRequired,
39-
width: PropTypes.number,
40-
height: PropTypes.number,
41-
radius: PropTypes.oneOfType([
42-
PropTypes.number,
43-
PropTypes.string
44-
])
45-
}
41+
Image.defaultProps = defaultProps
4642

4743
export default React.memo(Image)

src/components/image/style.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
import styled from 'styled-components'
22

3-
const ImageWrapper = styled.div`
3+
interface WrapperProps {
4+
width?: number;
5+
height?: number;
6+
radius?: string | number;
7+
}
8+
9+
const ImageWrapper = styled.div<WrapperProps>`
410
position: relative;
511
width: ${props => props.width}px;
612
height: ${props => props.height}px;

src/components/loading/index.tsx

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
11
import React from 'react'
22
import styled, { keyframes } from 'styled-components'
33

4-
const rotate = keyframes`
5-
from {
6-
transform: rotate(0deg);
7-
}
4+
interface LoadingProps {
5+
size?: number;
6+
text: string;
7+
}
88

9-
to {
10-
transform: rotate(360deg);
11-
}
9+
interface WrapperProps {
10+
size: number
11+
}
12+
13+
const rotate = keyframes`
14+
from { transform: rotate(0deg);}
15+
to { transform: rotate(360deg);}
1216
`;
1317

14-
const LoadingWrapper = styled.div`
18+
const LoadingWrapper = styled.div<WrapperProps>`
1519
margin: 10px auto;
1620
width: ${props => props.size}px;
1721
height: ${props => props.size}px;
@@ -26,7 +30,7 @@ const TextWrapper = styled.div`
2630
text-align: center;
2731
`
2832

29-
const Loading = props => {
33+
const Loading: React.FC<LoadingProps> = (props: LoadingProps) => {
3034
return (
3135
<>
3236
<LoadingWrapper size={props.size || 30} />

src/components/scroll-list/index.tsx

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,20 @@ import styled from 'styled-components'
66
// polyfill
77
import 'intersection-observer'
88

9+
export interface Iprops {
10+
loading: boolean;
11+
completed: boolean;
12+
children: React.ReactNode;
13+
onLoad: () => void;
14+
}
15+
916
const TipWord = styled.div`
1017
margin: 10px auto;
1118
color: #333;
1219
text-align: center;
1320
`
1421

15-
const ScrollList = props => {
22+
const ScrollList: React.FC<Iprops> = (props: Iprops) => {
1623
const { completed, onLoad, loading } = props
1724

1825
const hanlder = useCallback(entries => {
@@ -22,14 +29,14 @@ const ScrollList = props => {
2229
}
2330
}, [completed, onLoad])
2431

25-
const observer = useRef(new IntersectionObserver(hanlder))
26-
const bottomEl = useRef()
32+
const observer: React.RefObject<IntersectionObserver> = useRef(new IntersectionObserver(hanlder))
33+
const bottomEl: any = useRef<HTMLDivElement>()
2734

28-
useEffect(() => {
29-
observer.current.observe(bottomEl.current)
35+
useEffect(() => {
36+
observer.current && observer.current.observe(bottomEl.current)
3037

3138
return () => {
32-
observer.current.unobserve(bottomEl.current)
39+
observer.current && observer.current.unobserve(bottomEl.current)
3340
}
3441
}, [])
3542

src/components/tabbar/index.tsx

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,16 @@ import React from 'react'
22
import { NavLink } from 'react-router-dom'
33
import TabbarWrapper, { TabbarItem } from './style'
44

5-
const Tabbar = props => {
5+
export interface Tabber {
6+
name: string;
7+
route: string;
8+
}
9+
10+
interface Iprops {
11+
value: Array<Tabber>;
12+
}
13+
14+
const Tabbar: React.FC<Iprops> = (props: Iprops) => {
615
return (
716
<TabbarWrapper>
817
{
@@ -18,8 +27,4 @@ const Tabbar = props => {
1827
)
1928
}
2029

21-
Tabbar.defaultProps = {
22-
value: []
23-
}
24-
2530
export default React.memo(Tabbar)

src/components/tag/index.tsx

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,20 @@ import {
1010
COLOR_GREY_INFO
1111
} from 'style/constants'
1212

13-
const DICT = {
13+
interface Iprops {
14+
type: string
15+
}
16+
17+
interface cate {
18+
text: string;
19+
color: string;
20+
}
21+
22+
interface TagDict {
23+
[index: string]: cate;
24+
}
25+
26+
const DICT: TagDict = {
1427
top: { text: '置顶', color: COLOR_RED },
1528
good: { text: '精华', color: COLOR_ORANGE_DEEP },
1629
share: { text: '分享', color: COLOR_GREEN_TEAL },
@@ -19,7 +32,7 @@ const DICT = {
1932
default: { text: '话题', color: COLOR_GREY_INFO },
2033
}
2134

22-
const TagUI = styled.label`
35+
const TagUI = styled.label<{ color: string }>`
2336
display: inline-block;
2437
padding: 7px 10px;
2538
color: #fff;
@@ -29,7 +42,7 @@ const TagUI = styled.label`
2942
border-radius: 4px;
3043
`
3144

32-
const Tag = props => {
45+
const Tag: React.FC<Iprops> = (props: Iprops) => {
3346
const type = props.type || 'default'
3447
return <TagUI color={ DICT[type].color }>{ DICT[type].text }</TagUI>
3548
}

src/hooks/useAsync.ts

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,45 @@ import { useEffect, useState, useCallback, useRef } from 'react'
44
const noop = () => {}
55
const defaultOption = {
66
mannual: false,
7-
onSuccess: noop,
8-
onError: noop
7+
onSuccess: noop as SuccessHandler,
8+
onError: noop as ErrorHandler
9+
}
10+
11+
export interface SuccessHandler {
12+
<T>(res: any):T;
13+
}
14+
15+
export interface ErrorHandler {
16+
(err: any): void;
17+
}
18+
19+
export interface Option extends Partial<typeof defaultOption>{
20+
onSuccess: SuccessHandler;
21+
onError: ErrorHandler;
22+
}
23+
24+
export interface AsyncResult<T> {
25+
loading: boolean;
26+
run: () => void;
27+
result: T | undefined;
928
}
1029

1130
/**
1231
* @param {Function} action should return a Promise
13-
* @param {Object} option
14-
* @param {Array} deps dependecies
32+
* @param {Object} customOption
1533
*/
16-
const useAysnc = (action = noop, option = {}, deps = []) => {
17-
option = Object.assign({}, defaultOption, option)
34+
const useAsync = <T>(
35+
action: () => Promise<any>,
36+
customOption: object = {}
37+
): AsyncResult<T> => {
38+
let option: Option = Object.assign({}, defaultOption, customOption)
1839

19-
const result = useRef({})
40+
const result = useRef<T>()
2041
const [loading, setLoading] = useState(false)
2142

2243
const run = useCallback(() => {
2344
setLoading(true)
24-
const ret = action()
45+
const ret: Promise<any> = action()
2546
if (ret.then) {
2647
ret.then(res => {
2748
result.current = option.onSuccess(res) || res
@@ -40,8 +61,8 @@ const useAysnc = (action = noop, option = {}, deps = []) => {
4061
return {
4162
loading,
4263
run,
43-
result: result.current || {},
64+
result: result.current,
4465
}
4566
}
4667

47-
export default useAysnc
68+
export default useAsync
File renamed without changes.

src/hooks/useLoadMore.ts

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,31 @@
22
import { useEffect, useRef, useCallback } from 'react'
33
import useAsync from './useAsync'
44

5-
const noop = () => {}
6-
75
const defaultOption = {
86
initPage: 1,
97
initPageSize: 10
108
}
119

10+
interface Option extends Partial<typeof defaultOption> {
11+
formatResult?<T>(result: any): { list: Array<T> };
12+
isNoMore?(result: any): boolean;
13+
}
14+
15+
interface ActionOption {
16+
page: number;
17+
pageSize: number;
18+
}
19+
1220
/**
1321
* @param {Function} action should return a Promise
1422
* @param {Object} option
1523
* @param {Array} deps dependecies
1624
*/
17-
export default (action = noop, option, deps = []) => {
25+
export default (
26+
action: (res: any) => Promise<any>,
27+
option: Option = defaultOption,
28+
deps: React.DependencyList = []
29+
) => {
1830

1931
option = Object.assign({}, defaultOption, option || {})
2032

@@ -30,7 +42,7 @@ export default (action = noop, option, deps = []) => {
3042

3143
const { loading, run } = useAsync(actionHandler, {
3244
mannual: true,
33-
onSuccess: res => {
45+
onSuccess: (res: { list?: any }) => {
3446
const prevList = infoRef.current.list
3547
const currentPage = infoRef.current.page
3648

src/layouts/base-layout/index.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@ import React from 'react'
22
import { Switch, Route, Redirect } from 'react-router-dom'
33

44
import Header from 'components/header'
5-
import Tabbar from 'components/tabbar'
5+
import Tabbar, { Tabber } from 'components/tabbar'
66
import Topic from 'view/topic'
77
import Article from 'view/article'
88
import User from 'view/user'
99
import About from 'view/about'
1010
import NotFound from 'view/not-found'
1111
import Layout, { Fixed, Main } from './style'
1212

13-
const navList = [
13+
const navList: Tabber[] = [
1414
{ name: '全部', route: '/topic/all' },
1515
{ name: '精华', route: '/topic/good' },
1616
{ name: '分享', route: '/topic/share' },

0 commit comments

Comments
 (0)