forked from growingio/gio-design
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove list component dependency fix growingio#677
- Loading branch information
Showing
13 changed files
with
1,380 additions
and
563 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
220 changes: 176 additions & 44 deletions
220
src/components/pagination/__test__/__snapshots__/Pagination.test.js.snap
Large diffs are not rendered by default.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,139 @@ | ||
import React, { useMemo } from 'react'; | ||
import { noop } from 'lodash'; | ||
import classnames from 'classnames'; | ||
import VirtualList from './VirtualList'; | ||
import { OptionsListProps, Option } from './interface'; | ||
import Tooltip from '../tooltip'; | ||
import Checkbox from '../checkbox'; | ||
|
||
const OptionsList: React.FC<OptionsListProps> = (props) => { | ||
const { | ||
labelRenderer, | ||
prefixCls, | ||
data, | ||
groupStyle, | ||
optionStyle, | ||
selected, | ||
multiple, | ||
hasGroup, | ||
height, | ||
itemHeight, | ||
onOptionClick, | ||
optionLabelRenderer, | ||
...restProps | ||
} = props; | ||
|
||
const flettensOptions = useMemo(() => { | ||
const groupMap = new Map(); | ||
if (!hasGroup) return data; | ||
data?.map((cur: Option) => { | ||
const gvalue = groupMap.get(cur.groupValue); | ||
if (gvalue) { | ||
const { options, ...rest } = gvalue; | ||
return groupMap.set(cur.groupValue, { | ||
options: [...options, cur], | ||
...rest, | ||
}); | ||
} | ||
return groupMap.set(cur.groupValue, { | ||
label: cur.groupLabel, | ||
value: cur.groupValue, | ||
isSelectOptGroup: true, | ||
options: [cur], | ||
}); | ||
}); | ||
const flettenOption: Option[] = []; | ||
groupMap.forEach((value) => { | ||
flettenOption.push(value); | ||
flettenOption.push(...value.options); | ||
}); | ||
return flettenOption; | ||
}, [data, hasGroup]); | ||
|
||
const renderGroupItem = (option: Option) => { | ||
const { value, label } = option; | ||
return ( | ||
<div className={`${prefixCls}-list-group`} style={groupStyle} aria-hidden="true"> | ||
{label !== undefined ? label : value} | ||
</div> | ||
); | ||
}; | ||
|
||
const renderTootip = (option: Option, render: JSX.Element & React.ReactNode) => { | ||
const { tooltip } = option; | ||
if (tooltip) { | ||
return ( | ||
<Tooltip title={tooltip} placement="top"> | ||
{render} | ||
</Tooltip> | ||
); | ||
} | ||
return render; | ||
}; | ||
|
||
const renderOption = (option: Option) => { | ||
const { value, disabled, tooltip, groupValue, groupLabel, ...restOption } = option; | ||
const isSelected = | ||
typeof selected === 'string' || typeof selected === 'number' || typeof selected === 'undefined' | ||
? selected === value | ||
: selected.includes(value); | ||
const onClick = (event: React.MouseEvent<HTMLDivElement, MouseEvent>) => { | ||
event.stopPropagation(); | ||
event.preventDefault(); | ||
if (onOptionClick) { | ||
onOptionClick(value); | ||
} | ||
}; | ||
const labelNode = labelRenderer | ||
? labelRenderer( | ||
{ | ||
value, | ||
disabled, | ||
tooltip, | ||
groupValue, | ||
groupLabel, | ||
label: optionLabelRenderer(value, option), | ||
...restOption, | ||
}, | ||
false | ||
) | ||
: optionLabelRenderer(value, option); | ||
return ( | ||
<div className={`${prefixCls}-list-option-container`} style={optionStyle}> | ||
<div | ||
className={classnames(`${prefixCls}-list-option`, { | ||
[`${prefixCls}-list-option-isSelected`]: isSelected, | ||
[`${prefixCls}-list-option-disabled`]: disabled, | ||
})} | ||
onClick={disabled ? undefined : onClick} | ||
onKeyDown={noop} | ||
aria-hidden="true" | ||
> | ||
{multiple && ( | ||
<> | ||
<Checkbox checked={isSelected} disabled={disabled} onChange={noop} /> | ||
<span style={{ width: 10 }} /> | ||
</> | ||
)} | ||
{labelNode} | ||
</div> | ||
</div> | ||
); | ||
}; | ||
return ( | ||
<VirtualList | ||
itemKey="value" | ||
prefixCls={prefixCls} | ||
data={flettensOptions} | ||
height={height} | ||
itemHeight={itemHeight} | ||
{...restProps} | ||
> | ||
{(option: Option & { isSelectOptGroup: boolean }) => { | ||
return option.isSelectOptGroup ? renderGroupItem(option) : renderTootip(option, renderOption(option)); | ||
}} | ||
</VirtualList> | ||
); | ||
}; | ||
|
||
export default OptionsList; |
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.