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 5 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): 新增带搜索情况下,点击叉号按钮时下拉框同时清空
14 changes: 12 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 @@ -304,6 +310,10 @@ export interface PickerProps extends HiBaseHTMLFieldProps<'div'> {
* 开启内容区域可滚动
*/
scrollable?: boolean
/**
* 提供辅助方法的内部引用
*/
innerRef?: React.Ref<HTMLDivElement>
Copy link
Collaborator

Choose a reason for hiding this comment

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

HTMLDivElement 改为 useImperativeHandle 中声明的方法,例如:interface PikcerHelper {resetSearch: () => void}

}

if (__DEV__) {
Expand Down
16 changes: 14 additions & 2 deletions packages/ui/select/src/Select.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import { uniqBy } from '@hi-ui/array-utils'
import { HiBaseAppearanceEnum, HiBaseSizeEnum, useLocaleContext } from '@hi-ui/core'
import { callAllFuncs } from '@hi-ui/func-utils'
import { mockDefaultHandlers } from '@hi-ui/dom-utils'
import { mergeRefs } from '@hi-ui/react-utils'

const _role = 'select'
const _prefix = getPrefixCls(_role)
Expand Down Expand Up @@ -77,13 +78,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<HTMLDivElement>()
Copy link
Collaborator

Choose a reason for hiding this comment

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

HTMLDivElement 改为上面说的 PikcerHelper

const placeholder = isUndef(placeholderProp) ? i18n.get('select.placeholder') : placeholderProp

const [menuVisible, menuVisibleAction] = useUncontrolledToggle({
Expand Down Expand Up @@ -227,7 +229,7 @@ export const Select = forwardRef<HTMLDivElement | null, SelectProps>(
return (
<SelectProvider value={context}>
<Picker
ref={ref}
ref={mergeRefs(ref, innerRef)}
Copy link
Collaborator

@zyprepare zyprepare Jul 30, 2024

Choose a reason for hiding this comment

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

建议将 innerRef 放在该行下面,另外有了 innerRef 后不需要在使用 mergeRefs

className={cls}
{...rootProps}
visible={menuVisible}
Expand All @@ -241,6 +243,7 @@ export const Select = forwardRef<HTMLDivElement | null, SelectProps>(
loading={rest.loading !== undefined ? rest.loading : loading}
footer={renderExtraFooter ? renderExtraFooter() : null}
scrollable={!inVirtual}
innerRef={innerRef}
trigger={
customRender ? (
typeof customRender === 'function' ? (
Expand All @@ -265,6 +268,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 +385,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