Skip to content

Commit

Permalink
feat: select 打开时自动滚动至选中的项目
Browse files Browse the repository at this point in the history
  • Loading branch information
TerryZ committed Sep 25, 2024
1 parent 0ba4320 commit 718f2f3
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
14 changes: 14 additions & 0 deletions src/core/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,3 +120,17 @@ export function inputFocus (input) {
if (!input) return
input.focus({ preventScroll: true })
}
export function scrollIntoElement (container, active) {
const activeEl = typeof active === 'string'
? container.querySelector(active)
: active
if (
container.scrollHeight <= container.offsetHeight || !activeEl
) return

activeEl?.scrollIntoView({
behavior: 'smooth',
block: 'nearest',
inline: 'start'
})
}
15 changes: 13 additions & 2 deletions src/modules/select/SelectLevel.jsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { inject, computed, defineComponent } from 'vue'
import { inject, computed, defineComponent, ref, nextTick } from 'vue'

import DropdownContainer from '../../components/DropdownContainer'
import SelectLevelList from './SelectLevelList'
import SelectDropdownTrigger from './SelectDropdownTrigger'

import { injectKeyCore, injectKeyBase } from '../../constants'
import { scrollIntoElement } from '../../core/helper'

export default defineComponent({
name: 'RegionSelect',
Expand All @@ -18,23 +19,33 @@ export default defineComponent({
customTriggerClass,
customContainerClass
} = inject(injectKeyBase)
const list = ref()

const blankContent = blank ? lang.pleaseSelect : '&nbsp;'
const contentText = computed(() => (
data.value[props.level]?.name || blankContent
))

const handleVisibleChange = val => {
if (!val) return

nextTick(() => {
scrollIntoElement(list.value.$el, '.selected')
})
}

return () => (
<DropdownContainer
class='rg-select'
disabled={disabled.value}
custom-trigger-class={customTriggerClass}
custom-container-class={customContainerClass}
onVisibleChange={handleVisibleChange}
>{{
trigger: () => (
<SelectDropdownTrigger>{contentText.value}</SelectDropdownTrigger>
),
default: () => <SelectLevelList level={props.level} />
default: () => <SelectLevelList ref={list} level={props.level} />
}}</DropdownContainer>
)
}
Expand Down

0 comments on commit 718f2f3

Please sign in to comment.