Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
18 changes: 9 additions & 9 deletions components/suggestion/index.en-US.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ Common props ref:[Common props](/docs/react/common-props)
| --- | --- | --- | --- | --- |
| block | Take up the full width | boolean | false | - |
| children | Custom input box | ({ onTrigger, onKeyDown }) => ReactElement | - | - |
| items | Suggestion list | SuggestionItem[] \| ((info: T) => SuggestionItem[]) | - | - |
| items | Suggestion list | SuggestionItem\<T\>[] \| ((info: T) => SuggestionItem\<K\>[]) | - | - |
| open | Controlled open panel | boolean | - | - |
| rootClassName | Root element class name | string | - | - |
| onSelect | Callback when the suggestion item is selected | (value: string) => void | - | - |
| onSelect | Callback when the suggestion item is selected | (value: string, item: SuggestionItem\<T\>) => void | - | - |
| onOpenChange | Callback when the panel open state changes | (open: boolean) => void | - | - |

#### onTrigger
Expand All @@ -46,13 +46,13 @@ Suggestion accepts generics to customize the parameter type passed to `items` re

### SuggestionItem

| Property | Description | Type | Default | Version |
| -------- | ------------------------------------- | ---------------- | ------- | ------- |
| children | Child item for the suggestion item | SuggestionItem[] | - | - |
| extra | Extra content for the suggestion item | ReactNode | - | - |
| icon | Icon for the suggestion | ReactNode | - | - |
| label | Content to display for the suggestion | ReactNode | - | - |
| value | Value of the suggestion item | string | - | - |
| Property | Description | Type | Default | Version |
| -------- | ------------------------------------- | --------------------- | ------- | ------- |
| children | Child item for the suggestion item | SuggestionItem\<T\>[] | - | - |
| extra | Extra content for the suggestion item | ReactNode | - | - |
| icon | Icon for the suggestion | ReactNode | - | - |
| label | Content to display for the suggestion | ReactNode | - | - |
| value | Value of the suggestion item | string | - | - |

## Theme Variables (Design Token)

Expand Down
10 changes: 5 additions & 5 deletions components/suggestion/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { useXProviderContext } from '../x-provider';
import useStyle from './style';
import useActive from './useActive';

export type SuggestionItem = {
export type SuggestionItem<T = Record<string, any>> = {
label: React.ReactNode;
value: string;

Expand All @@ -17,7 +17,7 @@ export type SuggestionItem = {
children?: SuggestionItem[];

extra?: React.ReactNode;
};
} & T;

export interface RenderChildrenProps<T> {
onTrigger: (info?: T | false) => void;
Expand All @@ -33,7 +33,7 @@ export interface SuggestionProps<T = any> {
open?: boolean;
onOpenChange?: (open: boolean) => void;
items: SuggestionItem[] | ((info?: T) => SuggestionItem[]);
onSelect?: (value: string) => void;
onSelect?: (value: string, item: SuggestionItem) => void;
block?: boolean;
styles?: Partial<Record<string, React.CSSProperties>>;
classNames?: Partial<Record<string, string>>;
Expand Down Expand Up @@ -107,9 +107,9 @@ function Suggestion<T = any>(props: SuggestionProps<T>) {
);
};

const onInternalChange = (valuePath: string[]) => {
const onInternalChange = (valuePath: string[], selectedOptions: SuggestionItem[]) => {
if (onSelect) {
onSelect(valuePath[valuePath.length - 1]);
onSelect(valuePath[valuePath.length - 1], selectedOptions[valuePath.length - 1]);
}
triggerOpen(false);
};
Expand Down
18 changes: 9 additions & 9 deletions components/suggestion/index.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ coverDark: https://mdn.alipayobjects.com/huamei_iwk9zp/afts/img/A*cahuSJ4VxvoAAA
| --- | --- | --- | --- | --- |
| block | 是否整行宽度 | boolean | false | - |
| children | 自定义输入框 | ({ onTrigger, onKeyDown }) => ReactElement | - | - |
| items | 建议项列表 | SuggestionItem[] \| ((info: T) => SuggestionItem[]) | - | - |
| items | 建议项列表 | SuggestionItem\<T\>[] \| ((info: T) => SuggestionItem\<K\>[]) | - | - |
| open | 受控打开面板 | boolean | - | - |
| rootClassName | 根元素样式类名 | string | - | - |
| onSelect | 选中建议项回调 | (value: string) => void | - | - |
| onSelect | 选中建议项回调 | (value: string, item: SuggestionItem\<T\>) => void | - | - |
| onOpenChange | 面板打开状态变化回调 | (open: boolean) => void | - | - |

#### onTrigger
Expand All @@ -47,13 +47,13 @@ Suggestion 接受泛型以自定义传递给 `items` renderProps 的参数类型

### SuggestionItem

| 属性 | 说明 | 类型 | 默认值 | 版本 |
| -------- | -------------- | ---------------- | ------ | ---- |
| children | 子项目 | SuggestionItem[] | - | - |
| extra | 建议项额外内容 | ReactNode | - | - |
| icon | 建议项图标 | ReactNode | - | - |
| label | 建议项显示内容 | ReactNode | - | - |
| value | 建议项值 | string | - | - |
| 属性 | 说明 | 类型 | 默认值 | 版本 |
| -------- | -------------- | --------------------- | ------ | ---- |
| children | 子项目 | SuggestionItem\<T\>[] | - | - |
| extra | 建议项额外内容 | ReactNode | - | - |
| icon | 建议项图标 | ReactNode | - | - |
| label | 建议项显示内容 | ReactNode | - | - |
| value | 建议项值 | string | - | - |

## 主题变量(Design Token)

Expand Down
7 changes: 3 additions & 4 deletions components/suggestion/useActive.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useEvent } from 'rc-util';
import type { SuggestionItem } from '.';
import React from 'react';
import type { SuggestionItem } from '.';

/**
* Since Cascader not support ref active, we use `value` to mock the active item.
Expand All @@ -9,15 +9,14 @@ export default function useActive(
items: SuggestionItem[],
open: boolean,
rtl: boolean,
onSelect: (value: string[]) => void,
onSelect: (value: string[], options: SuggestionItem[]) => void,
onCancel: () => void,
) {
const [activePaths, setActivePaths] = React.useState<string[]>([]);

/** Get items by column index */
const getItems = (colIndex: number, paths = activePaths) => {
let currentItems = items;

for (let i = 0; i < colIndex - 1; i += 1) {
const activePath = paths[i];
const activeItem = currentItems.find((item) => item.value === activePath);
Expand Down Expand Up @@ -104,7 +103,7 @@ export default function useActive(
case 'Enter':
// Submit if not have children
if (!getItems(activePaths.length + 1).length) {
onSelect(getValues(activePaths));
onSelect(getValues(activePaths), getItems(activePaths.length, activePaths));
}
e.preventDefault();
break;
Expand Down