Skip to content

Commit 1f5bc86

Browse files
committed
refactor: 更改组件 API 使用 React
1 parent 19e3460 commit 1f5bc86

File tree

69 files changed

+494
-568
lines changed

Some content is hidden

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

69 files changed

+494
-568
lines changed

package.json

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,13 @@
3030
"eslintConfig": {
3131
"extends": [
3232
"o2team",
33-
"taro",
33+
"plugin:react/recommended",
3434
"plugin:prettier/recommended"
3535
],
3636
"parser": "babel-eslint",
37+
"plugins": [
38+
"react"
39+
],
3740
"env": {
3841
"jest": true
3942
},
@@ -45,7 +48,7 @@
4548
"no-unused-vars": [
4649
"error",
4750
{
48-
"varsIgnorePattern": "Taro"
51+
"varsIgnorePattern": "React"
4952
}
5053
],
5154
"no-console": [
@@ -94,14 +97,19 @@
9497
"react",
9598
"@typescript-eslint"
9699
],
97-
"extends": "plugin:prettier/recommended",
100+
"extends": [
101+
"eslint:recommended",
102+
"plugin:@typescript-eslint/eslint-recommended",
103+
"plugin:@typescript-eslint/recommended",
104+
"plugin:react/recommended",
105+
"plugin:prettier/recommended"
106+
],
98107
"env": {
99108
"jest": true
100109
},
101110
"rules": {
102-
"no-undef": 0,
103-
"no-unused-vars": 0,
104-
"@typescript-eslint/class-name-casing": 2,
111+
"@typescript-eslint/no-explicit-any": ["off"],
112+
"@typescript-eslint/member-delimiter-style": ["off"],
105113
"class-methods-use-this": "off",
106114
"prefer-rest-params": "off",
107115
"arrow-body-style": "warn",
@@ -115,8 +123,7 @@
115123
".tsx"
116124
]
117125
}
118-
],
119-
"taro/custom-component-children": "off"
126+
]
120127
}
121128
}
122129
]
@@ -212,12 +219,10 @@
212219
"clean": "rimraf .temp dist coverage"
213220
},
214221
"dependencies": {
215-
"@types/react": "^16.7.3",
216222
"bind-decorator": "^1.0.11",
217223
"classnames": "^2.2.6",
218224
"dayjs": "^1.7.7",
219-
"lodash": "^4.17.10",
220-
"prop-types": "^15.6.2"
225+
"lodash": "^4.17.10"
221226
},
222227
"devDependencies": {
223228
"@babel/core": "^7.0.0-rc.1",
@@ -244,6 +249,7 @@
244249
"@tarojs/taro-weapp": "^2.0.3",
245250
"@tarojs/webpack-runner": "^2.0.3",
246251
"@types/classnames": "^2.2.9",
252+
"@types/react": "^16.9.23",
247253
"@types/webpack-env": "^1.13.6",
248254
"@typescript-eslint/eslint-plugin": "^2.10.0",
249255
"@typescript-eslint/parser": "^2.10.0",
@@ -300,7 +306,9 @@
300306
"postcss": "^7.0.2",
301307
"postcss-loader": "^3.0.0",
302308
"prettier": "^1.19.1",
309+
"prop-types": "^15.7.2",
303310
"qrcode.react": "^1.0.0",
311+
"react": "^16.13.0",
304312
"react-markdown-loader": "^1.1.14",
305313
"react-router-dom": "^5.1.2",
306314
"regenerator-runtime": "^0.13.3",
@@ -317,5 +325,9 @@
317325
"webpack-dev-server": "^3.1.5",
318326
"webpack-merge": "^4.1.3",
319327
"yaml-loader": "^0.5.0"
328+
},
329+
"peerDependencies": {
330+
"prop-types": ">=15.7.2",
331+
"react": ">=16.13.0"
320332
}
321333
}

src/common/component.tsx

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Component } from '@tarojs/taro'
1+
import { Component } from 'react'
22

