Skip to content

Commit

Permalink
feat(select): support panel content and showArrow and inputProps apis
Browse files Browse the repository at this point in the history
  • Loading branch information
uyarn committed Jan 21, 2022
1 parent fdb7ff5 commit c126604
Show file tree
Hide file tree
Showing 8 changed files with 220 additions and 13 deletions.
83 changes: 83 additions & 0 deletions src/select/__tests__/__snapshots__/select.test.tsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -808,6 +808,89 @@ exports[`options.jsx 1`] = `
</DocumentFragment>
`;

exports[`panel.jsx 1`] = `
<DocumentFragment>
<div
style="display: flex;"
>
<div
class="t-select__wrap"
style="width: 300px; margin-right: 20px;"
>
<div
class="t-popup__reference t-select__popup-reference"
>
<div
class="t-select"
style="user-select: none;"
>
<span
class="t-select__left-icon"
/>
<span
class="t-select__placeholder"
>
请选择云解决方案
</span>
<svg
class="t-fake-arrow t-select__right-icon"
fill="none"
height="16"
viewBox="0 0 16 16"
width="16"
xmlns="http://www.w3.org/2000/svg"
>
<path
d="M3.75 5.7998L7.99274 10.0425L12.2361 5.79921"
stroke="black"
stroke-opacity="0.9"
stroke-width="1.3"
/>
</svg>
</div>
</div>
</div>
<div
class="t-select__wrap"
style="width: 300px;"
>
<div
class="t-popup__reference t-select__popup-reference"
>
<div
class="t-select"
style="user-select: none;"
>
<span
class="t-select__left-icon"
/>
<span
class="t-select__placeholder"
>
请选择云产品
</span>
<svg
class="t-fake-arrow t-select__right-icon"
fill="none"
height="16"
viewBox="0 0 16 16"
width="16"
xmlns="http://www.w3.org/2000/svg"
>
<path
d="M3.75 5.7998L7.99274 10.0425L12.2361 5.79921"
stroke="black"
stroke-opacity="0.9"
stroke-width="1.3"
/>
</svg>
</div>
</div>
</div>
</div>
</DocumentFragment>
`;

