Skip to content

Commit

Permalink
feat(select): 增加JSX方式添加Option
Browse files Browse the repository at this point in the history
affects: @gio-design/components, website

ISSUES CLOSED: #523, #522, #495, #448
  • Loading branch information
shiliqian committed Dec 4, 2020
1 parent e138f48 commit 4ac9b34
Show file tree
Hide file tree
Showing 15 changed files with 522 additions and 135 deletions.
1 change: 1 addition & 0 deletions packages/components/src/assets/images/BoxFilled.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
83 changes: 51 additions & 32 deletions packages/components/src/components/list/list.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from 'react';
import { get } from 'lodash';
import { List, AutoSizer } from 'react-virtualized';
import { List, AutoSizer, CellMeasurerCache, CellMeasurer } from 'react-virtualized';
import { withConfigConsumer, ConfigConsumerProps } from '../config-provider';
import SelectOption from './option';
import Group from './Group';
Expand All @@ -15,6 +15,11 @@ class SelectList extends React.Component<SelectListProps & ConfigConsumerProps>

public ref: React.RefObject<HTMLDivElement>;

public _cache = new CellMeasurerCache({
fixedWidth: true,
defaultHeight: 40,
});

public constructor(props: SelectListProps & ConfigConsumerProps) {
super(props);
this.ref = React.createRef();
Expand All @@ -37,7 +42,8 @@ class SelectList extends React.Component<SelectListProps & ConfigConsumerProps>
height={400}
style={{ height: height || '100%', overflow: 'auto' }}
rowCount={options.length}
rowHeight={typeof rowHeight === 'function' ? getRowHeight : rowHeight}
rowHeight={typeof rowHeight === 'function' ? getRowHeight : this._cache.rowHeight}
deferredMeasurementCache={this._cache}
rowRenderer={this.renderListItem(options)}
disabledOptions={disabledOptions}
className={`${prefixCls}-list`}
Expand All @@ -55,7 +61,15 @@ class SelectList extends React.Component<SelectListProps & ConfigConsumerProps>
return false;
};

private renderListItem = (options: any) => ({ index, style }: { index: number; style: React.CSSProperties }) => {
private renderListItem = (options: any) => ({
index,
style,
parent,
}: {
index: number;
style: React.CSSProperties;
parent: any;
}) => {
const {
isMultiple,
required,
Expand Down Expand Up @@ -87,35 +101,40 @@ class SelectList extends React.Component<SelectListProps & ConfigConsumerProps>

const groupIcon = getGroupIcon ? getGroupIcon(option.group) : null;

return isGroup ? (
<Group
key={option.label}
name={option.label}
option={option}
style={{ ...style, height: (style.height as number) - 4 }}
icon={groupIcon}
isSelected={this.getSelected(option)}
isMultiple={!!isMultiple}
labelRenderer={labelRenderer}
/>
) : (
<SelectOption
key={key}
style={{ ...style, height: (style.height as number) - 4 }}
option={option}
title={!labelRenderer ? label : undefined}
isSelected={this.getSelected(option)}
isMultiple={!!isMultiple}
allowDuplicate={allowDuplicate}
onSelect={this.handleSelect}
onClick={this.handleClick}
disabled={disabled}
hasGroupIcon={!!groupIcon}
getPopupContainer={getPopupContainer}
placement={placement}
>
{label}
</SelectOption>
return (
<CellMeasurer key={key} cache={this._cache} parent={parent} columnIndex={0} rowIndex={index}>
<div style={{ ...style, height: (style.height as number) - 4 }} className="row">
{isGroup ? (
<Group
key={option.label}
name={option.label}
option={option}
icon={groupIcon}
isSelected={this.getSelected(option)}
isMultiple={!!isMultiple}
labelRenderer={labelRenderer}
/>
) : (
<SelectOption
key={key}
style={{}}
option={option}
title={!labelRenderer ? label : undefined}
isSelected={this.getSelected(option)}
isMultiple={!!isMultiple}
allowDuplicate={allowDuplicate}
onSelect={this.handleSelect}
onClick={this.handleClick}
disabled={disabled}
hasGroupIcon={!!groupIcon}
getPopupContainer={getPopupContainer}
placement={placement}
>
{label}
</SelectOption>
)}
</div>
</CellMeasurer>
);
};

Expand Down
11 changes: 10 additions & 1 deletion packages/components/src/components/list/style/option.less
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

.@{select-prefix-cls}-option {
max-width: 100%;
height: 40px;
padding: 0 25px 0 16px;
overflow: hidden;
color: @color-font-list-primary-normal;
Expand All @@ -12,6 +11,7 @@
text-overflow: ellipsis;
border-radius: 5px;
transition: background-color 0.3s ease;

&:not(.group) {
display: flex;
align-items: center;
Expand All @@ -25,12 +25,15 @@
&.selected {
color: @color-font-list-primary-active;
background-color: @color-background-list-single-active;

&:hover {
background-color: @color-background-list-single-hover;
}

.@{list-prefix-cls}-option-title {
color: @color-font-list-primary-active;
}

.@{list-prefix-cls}-option-desc {
color: @color-font-list-secondary-active;
}
Expand All @@ -40,13 +43,16 @@
&.selected {
color: @color-font-list-primary-normal;
background-color: inherit;

.@{list-prefix-cls}-option-title {
color: @color-font-list-primary-normal;
}

.@{list-prefix-cls}-option-desc {
color: @color-font-list-secondary-normal;
}
}

&:hover {
background-color: @color-background-list-multiple-hover;
}
Expand All @@ -55,6 +61,7 @@
.@{list-prefix-cls}-option-title {
color: @color-font-list-primary-normal;
}

.@{list-prefix-cls}-option-desc {
color: @color-font-list-secondary-normal;
}
Expand All @@ -63,9 +70,11 @@
color: @color-font-list-primary-disable;
background-color: @color-background-list-multiple-disable;
cursor: not-allowed;

.@{list-prefix-cls}-option-title {
color: @color-font-list-primary-disable;
}

.@{list-prefix-cls}-option-desc {
color: @color-font-list-secondary-disable;
}
Expand Down
12 changes: 12 additions & 0 deletions packages/components/src/components/select/OptGroup.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import React from 'react';
import { OptGroupProps } from './interface'


export interface OptionGroupFC extends React.FC<OptGroupProps> {
isSelectOptGroup: boolean;
}
const OptGroup: OptionGroupFC = () => null;

OptGroup.isSelectOptGroup = true;

export default OptGroup;
12 changes: 12 additions & 0 deletions packages/components/src/components/select/Options.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import React from 'react';
import { OptionProps } from './interface'


export interface OptionFC extends React.FC<OptionProps> {
isSelectOption: boolean;
}
const Option: OptionFC = () => null;

Option.isSelectOption = true;

export default Option;
Loading

0 comments on commit 4ac9b34

Please sign in to comment.