Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: allow use value control selections; #317

Merged
merged 3 commits into from
Oct 15, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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
55 changes: 28 additions & 27 deletions packages/components/src/components/select/Select.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React, { useState, useContext, useMemo, useRef, useEffect } from 'react';
import classnames from 'classnames';

import { UpFilled, DownFilled } from '@gio-design/icons';
import { filter, intersection } from 'lodash';
import { filter, intersection, concat } from 'lodash';
import Dropdown from '../dropdown';
import Input from '../input';
import Tag from '../tag';
Expand Down Expand Up @@ -45,8 +45,9 @@ const Select: React.FC<SelectProps> = (props: SelectProps) => {
const {
multiple = false,
options,
value: _value,
defaultValue,
customizePrefixCls,
defaultSelection = [],
onChange,
size = 'medium',
searchable = false,
Expand All @@ -56,12 +57,17 @@ const Select: React.FC<SelectProps> = (props: SelectProps) => {
listRowHeight = defaultListRowHeight,
width,
getContainer,

// deprecated;
defaultSelection = [],
} = props;

const defaultValues = defaultValue || defaultSelection;
zzt1224 marked this conversation as resolved.
Show resolved Hide resolved
const { getPrefixCls } = useContext(ConfigContext);
const prefix = getPrefixCls('select', customizePrefixCls);
const [selection, _setSelection] = useState(
new Set(Array.isArray(defaultSelection) ? defaultSelection : [defaultSelection])
);
const [_selection, _setSelection] = useState(new Set(concat<string>([], defaultValues)));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

建议直接使用 es6 的 展开运行符 [... defaultValues]

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

传入值是 string | string[] 没办法直接展开吧 这里是逻辑是把string转换成 string[]

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

了解了。好多地方都有同样的逻辑,我们应该可以提供一个通用的方法

const value = new Set(concat<string>([], _value || []));
const selection = _value ? value : _selection;
const [extraOptions, setExtraOptions] = useState<Option[]>([]);
// options { value: index } hashtable;
const [extendedOptions, optionHash, LabelHash, groupCount] = useMemo(() => {
Expand All @@ -71,7 +77,9 @@ const Select: React.FC<SelectProps> = (props: SelectProps) => {
(maps, option, index) => {
maps[0].set(option.value, index);
maps[1].set(option.label, true);
groupKeys.set(option.groupValue, true);
if (option.groupLabel !== undefined) {
groupKeys.set(option.groupLabel, true);
}
return maps;
},
[new Map(), new Map()]
Expand Down Expand Up @@ -109,30 +117,30 @@ const Select: React.FC<SelectProps> = (props: SelectProps) => {
intersection(
Array.from(newSelection),
extraOptions.map((option) => option.value)
).map((value) => freeInputOption(value))
).map((v) => freeInputOption(v))
);
clearInput();
if (onChange) {
const selectedOptions = Array.from(optSelection).map(
(value) => extendedOptions[optionHash.get(value)] || freeInputOption(value)
(v) => extendedOptions[optionHash.get(v)] || freeInputOption(v)
);
onChange(multiple ? selectedOptions : selectedOptions[0]);
}
};

const onSelectChange = (value: string | string[]) => {
setSelection(Array.isArray(value) ? value : [value]);
const onSelectChange = (v: string | string[]) => {
setSelection(Array.isArray(v) ? v : [v]);
};

const onSelected = (value: string) => {
if (optionHash.get(value) === undefined) {
addExtraOptions(value);
const onSelected = (v: string) => {
if (optionHash.get(v) === undefined) {
addExtraOptions(v);
}
};

const addExtraOptions = (value: string) => {
const addExtraOptions = (v: string) => {
if (!hasExactMatch) {
extraOptions.push(freeInputOption(value));
extraOptions.push(freeInputOption(v));
setExtraOptions([...extraOptions]);
}
};
Expand Down Expand Up @@ -161,8 +169,8 @@ const Select: React.FC<SelectProps> = (props: SelectProps) => {
}
};

const onInputChange = (value: string) => {
setInput(value);
const onInputChange = (v: string) => {
setInput(v);
};

const clearInput = () => {
Expand Down Expand Up @@ -197,16 +205,9 @@ const Select: React.FC<SelectProps> = (props: SelectProps) => {
);

const renderMultipleValue = () =>
Array.from(selection).map((value) => (
<Tag
key={value}
data-key={value}
className={`${prefix}-item`}
closable
persistCloseIcon
onClose={onTagCloseClick}
>
{extendedOptions[optionHash.get(value)]?.label}
Array.from(selection).map((v) => (
<Tag key={v} data-key={v} className={`${prefix}-item`} closable persistCloseIcon onClose={onTagCloseClick}>
{extendedOptions[optionHash.get(v)]?.label}
</Tag>
));

Expand Down
8 changes: 6 additions & 2 deletions packages/components/src/components/select/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,20 @@ export interface SelectProps<VT = Option> {
options: VT[];
multiple?: boolean;
placeholder?: string;
defaultSelection?: string | string[];
value?: string | string[];
defaultValue?: string | string[];
searchable?: boolean;
searchPredicate?: (input: string) => _.ListIterateeCustom<VT, boolean>;
labelRenderer?: (input: string) => (option: Option, isGroup: boolean) => React.ReactNode;
labelRenderer?: (input: string, prefix: string) => (option: Option, isGroup: boolean) => React.ReactNode;
onChange?: (option: VT[] | VT) => void;
customizePrefixCls?: string;
width?: number;
listHeight?: number;
listRowHeight?: number;
getContainer?: (node: HTMLElement) => HTMLElement;

// deprecated
defaultSelection?: string | string[];
}

export { Option };
1 change: 1 addition & 0 deletions packages/components/src/components/select/style/index.less
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@
display: inline-block;
width: auto;
max-width: 100%;
height: auto !important;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

为啥为加 !important

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

被别的组件里 .gio-input覆盖了 input的坑

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

有的组件没封装 我这个封在这个组件的scope里了

overflow: hidden;
line-height: 22px;
vertical-align: bottom;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const values = ['all', 'online', 'pending', 'off', 'draft'];
const options = values.map((value, index) => ({
value,
label: labels[index],
groupKey: 'platform',
groupValue: 'platform',
groupLabel: '应用平台',
}));

Expand Down
21 changes: 12 additions & 9 deletions packages/website/src/components/functional/select/demos/search.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react';
import React, { useState } from 'react';
import { Select } from '@gio-design/components';
import '@gio-design/components/es/components/select/style/index.css';

Expand All @@ -8,16 +8,19 @@ const values = ['all', 'online', 'pending', 'off', 'draft'];
const options = values.map((value, index) => ({
value,
label: labels[index],
groupKey: 'platform',
groupValue: 'platform',
groupLabel: '应用平台',
}));

const Basics = (): React.ReactNode => (
<>
<Select options={options} searchable width={200} onChange={console.log} />
<br />
<Select options={options} multiple searchable width={200} onChange={console.log} />
</>
);
const Basics = (): React.ReactNode => {
const [state, setstate] = useState(values[0]);
return (
<>
<Select options={options} searchable width={200} onChange={(option: any) => setstate(option.id)} value={state} />
<br />
<Select options={options} multiple searchable width={200} onChange={console.log} />
</>
);
};

export default Basics;
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ group:

| 参数 | 说明 | 类型 | 默认值 |
| ------------- | --------------------------------------------------- | ------------------------------------------------------------------------- | --------------------- |
| value | 选中值 | `string` | `string[]` |
| defaultValue | 默认选中值 | `string` | `string[]` |
| size | select 的大小 | `small` , `medium` , `large` | `medium` |
| options | 同 list Options | `Options[]` | - |
| multiple | 多选 | `boolean` | `false` |
Expand Down