Skip to content

Commit a10afae

Browse files
authored
Merge pull request #1668 from axetroy/list_item_jsx_params
feat: 修改 ListItem 参数 title/note/extraText 由 String 类型改为 JSX.Element
2 parents 68d4a00 + 1402784 commit a10afae

File tree

3 files changed

+26
-12
lines changed

3 files changed

+26
-12
lines changed

packages/taro-ui-docs/markdown/list.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -159,12 +159,12 @@ import { AtList, AtListItem } from "taro-ui"
159159

160160
| 参数 | 说明 | 类型 | 可选值 | 默认值 |
161161
| ------------- | ---------------- | -------------------------------------------------------------------- | ---------------------- | ------- |
162-
| title | 元素的标题 | String | - | - |
162+
| title | 元素的标题 | JSX.Element | - | - |
163163
| disabled | 是否禁用 | Boolean | - | `false` |
164-
| note | 元素的描述信息 | String | - | - |
164+
| note | 元素的描述信息 | JSX.Element | - | - |
165165
| thumb | 元素的主要缩略图 | String | - | - |
166166
| arrow | 箭头的方向 | String | `right`,`top`,`down` | - |
167-
| extraText | 额外信息的文本 | String | - | - |
167+
| extraText | 额外信息的文本 | JSX.Element | - | - |
168168
| extraThumb | 额外信息的缩略图 | String | - | - |
169169
| isSwitch | 额外信息是否开关 | Boolean | - | `false` |
170170
| switchColor | 开关的颜色 | String | - | `#6190E8` |

packages/taro-ui/src/common/utils.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import Taro from '@tarojs/taro'
2+
import React from 'react'
23
import { SelectorQuery } from '@tarojs/taro/types/index'
34

45
const ENV = Taro.getEnv()
@@ -255,6 +256,21 @@ function mergeStyle(
255256
return objectToString(style1) + objectToString(style2)
256257
}
257258

259+
/**
260+
* 自定义验证器,用于验证参数是否是 JSX.Element
261+
* @param {any} props
262+
* @param {string} propName
263+
* @param {string} componentName
264+
* @returns
265+
*/
266+
function isJSXElement(props, propName, componentName) {
267+
if (!React.isValidElement(props[propName])) {
268+
return new Error(
269+
`Invalid prop ${propName} supplied to ${componentName}. It must be a valid JSX element.`
270+
)
271+
}
272+
}
273+
258274
export {
259275
delay,
260276
delayQuerySelector,
@@ -266,5 +282,6 @@ export {
266282
handleTouchScroll,
267283
delayGetClientRect,
268284
delayGetScrollOffset,
269-
mergeStyle
285+
mergeStyle,
286+
isJSXElement
270287
}

packages/taro-ui/src/components/list/item/index.tsx

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import React from 'react'
44
import { Image, Switch, Text, View } from '@tarojs/components'
55
import { CommonEvent, ITouchEvent } from '@tarojs/components/types/common'
66
import { AtListItemProps } from '../../../../types/list'
7-
import { mergeStyle } from '../../../common/utils'
7+
import { mergeStyle, isJSXElement } from '../../../common/utils'
88

99
export default class AtListItem extends React.Component<AtListItemProps> {
1010
public static defaultProps: AtListItemProps
@@ -43,10 +43,7 @@ export default class AtListItem extends React.Component<AtListItemProps> {
4343
switchIsCheck
4444
} = this.props
4545

46-
let { extraText, title } = this.props
47-
48-
extraText = String(extraText)
49-
title = String(title)
46+
const { extraText, title } = this.props
5047

5148
const rootClass = classNames(
5249
'at-list__item',
@@ -156,16 +153,16 @@ AtListItem.defaultProps = {
156153
}
157154

158155
AtListItem.propTypes = {
159-
note: PropTypes.string,
156+
note: isJSXElement,
160157
disabled: PropTypes.bool,
161-
title: PropTypes.string,
158+
title: isJSXElement,
162159
thumb: PropTypes.string,
163160
onClick: PropTypes.func,
164161
isSwitch: PropTypes.bool,
165162
hasBorder: PropTypes.bool,
166163
switchColor: PropTypes.string,
167164
switchIsCheck: PropTypes.bool,
168-
extraText: PropTypes.string,
165+
extraText: isJSXElement,
169166
extraThumb: PropTypes.string,
170167
onSwitchChange: PropTypes.func,
171168
arrow: PropTypes.oneOf(['up', 'down', 'right']),

0 commit comments

Comments
 (0)