Skip to content

Commit

Permalink
feat(pro:search): bluring segment with no panel triggers update now (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
sallerli1 committed Jul 26, 2023
1 parent ffc85a1 commit 294884e
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 8 deletions.
2 changes: 1 addition & 1 deletion packages/pro/search/src/components/SearchItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export default defineComponent({
segmentsRef.value?.scrollTo(0, 0)
})

const segmentStateContext = useSegmentStates(props, proSearchProps, context, isActive)
const segmentStateContext = useSegmentStates(props, proSearchProps, context)
const segmentOverlayUpdateContext = useSegmentOverlayUpdate()
const { searchState, segmentStates } = segmentStateContext

Expand Down
26 changes: 25 additions & 1 deletion packages/pro/search/src/composables/useSearchStates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export function useSearchStates(
const { searchValues, setSearchValues } = searchValueContext
const getKey = createStateKeyGetter()
const { isMarked, getMarks, mark, unmark, clearMarks } = useSearchStateMarks()
let queuedUpdateCnt = 0

const searchStates = ref<SearchState[]>([])
const searchStateKeyMap = computed(() => {
Expand Down Expand Up @@ -294,7 +295,7 @@ export function useSearchStates(
return newSearchState
}

function updateSearchValues() {
function _updateSearchValues() {
const newSearchValues = searchStates.value
.map(state => {
// filters invalid searchValues
Expand All @@ -318,6 +319,29 @@ export function useSearchStates(
callEmit(props.onChange, toRaw(newSearchValues), toRaw(oldeSearchValue))
}
}
function updateSearchValues() {
queuedUpdateCnt++

if (queuedUpdateCnt > 1) {
return
}

const exec = () => {
_updateSearchValues()

// queue update operation until next macro task loop
// to prevent repeated updates
setTimeout(() => {
queuedUpdateCnt--

if (queuedUpdateCnt > 0) {
exec()
}
})
}

exec()
}

const updateSegmentValue = (key: VKey, name: string, value: unknown) => {
if (props.disabled) {
Expand Down
35 changes: 29 additions & 6 deletions packages/pro/search/src/composables/useSegmentStates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ export function useSegmentStates(
props: SearchItemProps,
proSearchProps: ProSearchProps,
proSearchContext: ProSearchContext,
isActive: ComputedRef<boolean>,
): SegmentStatesContext {
const {
initSearchState,
Expand Down Expand Up @@ -173,11 +172,35 @@ export function useSegmentStates(
}
}

watch([isActive, () => props.searchItem?.resolvedSearchField], ([active, searchField]) => {
if (!active || !searchField) {
initSearchState(props.searchItem!.key)
}
})
watch(
[activeSegment, () => props.searchItem?.resolvedSearchField],
([activeSegment, searchField], [preActiveSegment]) => {
if (activeSegment?.itemKey === props.searchItem?.key) {
return
}

if (!searchField) {
initSearchState(props.searchItem!.key)
return
}

const preSegment = props.searchItem?.resolvedSearchField.segments.find(seg => seg.name === preActiveSegment?.name)

// if current segment has no panel and the whole search item is valid after input
// then update search values
//
// we do this to improve user experience
// because always pressing `Enter` after input to trigger update could be annoying
if (preSegment && !preSegment.panelRenderer && validateSearchState(props.searchItem!.key)) {
updateSearchValues()
} else {
initSearchState(props.searchItem!.key)
}
},
{
flush: 'post',
},
)

return {
searchState,
Expand Down

0 comments on commit 294884e

Please sign in to comment.