Skip to content
This repository has been archived by the owner on Mar 27, 2023. It is now read-only.

A SelectOption支持节点 #493

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion packages/react-impression/src/components/Select/Select.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,10 +139,28 @@ export default class Select extends React.PureComponent {
}
}

/**
* @description 获取节点下内容
* @memberof Select
*/
getIntLabel(node) {
function getLabel(target) {
const count = React.Children.count(target)
if (count > 1) return ''
if (!target) return ''
if (typeof target !== 'object') return target.toString()
if (typeof target.props.children === 'string') {
return target.props.children.toString()
}
return getLabel(target.props.children)
}
return getLabel(node)
}

getOptionList = (arr = []) => {
return arr.map(option => {
const { value, children = '' } = option.props
return { value, name: children.toString() }
return { value, name: this.getIntLabel(children) }
})
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,13 @@ export default class SelectOption extends React.PureComponent {
}

get labelName() {
return (this.props.children || '').toString()
return this.parent().getIntLabel(this.props.children)
}

get child() {
const count = React.Children.count(this.props.children)
if (count > 1) return ''
return this.props.children
}

isEqual(a, b) {
Expand Down Expand Up @@ -138,10 +144,10 @@ export default class SelectOption extends React.PureComponent {
defaultMethod(query, this.getLabel())
} else {
const visible = defaultMethod(query, this.getLabel())

this.setState({ visible })
}
}

getLabel() {
return (
this.labelName ||
Expand All @@ -153,7 +159,7 @@ export default class SelectOption extends React.PureComponent {
}

render() {
const { disabled, className, children, ...others } = this.props
const { disabled, className, ...others } = this.props
const { active } = this.state
const { visible } = this.state
const displayStyle = visible ? {} : { display: 'none' }
Expand All @@ -164,7 +170,7 @@ export default class SelectOption extends React.PureComponent {
className={classnames('select-option', { active, disabled }, className)}
onClick={this.optionClickHandle}
>
{children}
{this.child}
</li>
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ export default class SelectOptionGroup extends React.PureComponent {
if (!child) {
return child
}
let { value, children = '' } = child.props
this.options.push(children.toString())
let { children = '' } = child.props
this.options.push(this.parent().getIntLabel(children))
return React.cloneElement(child, {
key: index,
disabled: disabled,
Expand Down