33
const objectToString = (style: object | string): string => {
44
if (style && typeof style === 'object') {
@@ -15,10 +15,6 @@ const objectToString = (style: object | string): string => {
1515
}
1616

1717
export default class AtComponent<P = {}, S = {}> extends Component<P, S> {
18-
static options = {
19-
addGlobalClass: true
20-
}
21-
2218
/**
2319
* 合并 style
2420
* @param {Object|String} style1

src/common/utils.ts

Lines changed: 47 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import Taro from '@tarojs/taro'
2-
import { execObject, SelectorQuery } from '@tarojs/taro/types/index'
2+
import { SelectorQuery } from '@tarojs/taro/types/index'
33

44
const ENV = Taro.getEnv()
55

@@ -19,29 +19,28 @@ function delayQuerySelector(
1919
self,
2020
selectorStr: string,
2121
delayTime = 500
22-
): Promise<Array<execObject>> {
23-
const $scope = ENV === Taro.ENV_TYPE.WEB ? self : self.$scope
24-
const selector: SelectorQuery = Taro.createSelectorQuery().in($scope)
22+
): Promise<any[]> {
23+
const selector: SelectorQuery = Taro.createSelectorQuery()
2524

2625
return new Promise(resolve => {
2726
delay(delayTime).then(() => {
2827
selector
2928
.select(selectorStr)
3029
.boundingClientRect()
31-
.exec((res: Array<execObject>) => {
30+
.exec((res: any[]) => {
3231
resolve(res)
3332
})
3433
})
3534
})
3635
}
3736

38-
function delayGetScrollOffset({ delayTime = 500 }): Promise<Array<execObject>> {
37+
function delayGetScrollOffset({ delayTime = 500 }): Promise<any[]> {
3938
return new Promise(resolve => {
4039
delay(delayTime).then(() => {
4140
Taro.createSelectorQuery()
4241
.selectViewport()
4342
.scrollOffset()
44-
.exec((res: Array<execObject>) => {
43+
.exec((res: any[]) => {
4544
resolve(res)
4645
})
4746
})
@@ -52,17 +51,15 @@ function delayGetClientRect({
5251
self,
5352
selectorStr,
5453
delayTime = 500
55-
}): Promise<Array<execObject>> {
56-
const $scope =
57-
ENV === Taro.ENV_TYPE.WEB || ENV === Taro.ENV_TYPE.SWAN ? self : self.$scope
58-
const selector: SelectorQuery = Taro.createSelectorQuery().in($scope)
54+
}): Promise<any[]> {
55+
const selector: SelectorQuery = Taro.createSelectorQuery()
5956

6057
return new Promise(resolve => {
6158
delay(delayTime).then(() => {
6259
selector
6360
.select(selectorStr)
6461
.boundingClientRect()
65-
.exec((res: Array<execObject>) => {
62+
.exec((res: any[]) => {
6663
resolve(res)
6764
})
6865
})
@@ -215,7 +212,7 @@ function handleTouchScroll(flag: any): void {
215212
// 把脱离文档流的body拉上去!否则页面会回到顶部!
216213
document.body.style.top = `${-scrollTop}px`
217214
} else {
218-
document.body.style.top = null
215+
document.body.style.top = ''
219216
document.body.classList.remove('at-frozen')
220217

221218
document.documentElement.scrollTop = scrollTop
@@ -227,6 +224,41 @@ function pxTransform(size: number): string {
227224
return Taro.pxTransform(size)
228225
}
229226

227+
function objectToString(style: object | string): string {
228+
if (style && typeof style === 'object') {
229+
let styleStr = ''
230+
Object.keys(style).forEach(key => {
231+
const lowerCaseKey = key.replace(/([A-Z])/g, '-$1').toLowerCase()
232+
styleStr += `${lowerCaseKey}:${style[key]};`
233+
})
234+
return styleStr
235+
} else if (style && typeof style === 'string') {
236+
return style
237+
}
238+
return ''
239+
}
240+
241+
/**
242+
* 合并 style
243+
* @param {Object|String} style1
244+
* @param {Object|String} style2
245+
* @returns {String}
246+
*/
247+
function mergeStyle(
248+
style1: object | string,
249+
style2: object | string
250+
): object | string {
251+
if (
252+
style1 &&
253+
typeof style1 === 'object' &&
254+
style2 &&
255+
typeof style2 === 'object'
256+
) {
257+
return Object.assign({}, style1, style2)
258+
}
259+
return objectToString(style1) + objectToString(style2)
260+
}
261+
230262
export {
231263
delay,
232264
delayQuerySelector,
@@ -237,5 +269,6 @@ export {
237269
pxTransform,
238270
handleTouchScroll,
239271
delayGetClientRect,
240-
delayGetScrollOffset
272+
delayGetScrollOffset,
273+
mergeStyle
241274
}

src/components/accordion/index.tsx

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,12 @@
11
import classNames from 'classnames'
22
import PropTypes, { InferProps } from 'prop-types'
3+
import React from 'react'
34
import { AtAccordionProps, AtAccordionState } from 'types/accordion'
45
import { Text, View } from '@tarojs/components'
56
import { CommonEvent } from '@tarojs/components/types/common'
6-
import Taro from '@tarojs/taro'
7-
import AtComponent from '../../common/component'
8-
import { delayQuerySelector, initTestEnv } from '../../common/utils'
7+
import { delayQuerySelector } from '../../common/utils'
98

10-
initTestEnv()
11-
12-
// 文档
13-
export default class AtAccordion extends AtComponent<
9+
export default class AtAccordion extends React.Component<
1410
AtAccordionProps,
1511
AtAccordionState
1612
> {
@@ -69,7 +65,7 @@ export default class AtAccordion extends AtComponent<
6965
})
7066
}
7167

72-
public componentWillReceiveProps(nextProps: AtAccordionProps): void {
68+
public UNSAFE_componentWillReceiveProps(nextProps: AtAccordionProps): void {
7369
if (nextProps.open !== this.props.open) {
7470
this.startOpen = !!nextProps.open && !!nextProps.isAnimation
7571
this.toggleWithAnimation()
@@ -145,8 +141,7 @@ AtAccordion.defaultProps = {
145141
note: '',
146142
icon: { value: '' },
147143
hasBorder: true,
148-
isAnimation: true,
149-
onClick: () => {}
144+
isAnimation: true
150145
}
151146

152147
AtAccordion.propTypes = {

src/components/action-sheet/body/index.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
import classNames from 'classnames'
2+
import React from 'react'
23
import { AtActionSheetBodyProps } from 'types/action-sheet'
34
import { View } from '@tarojs/components'
4-
import Taro from '@tarojs/taro'
5-
import AtComponent from '../../../common/component'
65

7-
export default class AtActionSheetBody extends AtComponent<
6+
export default class AtActionSheetBody extends React.Component<
87
AtActionSheetBodyProps
98
> {
109
public render(): JSX.Element {

src/components/action-sheet/body/item/index.tsx

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
import classNames from 'classnames'
22
import _isFunction from 'lodash/isFunction'
33
import PropTypes, { InferProps } from 'prop-types'
4+
import React from 'react'
45
import { AtActionSheetItemProps } from 'types/action-sheet'
56
import { View } from '@tarojs/components'
6-
import Taro from '@tarojs/taro'
7-
import AtComponent from '../../../../common/component'
87

9-
export default class AtActionSheetItem extends AtComponent<
8+
export default class AtActionSheetItem extends React.Component<
109
AtActionSheetItemProps
1110
> {
1211
public static defaultProps: AtActionSheetItemProps
@@ -29,10 +28,6 @@ export default class AtActionSheetItem extends AtComponent<
2928
}
3029
}
3130

32-
AtActionSheetItem.defaultProps = {
33-
onClick: () => {}
34-
}
35-
3631
AtActionSheetItem.propTypes = {
3732
onClick: PropTypes.func
3833
}

src/components/action-sheet/footer/index.tsx

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
import classNames from 'classnames'
22
import _isFunction from 'lodash/isFunction'
33
import PropTypes, { InferProps } from 'prop-types'
4+
import React from 'react'
45
import { AtActionSheetFooterProps } from 'types/action-sheet'
56
import { View } from '@tarojs/components'
6-
import Taro from '@tarojs/taro'
7-
import AtComponent from '../../../common/component'
87

9-
export default class AtActionSheetFooter extends AtComponent<
8+
export default class AtActionSheetFooter extends React.Component<
109
AtActionSheetFooterProps
1110
> {
1211
public static defaultProps: AtActionSheetFooterProps
@@ -32,10 +31,6 @@ export default class AtActionSheetFooter extends AtComponent<
3231
}
3332
}
3433

35-
AtActionSheetFooter.defaultProps = {
36-
onClick: () => {}
37-
}
38-
3934
AtActionSheetFooter.propTypes = {
4035
onClick: PropTypes.func
4136
}

src/components/action-sheet/header/index.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
import classNames from 'classnames'
2+
import React from 'react'
23
import { AtActionSheetHeaderProps } from 'types/action-sheet'
34
import { View } from '@tarojs/components'
4-
import Taro from '@tarojs/taro'
5-
import AtComponent from '../../../common/component'
65

7-
export default class AtActionSheetHeader extends AtComponent<
6+
export default class AtActionSheetHeader extends React.Component<
87
AtActionSheetHeaderProps
98
> {
109
public render(): JSX.Element {

src/components/action-sheet/index.tsx

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
import classNames from 'classnames'
22
import _isFunction from 'lodash/isFunction'
33
import PropTypes, { InferProps } from 'prop-types'
4+
import React from 'react'
45
import { AtActionSheetProps, AtActionSheetState } from 'types/action-sheet'
56
import { View } from '@tarojs/components'
67
import { CommonEvent } from '@tarojs/components/types/common'
7-
import Taro from '@tarojs/taro'
8-
import AtComponent from '../../common/component'
98
import AtActionSheetBody from './body/index'
109
import AtActionSheetFooter from './footer/index'
1110
import AtActionSheetHeader from './header/index'
1211

13-
export default class AtActionSheet extends AtComponent<
12+
export default class AtActionSheet extends React.Component<
1413
AtActionSheetProps,
1514
AtActionSheetState
1615
> {
@@ -26,7 +25,7 @@ export default class AtActionSheet extends AtComponent<
2625
}
2726
}
2827

29-
public componentWillReceiveProps(nextProps: AtActionSheetProps): void {
28+
public UNSAFE_componentWillReceiveProps(nextProps: AtActionSheetProps): void {
3029
const { isOpened } = nextProps
3130
if (isOpened !== this.state._isOpened) {
3231
this.setState({

src/components/activity-indicator/index.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
import classNames from 'classnames'
22
import PropTypes, { InferProps } from 'prop-types'
3+
import React from 'react'
34
import { AtActivityIndicatorProps } from 'types/activity-indicator'
45
import { Text, View } from '@tarojs/components'
5-
import Taro from '@tarojs/taro'
6-
import AtComponent from '../../common/component'
76
import AtLoading from '../loading/index'
87

9-
export default class AtActivityIndicator extends AtComponent<
8+
export default class AtActivityIndicator extends React.Component<
109
AtActivityIndicatorProps
1110
> {
1211
public static defaultProps: AtActivityIndicatorProps

0 commit comments

Comments
 (0)