-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(list-picker): selection consumer render children (#1519)
Co-authored-by: shiliqian <shiliqian@growingio.com>
- Loading branch information
1 parent
d06bd39
commit 0485f54
Showing
9 changed files
with
155 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,57 +1,86 @@ | ||
import classNames from 'classnames'; | ||
import React, { useMemo } from 'react'; | ||
import { ListProps, SelectionProps } from './interfance'; | ||
import { isEmpty, isFunction } from 'lodash'; | ||
import toArray from 'rc-util/lib/Children/toArray'; | ||
import { ListProps, SelectionProps, OptionProps } from './interfance'; | ||
import usePrefixCls from '../utils/hooks/use-prefix-cls'; | ||
import { PREFIX } from './constants'; | ||
import { getFlattenOptions } from '../list-picker/util'; | ||
import List from './List'; | ||
import { OptionProps } from '.'; | ||
import { ListContext } from './context'; | ||
|
||
const Selection: React.FC<SelectionProps> & { isSelection: boolean } = (props) => { | ||
const Selection: React.FC<SelectionProps> = (props) => { | ||
const { className, style, options = [], children, ...rest } = props; | ||
const prefixCls = `${usePrefixCls(PREFIX)}--selection`; | ||
const childrens = React.Children.toArray(children); | ||
const isSelection = options?.every((val) => 'groupId' in val) ?? false; | ||
const selectionOptions: { groupId: string; groupName: string; options: OptionProps[] }[] | OptionProps[] = useMemo( | ||
() => getFlattenOptions(options, isSelection), | ||
[isSelection, options] | ||
); | ||
// var cars = [{ make: 'audi', model: 'r8', year: '2012' }, { make: 'audi', model: 'rs5', year: '2013' }, { make: 'ford', model: 'mustang', year: '2012' }, { make: 'ford', model: 'fusion', year: '2015' }, { make: 'kia', model: 'optima', year: '2012' }], | ||
// result = cars.reduce(function (r, a) { | ||
// r[a.make] = r[a.make] || []; | ||
// r[a.make].push(a); | ||
// return r; | ||
// }, Object.create(null)); | ||
|
||
// console.log(result); | ||
if (options.length) { | ||
return ( | ||
<div className={classNames(prefixCls, className)} style={style}> | ||
{isSelection && | ||
(selectionOptions as { groupId: string; groupName: string; options: OptionProps[] }[])?.map((option) => ( | ||
<div className={`${prefixCls}--item`}> | ||
{option?.groupId && <div className={`${prefixCls}--title`}>{option?.groupName}</div>} | ||
<List options={option.options} /> | ||
</div> | ||
))} | ||
{!isSelection && <List options={selectionOptions as OptionProps[]} />} | ||
(selectionOptions as { groupId: string; groupName: string; options: OptionProps[] }[])?.map( | ||
(option) => | ||
!isEmpty(option.options) && ( | ||
<div className={`${prefixCls}--item`}> | ||
{option?.groupId && <div className={`${prefixCls}--title`}>{option?.groupName}</div>} | ||
<List options={option.options} /> | ||
</div> | ||
) | ||
)} | ||
{!isSelection && !isEmpty(selectionOptions) && <List options={selectionOptions as OptionProps[]} />} | ||
</div> | ||
); | ||
} | ||
if (isFunction(children)) { | ||
return ( | ||
<div className={classNames(prefixCls, className)} style={style}> | ||
<ListContext.Consumer> | ||
{(context) => | ||
toArray(children?.(context))?.map((node: React.ReactElement<ListProps>) => | ||
!isEmpty(node?.props.options) || | ||
React.isValidElement(node.props.children) || | ||
!isEmpty(toArray(node.props.children)) ? ( | ||
<div className={`${prefixCls}--item`}> | ||
{node?.props?.title && <div className={`${prefixCls}--title`}>{node?.props?.title}</div>} | ||
{React.cloneElement(node, { | ||
...rest, | ||
...node.props, | ||
})} | ||
</div> | ||
) : ( | ||
React.cloneElement(node, { | ||
...rest, | ||
...node.props, | ||
}) | ||
) | ||
) | ||
} | ||
</ListContext.Consumer> | ||
</div> | ||
); | ||
} | ||
|
||
return ( | ||
<div className={classNames(prefixCls, className)} style={style}> | ||
{childrens?.map((node: React.ReactElement<ListProps>) => ( | ||
<div className={`${prefixCls}--item`}> | ||
{node?.props?.title && <div className={`${prefixCls}--title`}>{node?.props?.title}</div>} | ||
{React.cloneElement(node, { | ||
...rest, | ||
...node.props, | ||
})} | ||
</div> | ||
))} | ||
{toArray(children)?.map( | ||
(node: React.ReactElement<ListProps>) => | ||
(!isEmpty(node?.props.options) || | ||
React.isValidElement(node.props.children) || | ||
!isEmpty(toArray(node.props.children))) && ( | ||
<div className={`${prefixCls}--item`}> | ||
{node?.props?.title && <div className={`${prefixCls}--title`}>{node?.props?.title}</div>} | ||
{React.cloneElement(node, { | ||
...rest, | ||
...node.props, | ||
})} | ||
</div> | ||
) | ||
)} | ||
</div> | ||
); | ||
}; | ||
Selection.isSelection = true; | ||
|
||
export default Selection; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
0485f54
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs: