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

fix(pro:search): quickselect search input shouldn't hide when option selected #1883

Merged
merged 1 commit into from
Apr 8, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
27 changes: 19 additions & 8 deletions packages/pro/search/src/components/quickSelect/QuickSelectItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,20 +34,18 @@ export default defineComponent({
updateSearchValues,
getCacheData,
setCacheData,
tempSegmentInputRef,
} = inject(proSearchContext)!

const searchInputRef = ref<HTMLInputElement>()
const searchIconRef = ref<IconInstance>()

const [searchInput, setSearchInput] = useState('')
const [searchInputOpend, _setSearchInputOpened] = useState(false)
const setSearchInputOpened = (opened: boolean) => {
_setSearchInputOpened(opened)
if (opened) {
searchInputRef.value?.focus()
props.setSearchInputActive?.(true)
} else {
tempSegmentInputRef.value?.focus()
props.setSearchInputActive?.(false)
}
}

Expand All @@ -67,7 +65,7 @@ export default defineComponent({
const prefixCls = `${mergedPrefixCls.value}-quick-select-item-search-bar`
return normalizeClass({
[prefixCls]: true,
[`${prefixCls}-opened`]: searchInputOpend.value,
[`${prefixCls}-opened`]: props.searchInputActive,
})
})

Expand All @@ -81,6 +79,14 @@ export default defineComponent({

const [itemValue, setItemValue] = useState<unknown>(searchDataSegmentState.value?.value)
watch(() => searchDataSegmentState.value?.value, setItemValue)
watch(
() => props.searchInputActive,
active => {
if (!active) {
setSearchInput('')
}
},
)

const confirmValue = (value: unknown) => {
let searchStateKey = searchState.value?.key
Expand Down Expand Up @@ -114,16 +120,21 @@ export default defineComponent({
const setOnKeyDown = () => {}

const handleBlur = () => {
setSearchInput('')
setSearchInputOpened(false)
}
const handleSearchIconClick = () => {
setSearchInputOpened(!searchInputOpend.value)
setSearchInputOpened(!props.searchInputActive)
}
const handleSearchIconMouseDown = (evt: MouseEvent) => {
evt.preventDefault()
evt.stopImmediatePropagation()
}
const handleContentMouseDown = (evt: MouseEvent) => {
if (props.searchInputActive) {
evt.preventDefault()
evt.stopImmediatePropagation()
}
}

return () => {
const prefixCls = `${mergedPrefixCls.value}-quick-select-item`
Expand Down Expand Up @@ -167,7 +178,7 @@ export default defineComponent({
</div>
)}
</div>
<div class={`${prefixCls}-content`}>
<div class={`${prefixCls}-content`} onMousedown={handleContentMouseDown}>
{searchDataSegment.value?.panelRenderer?.({
slots,
input: searchInput.value,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@

import type { ResolvedSearchField } from '../../types'

import { type VNodeChild, computed, defineComponent, inject } from 'vue'
import { type VNodeChild, computed, defineComponent, inject, watch } from 'vue'

import { isEmptyNode } from '@idux/cdk/utils'
import { isNil } from 'lodash-es'

import { type VKey, isEmptyNode, useState } from '@idux/cdk/utils'

import QuickSelectPanelItem from './QuickSelectItem'
import QuickSelectPanelShortcut from './QuickSelectShortcut'
Expand Down Expand Up @@ -37,6 +39,27 @@ export default defineComponent({
}
})

const [inputActiveFieldKey, setInputActiveFieldKey] = useState<VKey | undefined>(undefined)
const setInputActive = (key: VKey, active: boolean) => {
if (active) {
setInputActiveFieldKey(key)
} else if (inputActiveFieldKey.value === key) {
setInputActiveFieldKey(undefined)
tempSegmentInputRef.value?.focus()
}
}

watch(separatedFields, fields => {
const { quickSelectFields } = fields

if (
!isNil(inputActiveFieldKey.value) &&
quickSelectFields.findIndex(field => field.key === inputActiveFieldKey.value) < 0
) {
setInputActiveFieldKey(undefined)
}
})

const handleMouseDown = (evt: MouseEvent) => {
if (!(evt.target instanceof HTMLInputElement)) {
evt.preventDefault()
Expand Down Expand Up @@ -71,7 +94,16 @@ export default defineComponent({
{separatedFields.value.quickSelectFields.length && (
<div class={`${prefixCls}-items`}>
{separatedFields.value.quickSelectFields.map((field, idx) => {
const nodes = [<QuickSelectPanelItem searchField={field} v-slots={slots} />]
const nodes = [
<QuickSelectPanelItem
searchField={field}
searchInputActive={inputActiveFieldKey.value === field.key}
setSearchInputActive={(active: boolean) => {
setInputActive(field.key, active)
}}
v-slots={slots}
/>,
]

if (idx < separatedFields.value.quickSelectFields.length - 1) {
nodes.push(<div class={`${prefixCls}-item-separator`}></div>)
Expand Down
2 changes: 2 additions & 0 deletions packages/pro/search/src/types/quickSelectPanel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import type { DefineComponent, HTMLAttributes, PropType } from 'vue'

export const quickSelectPanelItemProps = {
searchField: { type: Object as PropType<ResolvedSearchField>, required: true },
searchInputActive: Boolean,
setSearchInputActive: Function as PropType<(active: boolean) => void>,
} as const

export const quickSelectPanelShortcutProps = {
Expand Down