exports[`popup-props.jsx 1`] = `
<DocumentFragment>
<div
Expand Down
76 changes: 76 additions & 0 deletions src/select/_example/panel.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import React, { useState } from 'react';
import { Select, Textarea, Button, Input } from 'tdesign-react';

const OPTIONS = [
{ label: '架构云', value: '1' },
{ label: '大数据', value: '2' },
{ label: '区块链', value: '3' },
{ label: '物联网', value: '4', disabled: true },
{ label: '人工智能', value: '5' },
];

export default function PanelExample() {
const [topOptions, setTopOptions] = useState(OPTIONS);
const [bottomOptions, setBottomOptions] = useState(OPTIONS);
const [editOrCreate, toggleEditOrCreate] = useState('edit');

const [inputVal, changeInputVal] = useState('');

const handleOnSearch = (v) => {
const filteredValue = OPTIONS.filter((item) => item.label.indexOf(v) !== -1);
setTopOptions(filteredValue);
};

const handleClickConfirm = () => {
const id = Math.round(Math.random() * 100);
const newBottomOptions = bottomOptions.concat({ label: inputVal, value: id });
setBottomOptions(newBottomOptions);
changeInputVal('');
toggleEditOrCreate('edit');
};
return (
<div style={{ display: 'flex' }}>
<Select
clearable
placeholder="请选择云解决方案"
style={{ width: '300px', marginRight: '20px' }}
options={topOptions}
panelTopContent={
<div>
<Textarea placeholder="请输入关键词搜索" onChange={handleOnSearch} />
</div>
}
></Select>
<Select
placeholder="请选择云产品"
style={{ width: '300px' }}
options={bottomOptions}
clearable
panelBottomContent={
<div className="select-panel-footer">
{editOrCreate === 'edit' ? (
<Button theme="primary" variant="text" onClick={() => toggleEditOrCreate('create')}>
新增选项
</Button>
) : (
<div>
<Input autoFocus value={inputVal} onChange={(v) => changeInputVal(v)}></Input>
<Button size="small" style={{ marginTop: '12px' }} onClick={handleClickConfirm}>
确认
</Button>
<Button
theme="default"
size="small"
style={{ marginTop: '12px', marginLeft: '8px' }}
onClick={() => toggleEditOrCreate('edit')}
>
取消
</Button>
</div>
)}
</div>
}
></Select>
</div>
);
}
32 changes: 23 additions & 9 deletions src/select/base/PopupContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,18 @@ type OptionsType = TdOptionProps[];
interface SelectPopupProps
extends Pick<
TdSelectProps,
'value' | 'size' | 'multiple' | 'empty' | 'options' | 'max' | 'loadingText' | 'loading' | 'valueType' | 'keys'
| 'value'
| 'size'
| 'multiple'
| 'empty'
| 'options'
| 'max'
| 'loadingText'
| 'loading'
| 'valueType'
| 'keys'
| 'panelTopContent'
| 'panelBottomContent'
> {
onChange?: (value: SelectValue, context?: { label?: string | number; restData?: Record<string, any> }) => void;
/**
Expand Down Expand Up @@ -40,6 +51,8 @@ const PopupContent = (props: SelectPopupProps) => {
valueType,
children,
keys,
panelTopContent,
panelBottomContent,
} = props;

// 国际化文本初始化
Expand Down Expand Up @@ -115,14 +128,15 @@ const PopupContent = (props: SelectPopupProps) => {

const isEmpty = (Array.isArray(childrenWithProps) && !childrenWithProps.length) || (options && options.length === 0);

if (isEmpty) {
return <div className={`${classPrefix}-select__empty`}>{empty ? empty : <p>{emptyText}</p>}</div>;
}

if (loading) {
return <div className={`${classPrefix}-select__loading-tips`}>{loadingText}</div>;
}
return <div>{renderOptions()}</div>;
return (
<div>
{panelTopContent}
{isEmpty && <div className={`${classPrefix}-select__empty`}>{empty ? empty : <p>{emptyText}</p>}</div>}
{!isEmpty && loading && <div className={`${classPrefix}-select__loading-tips`}>{loadingText}</div>}
{!isEmpty && !loading && renderOptions()}
{panelBottomContent}
</div>
);
};

export default PopupContent;
11 changes: 10 additions & 1 deletion src/select/base/Select.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ const Select = forwardRefWithStatics(
valueDisplay,
onEnter,
onVisibleChange,
showArrow = true,
inputProps,
panelBottomContent,
panelTopContent,
} = useDefaultValue(props);

const { classPrefix } = useConfig();
Expand Down Expand Up @@ -324,6 +328,7 @@ const Select = forwardRefWithStatics(
onFocus={(_, context) => onFocus?.({ value, e: context?.e })}
onBlur={(_, context) => onBlur?.({ value, e: context?.e })}
onEnter={(_, context) => onEnter?.({ inputValue: inputVal, value, e: context?.e })}
{...inputProps}
/>
);

Expand Down Expand Up @@ -365,7 +370,9 @@ const Select = forwardRefWithStatics(
/>
);
}
return <FakeArrow overlayClassName={`${name}__right-icon`} isActive={showPopup} disabled={disabled} />;
return (
showArrow && <FakeArrow overlayClassName={`${name}__right-icon`} isActive={showPopup} disabled={disabled} />
);
};

const popupContentProps = {
Expand All @@ -383,6 +390,8 @@ const Select = forwardRefWithStatics(
loading,
valueType,
keys,
panelBottomContent,
panelTopContent,
};

const renderContent = () => <PopupContent {...popupContentProps}>{children}</PopupContent>;
Expand Down
7 changes: 7 additions & 0 deletions src/select/select.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

名称 | 类型 | 默认值 | 说明 | 必传
-- | -- | -- | -- | --
className | String | - | 类名 | N
style | Object | - | 样式,TS 类型:`React.CSSProperties` | N
bordered | Boolean | true | 是否有边框 | N
clearable | Boolean | false | 是否可以清空选项 | N
collapsedItems | TElement | - | 多选情况下,用于设置折叠项内容,默认为 `+N`。如果需要悬浮就显示其他内容,可以使用 collapsedItems 自定义。TS 类型:`TNode<{ value: T[]; collapsedSelectedItems: T[]; count: number }>`[通用类型定义](https://github.com/Tencent/tdesign-react/blob/develop/src/common.ts) | N
Expand All @@ -13,6 +15,7 @@ disabled | Boolean | false | 是否禁用组件 | N
empty | TNode | '' | 当下拉列表为空时显示的内容。TS 类型:`string | TNode`[通用类型定义](https://github.com/Tencent/tdesign-react/blob/develop/src/common.ts) | N
filter | Function | - | 自定义过滤方法,用于对现有数据进行搜索过滤,判断是否过滤某一项数据。TS 类型:`(filterWords: string, option: T) => boolean | Promise<boolean>` | N
filterable | Boolean | false | 是否可搜索 | N
inputProps | Object | - | 透传 Input 组件全部属性。TS 类型:`InputProps`[详细类型定义](https://github.com/Tencent/tdesign-react/blob/develop/src/select/type.ts) | N
keys | Object | - | 用来定义 value / label 在 `options` 中对应的字段别名。TS 类型:`SelectKeysType`[通用类型定义](https://github.com/Tencent/tdesign-react/blob/develop/src/common.ts)[详细类型定义](https://github.com/Tencent/tdesign-react/blob/develop/src/select/type.ts) | N
loading | Boolean | false | 是否为加载状态 | N
loadingText | TNode | '' | 远程加载时显示的文字,支持自定义。如加上超链接。TS 类型:`string | TNode`[通用类型定义](https://github.com/Tencent/tdesign-react/blob/develop/src/common.ts) | N
Expand Down Expand Up @@ -46,6 +49,8 @@ onVisibleChange | Function | | TS 类型:`(visible: boolean) => void`<br/>下

名称 | 类型 | 默认值 | 说明 | 必传
-- | -- | -- | -- | --
className | String | - | 类名 | N
style | Object | - | 样式,TS 类型:`React.CSSProperties` | N
children | TNode | - | 用于定义复杂的选项内容,同 content。TS 类型:`string | TNode`[通用类型定义](https://github.com/Tencent/tdesign-react/blob/develop/src/common.ts) | N
content | TNode | - | 用于定义复杂的选项内容。TS 类型:`string | TNode`[通用类型定义](https://github.com/Tencent/tdesign-react/blob/develop/src/common.ts) | N
disabled | Boolean | false | 是否禁用该选项 | N
Expand All @@ -56,5 +61,7 @@ value | String / Number | - | 选项值 | N

名称 | 类型 | 默认值 | 说明 | 必传
-- | -- | -- | -- | --
className | String | - | 类名 | N
style | Object | - | 样式,TS 类型:`React.CSSProperties` | N
divider | Boolean | true | 是否显示分隔线 | N
label | String | - | 分组别名 | N
20 changes: 18 additions & 2 deletions src/select/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

/**
* 该文件为脚本自动生成文件,请勿随意修改。如需修改请联系 PMC
* updated at 2021-12-27 17:08:43
* */

import { InputProps } from '../input';
import { PopupProps } from '../popup';
import { TNode, TElement, SizeEnum } from '../common';
import { MouseEvent, KeyboardEvent, FocusEvent } from 'react';
Expand Down Expand Up @@ -48,6 +48,10 @@ export interface TdSelectProps<T extends SelectOption = SelectOption> {
* @default false
*/
filterable?: boolean;
/**
* 透传 Input 组件全部属性
*/
inputProps?: InputProps;
/**
* 用来定义 value / label 在 `options` 中对应的字段别名
*/
Expand Down Expand Up @@ -82,9 +86,16 @@ export interface TdSelectProps<T extends SelectOption = SelectOption> {
* @default []
*/
options?: Array<T>;
/**
* 面板内的底部内容
*/
panelBottomContent?: TNode;
/**
* 面板内的顶部内容
*/
panelTopContent?: TNode;
/**
* 占位符
* @default ''
*/
placeholder?: string;
/**
Expand All @@ -100,6 +111,11 @@ export interface TdSelectProps<T extends SelectOption = SelectOption> {
* @default false
*/
reserveKeyword?: boolean;
/**
* 是否显示右侧箭头,默认显示
* @default true
*/
showArrow?: boolean;
/**
* 组件尺寸
* @default medium
Expand Down
2 changes: 2 additions & 0 deletions test/ssr/__snapshots__/ssr.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,8 @@ exports[`ssr snapshot test renders ./src/select/_example/noborder.jsx correctly

