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): segment input delete doesn't work after selecting all #1722

Merged
merged 1 commit into from
Oct 23, 2023
Merged
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
46 changes: 30 additions & 16 deletions packages/pro/search/src/components/segment/Segment.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@
}
const updateSelectionStart = (selectionStart: number) => {
const inputEl = segmentInputRef.value?.getInputElement()
if (inputEl && selectionStart !== inputEl.selectionStart) {

if (inputEl && (selectionStart !== inputEl.selectionStart || selectionStart !== inputEl.selectionEnd)) {
inputEl.setSelectionRange(selectionStart, selectionStart)
}
}
Expand Down Expand Up @@ -135,17 +136,18 @@
handleSegmentCancel(props.segment.name)
}

const { handleInput, handleFocus, handleKeyDown, handleMouseDown, setPanelOnKeyDown } = useInputEvents(
props,
context,
segmentInputRef,
overlayOpened,
handleSegmentInput,
handleSegmentSelect,
setOverlayOpened,
changeActiveAndSelect,
handleConfirm,
)
const { handleInput, handleFocus, handleKeyDown, handleMouseDown, handleSelect, setPanelOnKeyDown } =
useInputEvents(
props,
context,
segmentInputRef,
overlayOpened,
handleSegmentInput,
handleSegmentSelect,
setOverlayOpened,
changeActiveAndSelect,
handleConfirm,
)

const overlayProps = useOverlayAttrs(props, mergedPrefixCls, commonOverlayProps, overlayOpened)

Expand All @@ -161,6 +163,7 @@
onFocus={handleFocus}
onKeydown={handleKeyDown}
onMousedown={handleMouseDown}
onSelect={handleSelect}
onWidthChange={triggerOverlayUpdate}
></SegmentInput>
)
Expand Down Expand Up @@ -223,6 +226,7 @@
handleFocus: (evt: FocusEvent) => void
handleKeyDown: (evt: KeyboardEvent) => void
handleMouseDown: (evt: MouseEvent) => void
handleSelect: () => void
setPanelOnKeyDown: (onKeyDown: ((evt: KeyboardEvent) => boolean) | undefined) => void
}

Expand Down Expand Up @@ -251,7 +255,11 @@
}
function setSelectionStart() {
setTimeout(() => {
handleSegmentSelect(props.segment.name, getInputElement()?.selectionStart)
const inputEl = getInputElement()
handleSegmentSelect(
props.segment.name,
inputEl?.selectionDirection === 'backward' ? inputEl?.selectionStart : inputEl?.selectionEnd,
)
})
}

Expand All @@ -271,63 +279,69 @@
evt.stopImmediatePropagation()
setSelectionStart()
}
const handleSelect = () => {
setSelectionStart()
}

const handleKeyDown = (evt: KeyboardEvent) => {
evt.stopPropagation()
if (overlayOpened.value && panelOnKeyDown.value && !panelOnKeyDown.value(evt)) {
return
}

const inputEl = getInputElement()

switch (evt.key) {
case 'Enter':
evt.preventDefault()
if (props.input || overlayOpened.value || !props.segment.panelRenderer) {
confirm()
} else {
setOverlayOpened(true)
}

break
case 'Backspace':
if (props.selectionStart === 0) {
if ((inputEl?.selectionStart ?? 0) <= 0 && (inputEl?.selectionEnd ?? 0) <= 0) {
evt.preventDefault()

changeActiveAndSelect(-1, 'end')
}
break
case 'Escape':
setOverlayOpened(false)
break
case 'ArrowLeft':
if ((getInputElement()?.selectionStart ?? 0) <= 0) {
if ((inputEl?.selectionStart ?? 0) <= 0) {
evt.preventDefault()
changeActiveAndSelect(-1, 'end')
} else {
setSelectionStart()
setOverlayOpened(true)
}
break
case 'ArrowRight':
if ((getInputElement()?.selectionStart ?? 0) >= (props.input?.length ?? 0)) {
if ((inputEl?.selectionStart ?? 0) >= (props.input?.length ?? 0)) {
evt.preventDefault()
changeActiveAndSelect(1, 'start')
} else {
setSelectionStart()
setOverlayOpened(true)
}
break
default:
setSelectionStart()
setOverlayOpened(true)
break
}
}

return {
handleInput,
handleFocus,
handleKeyDown,
handleMouseDown,
handleSelect,
setPanelOnKeyDown,
}

Check notice on line 346 in packages/pro/search/src/components/segment/Segment.tsx

View check run for this annotation

codefactor.io / CodeFactor

packages/pro/search/src/components/segment/Segment.tsx#L286-L346

Complex Method
}