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): remote search not working #1727

Merged
merged 1 commit into from
Oct 30, 2023
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
22 changes: 16 additions & 6 deletions packages/pro/search/src/composables/useSearchStates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import type { ProSearchProps, ResolvedSearchField, SearchValue, Segment, Segment

import { type ComputedRef, type Ref, computed, ref, toRaw } from 'vue'

import { isEqual, isNil } from 'lodash-es'
import { isEqual, isNil, isString } from 'lodash-es'

import { type VKey, callEmit, convertArray } from '@idux/cdk/utils'

Expand All @@ -23,10 +23,17 @@ export interface SearchState {
segmentStates: SegmentState[]
}

interface InitSearchState {
(key: VKey): void
(key: VKey, force: boolean): void
(key: VKey, segmentName: string): void
(key: VKey, segmentName: string, force: boolean): void
}

export interface SearchStateContext {
searchStates: Ref<SearchState[]>
initSearchStates: () => void
initSearchState: (key: VKey, segmentName?: string) => void
initSearchState: InitSearchState
createSearchState: (fieldKey: VKey, searchValue?: Omit<SearchValue, 'key'>) => SearchState | undefined
getSearchStateByKey: (key: VKey) => SearchState | undefined
getSearchStatesByFieldKey: (fieldKey: VKey) => SearchState[]
Expand Down Expand Up @@ -260,20 +267,23 @@ export function useSearchStates(
searchStates.value = [...newSearchStates, ...createdStates]
}

const initSearchState = (key: VKey, segmentName?: string) => {
const initSearchState: InitSearchState = (key: VKey, segmentNameOrForce?: string | boolean, force?: boolean) => {
const _force = isString(segmentNameOrForce) ? force : segmentNameOrForce
const _segmentName = isString(segmentNameOrForce) ? segmentNameOrForce : undefined

const searchState = getSearchStateByKey(key)
const searchField = findSearchField(searchState?.fieldKey)
if (!searchState || !searchField) {
if (!searchState || !searchField || (isMarked(key) && !_force)) {
return
}

const searchValue = !isNil(searchState.index) ? searchValues.value?.[searchState.index] : undefined
const segmentStates = generateSegmentStates(searchField, searchValue)

if (!segmentName) {
if (!_segmentName) {
searchState.segmentStates = segmentStates
} else {
const idx = searchState.segmentStates.findIndex(state => state.name === segmentName)
const idx = searchState.segmentStates.findIndex(state => state.name === _segmentName)
searchState.segmentStates[idx] = segmentStates[idx]
}
}
Expand Down
6 changes: 3 additions & 3 deletions packages/pro/search/src/composables/useSegmentStates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ export function useSegmentStates(
}
const handleSegmentCancel = (name: string) => {
// init the canceled segment state
initSearchState(props.searchItem!.key, name)
initSearchState(props.searchItem!.key, name, true)

const segmentState = segmentStates.value[name]

Expand All @@ -189,7 +189,7 @@ export function useSegmentStates(
const searchField = props.searchItem?.resolvedSearchField

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

Expand All @@ -203,7 +203,7 @@ export function useSegmentStates(
if (preSegment && !preSegment.panelRenderer && validateSearchState(props.searchItem!.key)) {
updateSearchValues()
} else {
initSearchState(props.searchItem!.key)
initSearchState(props.searchItem!.key, true)
}
},
{
Expand Down