exports[`ssr snapshot test renders ./src/select/_example/options.jsx correctly 1`] = `"<div class=\\"t-select__wrap\\" style=\\"width:40%\\"><div class=\\"t-popup__reference t-select__popup-reference\\"><div class=\\"t-select\\" style=\\"user-select:none\\"><span class=\\"t-select__left-icon\\"></span><div class=\\"t-select__input t-input\\"><input class=\\"t-input__inner\\" placeholder=\\"-请选择-\\" value=\\"\\"/></div><svg class=\\"t-fake-arrow t-select__right-icon\\" width=\\"16\\" height=\\"16\\" viewBox=\\"0 0 16 16\\" fill=\\"none\\" xmlns=\\"http://www.w3.org/2000/svg\\"><path d=\\"M3.75 5.7998L7.99274 10.0425L12.2361 5.79921\\" stroke=\\"black\\" stroke-opacity=\\"0.9\\" stroke-width=\\"1.3\\"></path></svg></div></div></div>"`;

exports[`ssr snapshot test renders ./src/select/_example/panel.jsx correctly 1`] = `"<div style=\\"display:flex\\" data-reactroot=\\"\\"><div class=\\"t-select__wrap\\" style=\\"width:300px;margin-right:20px\\"><div class=\\"t-popup__reference t-select__popup-reference\\"><div class=\\"t-select\\" style=\\"user-select:none\\"><span class=\\"t-select__left-icon\\"></span><span class=\\"t-select__placeholder\\">请选择云解决方案</span><svg class=\\"t-fake-arrow t-select__right-icon\\" width=\\"16\\" height=\\"16\\" viewBox=\\"0 0 16 16\\" fill=\\"none\\" xmlns=\\"http://www.w3.org/2000/svg\\"><path d=\\"M3.75 5.7998L7.99274 10.0425L12.2361 5.79921\\" stroke=\\"black\\" stroke-opacity=\\"0.9\\" stroke-width=\\"1.3\\"></path></svg></div></div></div><div class=\\"t-select__wrap\\" style=\\"width:300px\\"><div class=\\"t-popup__reference t-select__popup-reference\\"><div class=\\"t-select\\" style=\\"user-select:none\\"><span class=\\"t-select__left-icon\\"></span><span class=\\"t-select__placeholder\\">请选择云产品</span><svg class=\\"t-fake-arrow t-select__right-icon\\" width=\\"16\\" height=\\"16\\" viewBox=\\"0 0 16 16\\" fill=\\"none\\" xmlns=\\"http://www.w3.org/2000/svg\\"><path d=\\"M3.75 5.7998L7.99274 10.0425L12.2361 5.79921\\" stroke=\\"black\\" stroke-opacity=\\"0.9\\" stroke-width=\\"1.3\\"></path></svg></div></div></div></div>"`;

