Skip to content

Commit

Permalink
feat(select): Add searchOnInit api (#3004)
Browse files Browse the repository at this point in the history
  • Loading branch information
zyprepare committed Sep 19, 2024
1 parent 0b98947 commit 66942ad
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 2 deletions.
6 changes: 6 additions & 0 deletions .changeset/clever-monkeys-speak.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@hi-ui/hiui": patch
---

feat(select): Add searchOnInit api
feat(check-select): Add searchOnInit api
7 changes: 7 additions & 0 deletions .changeset/rude-ways-look.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@hi-ui/use-search-mode": minor
"@hi-ui/check-select": minor
"@hi-ui/select": minor
---

feat: Add searchOnInit api
9 changes: 7 additions & 2 deletions packages/hooks/use-search-mode/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export const useSearchMode = ({
searchable: searchableProp,
strategies,
keyword: keywordProp,
searchOnInit,
}: UseSearchModeProps) => {
const [keyword, setKeyword] = useUncontrolledState('', keywordProp)

Expand All @@ -43,11 +44,11 @@ export const useSearchMode = ({
const runSearch = useCallback(
(keyword: string) => {
if (!searchable) return
if (keyword && runSearchStrategy) {
if ((keyword || searchOnInit) && runSearchStrategy) {
runSearchStrategy(keyword, setStateInSearch)
}
},
[searchable, runSearchStrategy]
[searchable, searchOnInit, runSearchStrategy]
)

const onSearch = useCallback(
Expand Down Expand Up @@ -108,6 +109,10 @@ export interface UseSearchModeProps<T = any> {
* 异步加载数据
*/
dataSource?: UseDataSource<T>
/**
* 初始化时执行一次搜索,仅在 dataSource 不为空时有效
*/
searchOnInit?: boolean
// DataSourceFun | TreeSelectDataSource | Promise<TreeSelectDataItem[]>
strategies: any[]
}
Expand Down
6 changes: 6 additions & 0 deletions packages/ui/check-select/src/CheckSelect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ export const CheckSelect = forwardRef<HTMLDivElement | null, CheckSelectProps>(
invalid,
// search
dataSource,
searchOnInit,
filterOption,
searchable: searchableProp,
render: titleRender,
Expand Down Expand Up @@ -144,6 +145,7 @@ export const CheckSelect = forwardRef<HTMLDivElement | null, CheckSelectProps>(
searchable: searchableProp,
keyword: keywordProp,
strategies: [dataSourceStrategy, customSearchStrategy, filterSearchStrategy],
searchOnInit,
})

// 拦截 titleRender,自定义高亮展示
Expand Down Expand Up @@ -461,6 +463,10 @@ export interface CheckSelectProps
* 异步加载数据
*/
dataSource?: UseDataSource<CheckSelectMergedItem[]>
/**
* 初始化时执行一次搜索,仅在 dataSource 不为空时有效
*/
searchOnInit?: boolean
/**
* 自定义下拉菜单底部渲染
*/
Expand Down
1 change: 1 addition & 0 deletions packages/ui/check-select/stories/data-source.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export const DataSource = () => {
// searchPlaceholder="请输入搜索内容"
data={data}
onChange={console.log}
// searchOnInit
dataSource={(keyword) => {
console.log('DataSource', keyword)
const url =
Expand Down
6 changes: 6 additions & 0 deletions packages/ui/select/src/Select.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ export const Select = forwardRef<HTMLDivElement | null, SelectProps>(
searchable: searchableProp,
keyword: keywordProp,
dataSource,
searchOnInit,
filterOption,
// popper
visible,
Expand Down Expand Up @@ -132,6 +133,7 @@ export const Select = forwardRef<HTMLDivElement | null, SelectProps>(
searchable: searchableProp,
keyword: keywordProp,
strategies: [dataSourceStrategy, customSearchStrategy, filterSearchStrategy],
searchOnInit,
})

// 拦截 titleRender,自定义高亮展示
Expand Down Expand Up @@ -380,6 +382,10 @@ export interface SelectProps
* 异步加载数据
*/
dataSource?: UseDataSource<SelectMergedItem[]>
/**
* 初始化时执行一次搜索,仅在 dataSource 不为空时有效
*/
searchOnInit?: boolean
/**
* 搜索时触发
*/
Expand Down
1 change: 1 addition & 0 deletions packages/ui/select/stories/data-source.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export const DataSource = () => {
// searchPlaceholder="请输入搜索内容"
data={data}
onChange={console.log}
// searchOnInit
dataSource={(keyword) => {
console.log('DataSource', keyword)
const url =
Expand Down

0 comments on commit 66942ad

Please sign in to comment.