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

feat(select): 新增带搜索情况下,点击叉号按钮时下拉框同时清空(#2945) #2948

Merged
merged 6 commits into from
Aug 1, 2024
Merged
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
5 changes: 5 additions & 0 deletions .changeset/healthy-needles-cheer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@hi-ui/select": minor
---

feat: 新增带搜索情况下,点击叉号按钮时下拉框同时清空
5 changes: 5 additions & 0 deletions .changeset/ten-countries-kick.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@hi-ui/hiui": patch
---

feat(select): 新增带搜索情况下,点击叉号按钮时下拉框同时清空
18 changes: 16 additions & 2 deletions packages/ui/picker/src/Picker.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { forwardRef, useCallback, useState } from 'react'
import React, { forwardRef, useCallback, useImperativeHandle, useState } from 'react'
import { cx, getPrefixCls } from '@hi-ui/classname'
import { __DEV__ } from '@hi-ui/env'
import { HiBaseHTMLFieldProps, useLocaleContext } from '@hi-ui/core'
Expand Down Expand Up @@ -48,6 +48,7 @@ export const Picker = forwardRef<HTMLDivElement | null, PickerProps>(
trigger,
footer,
onOverlayScroll,
innerRef,
...rest
},
ref
Expand All @@ -72,7 +73,6 @@ export const Picker = forwardRef<HTMLDivElement | null, PickerProps>(
onSearch,
Object.is
)

// const inSearch = searchable && !!searchValue
// const isEmpty = inSearch && showEmpty
const resetSearchOnClosed = keywordProp === undefined
Expand Down Expand Up @@ -141,6 +141,12 @@ export const Picker = forwardRef<HTMLDivElement | null, PickerProps>(

const cls = cx(prefixCls, className, `${prefixCls}--${menuVisible ? 'open' : 'closed'}`)

useImperativeHandle(innerRef, () => ({
resetSearch: () => {
resetSearchOnClosed && resetSearch()
},
}))

return (
<div
ref={ref}
Expand Down Expand Up @@ -222,6 +228,10 @@ export const Picker = forwardRef<HTMLDivElement | null, PickerProps>(
}
)

export interface PickerHelper {
resetSearch: () => void
}

export interface PickerProps extends HiBaseHTMLFieldProps<'div'> {
/**
* 是否禁用
Expand Down Expand Up @@ -304,6 +314,10 @@ export interface PickerProps extends HiBaseHTMLFieldProps<'div'> {
* 开启内容区域可滚动
*/
scrollable?: boolean
/**
* 提供辅助方法的内部引用
*/
innerRef?: React.Ref<PickerHelper>
}

if (__DEV__) {
Expand Down
15 changes: 13 additions & 2 deletions packages/ui/select/src/Select.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { useLatestCallback } from '@hi-ui/use-latest'
import VirtualList, { useCheckInVirtual } from '@hi-ui/virtual-list'
import type { ListRef } from 'rc-virtual-list'
import { isArrayNonEmpty, isUndef } from '@hi-ui/type-assertion'
import { Picker, PickerProps } from '@hi-ui/picker'
import { Picker, PickerProps, PickerHelper } from '@hi-ui/picker'
import { Highlighter } from '@hi-ui/highlighter'
import { UseDataSource } from '@hi-ui/use-data-source'
import {
Expand Down Expand Up @@ -77,13 +77,14 @@ export const Select = forwardRef<HTMLDivElement | null, SelectProps>(
onSelect: onSelectProp,
onSearch: onSearchProp,
onKeyDown: onKeyDownProp,
onClear: onClearProp,
customRender,
...rest
},
ref
) => {
const i18n = useLocaleContext()

const innerRef = useRef<PickerHelper>()
const placeholder = isUndef(placeholderProp) ? i18n.get('select.placeholder') : placeholderProp

const [menuVisible, menuVisibleAction] = useUncontrolledToggle({
Expand Down Expand Up @@ -228,6 +229,7 @@ export const Select = forwardRef<HTMLDivElement | null, SelectProps>(
<SelectProvider value={context}>
<Picker
ref={ref}
innerRef={innerRef}
className={cls}
{...rootProps}
visible={menuVisible}
Expand Down Expand Up @@ -265,6 +267,11 @@ export const Select = forwardRef<HTMLDivElement | null, SelectProps>(
value={value}
onChange={(value, item) => {
tryChangeValue(value, item.raw)
// 非受控模式下清空下拉框
if (value === '') {
innerRef.current?.resetSearch()
onClearProp?.()
}
}}
size={size}
data={mergedData}
Expand Down Expand Up @@ -377,6 +384,10 @@ export interface SelectProps
* 搜索时触发
*/
onSearch?: (keyword: string) => void
/**
* 点击关闭按钮时触发
*/
onClear?: () => void
/**
* 设置大小
*/
Expand Down
3 changes: 3 additions & 0 deletions packages/ui/select/stories/search-controlled.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ export const SearchControlled = () => {
setKeyword(value)
console.log('onSearch', value)
}}
onClear={() => {
setKeyword('')
}}
placeholder="请选择品类"
searchPlaceholder="请输入搜索内容"
data={data}
Expand Down