exports[`ssr snapshot test renders ./src/select/_example/popup-props.jsx correctly 1`] = `"<div style=\\"display:flex\\" data-reactroot=\\"\\"><div class=\\"t-select__wrap\\" style=\\"width:40%\\"><div class=\\"t-popup__reference t-select__popup-reference\\"><div class=\\"t-select\\" style=\\"user-select:none\\"><span class=\\"t-select__left-icon\\"></span><span class=\\"\\">-请选择-</span><svg class=\\"t-fake-arrow t-select__right-icon\\" width=\\"16\\" height=\\"16\\" viewBox=\\"0 0 16 16\\" fill=\\"none\\" xmlns=\\"http://www.w3.org/2000/svg\\"><path d=\\"M3.75 5.7998L7.99274 10.0425L12.2361 5.79921\\" stroke=\\"black\\" stroke-opacity=\\"0.9\\" stroke-width=\\"1.3\\"></path></svg></div></div></div></div>"`;

exports[`ssr snapshot test renders ./src/select/_example/prefix.jsx correctly 1`] = `"<div class=\\"t-select__wrap\\" style=\\"width:40%\\"><div class=\\"t-popup__reference t-select__popup-reference\\"><div class=\\"t-select t-has-prefix\\" style=\\"user-select:none\\"><span class=\\"t-select__left-icon\\"><svg fill=\\"none\\" viewBox=\\"0 0 16 16\\" width=\\"1em\\" height=\\"1em\\" class=\\"t-icon t-icon-browse\\"><path fill=\\"currentColor\\" d=\\"M10.88 8a2.88 2.88 0 11-5.76 0 2.88 2.88 0 015.76 0zm-1 0a1.88 1.88 0 10-3.76 0 1.88 1.88 0 003.76 0z\\" fill-opacity=\\"0.9\\"></path><path fill=\\"currentColor\\" d=\\"M1.12 8.23A7.72 7.72 0 008 12.5c2.9 0 5.54-1.63 6.88-4.27L15 8l-.12-.23A7.73 7.73 0 008 3.5a7.74 7.74 0 00-6.88 4.27L1 8l.12.23zM8 11.5A6.73 6.73 0 012.11 8 6.73 6.73 0 0113.9 8 6.74 6.74 0 018 11.5z\\" fill-opacity=\\"0.9\\"></path></svg></span><span class=\\"\\">-请选择-</span><svg class=\\"t-fake-arrow t-select__right-icon\\" width=\\"16\\" height=\\"16\\" viewBox=\\"0 0 16 16\\" fill=\\"none\\" xmlns=\\"http://www.w3.org/2000/svg\\"><path d=\\"M3.75 5.7998L7.99274 10.0425L12.2361 5.79921\\" stroke=\\"black\\" stroke-opacity=\\"0.9\\" stroke-width=\\"1.3\\"></path></svg></div></div></div>"`;
Expand Down

0 comments on commit c126604

Please sign in to comment.