fix(select): doesn't scroll to selected item when menu is opened (#348)

This commit is contained in:
07akioni 2021-07-02 01:35:10 +08:00 committed by GitHub
parent 8564f95031
commit 30d0299e0d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 29 additions and 22 deletions

View File

@ -22,6 +22,7 @@
- Fix `n-log` warn on highlight.js when no language is set, closes [#327](https://github.com/TuSimple/naive-ui/issues/327). - Fix `n-log` warn on highlight.js when no language is set, closes [#327](https://github.com/TuSimple/naive-ui/issues/327).
- Remove `n-calendar`'s useless `console.log`. - Remove `n-calendar`'s useless `console.log`.
- Fix loading-bar disappears unexpectl, closes [#343](https://github.com/TuSimple/naive-ui/issues/343). - Fix loading-bar disappears unexpectl, closes [#343](https://github.com/TuSimple/naive-ui/issues/343).
- Fix `n-select` doesn't scroll to selected item when menu is opened, closes [#346](https://github.com/TuSimple/naive-ui/issues/346).
## 2.15.1 (2021-06-30) ## 2.15.1 (2021-06-30)

View File

@ -22,6 +22,7 @@
- 修复 `n-log` 在未设定语言时仍然对缺少 highlight.js 报错,关闭 [#327](https://github.com/TuSimple/naive-ui/issues/327) - 修复 `n-log` 在未设定语言时仍然对缺少 highlight.js 报错,关闭 [#327](https://github.com/TuSimple/naive-ui/issues/327)
- 移除 `n-calendar` 无用的 console.log - 移除 `n-calendar` 无用的 console.log
- 修复 loading-bar 自动消失,关闭 [#343](https://github.com/TuSimple/naive-ui/issues/343) - 修复 loading-bar 自动消失,关闭 [#343](https://github.com/TuSimple/naive-ui/issues/343)
- 修复 `n-select` 打开菜单时没有自动滚动到选中项,关闭 [#346](https://github.com/TuSimple/naive-ui/issues/346)
## 2.15.1 (2021-06-30) ## 2.15.1 (2021-06-30)

View File

@ -131,7 +131,7 @@
"vdirs": "^0.1.4", "vdirs": "^0.1.4",
"vfonts": "^0.1.0", "vfonts": "^0.1.0",
"vooks": "^0.2.6", "vooks": "^0.2.6",
"vueuc": "^0.4.7" "vueuc": "^0.4.9"
}, },
"sideEffects": false, "sideEffects": false,
"homepage": "https://www.naiveui.com", "homepage": "https://www.naiveui.com",

View File

@ -8,7 +8,9 @@ import {
watch, watch,
toRef, toRef,
renderSlot, renderSlot,
provide provide,
nextTick,
watchEffect
} from 'vue' } from 'vue'
import { TreeNode, createIndexGetter } from 'treemate' import { TreeNode, createIndexGetter } from 'treemate'
import { VirtualList, VirtualListInst } from 'vueuc' import { VirtualList, VirtualListInst } from 'vueuc'
@ -131,13 +133,15 @@ export default defineComponent({
: null : null
) )
} }
watch(toRef(props, 'show'), (value) => {
if (value) initPendingNode()
})
initPendingNode() initPendingNode()
const defaultScrollIndex = pendingNodeRef.value onMounted(() => {
? fIndexGetterRef.value(pendingNodeRef.value.key) ?? undefined watchEffect(() => {
: undefined if (props.show) {
initPendingNode()
void nextTick(scrollToPendingNode)
}
})
})
const itemSizeRef = computed(() => { const itemSizeRef = computed(() => {
return depx(themeRef.value.self[createKey('optionHeight', props.size)]) return depx(themeRef.value.self[createKey('optionHeight', props.size)])
}) })
@ -231,17 +235,20 @@ export default defineComponent({
doScroll = false doScroll = false
): void { ): void {
pendingNodeRef.value = tmNode pendingNodeRef.value = tmNode
if (doScroll && tmNode) { if (doScroll) scrollToPendingNode()
const fIndex = fIndexGetterRef.value(tmNode.key) }
if (fIndex === null) return function scrollToPendingNode (): void {
if (props.virtualScroll) { const tmNode = pendingNodeRef.value
virtualListRef.value?.scrollTo({ index: fIndex }) if (!tmNode) return
} else { const fIndex = fIndexGetterRef.value(tmNode.key)
scrollbarRef.value?.scrollTo({ if (fIndex === null) return
index: fIndex, if (props.virtualScroll) {
elSize: itemSizeRef.value virtualListRef.value?.scrollTo({ index: fIndex })
}) } else {
} scrollbarRef.value?.scrollTo({
index: fIndex,
elSize: itemSizeRef.value
})
} }
} }
function handleFocusin (e: FocusEvent): void { function handleFocusin (e: FocusEvent): void {
@ -327,7 +334,6 @@ export default defineComponent({
virtualListRef, virtualListRef,
scrollbarRef, scrollbarRef,
style: styleRef, style: styleRef,
defaultScrollIndex,
itemSize: itemSizeRef, itemSize: itemSizeRef,
padding: paddingRef, padding: paddingRef,
flattenedNodes: flattenedNodesRef, flattenedNodes: flattenedNodesRef,
@ -391,7 +397,6 @@ export default defineComponent({
items={this.flattenedNodes} items={this.flattenedNodes}
itemSize={this.itemSize} itemSize={this.itemSize}
showScrollbar={false} showScrollbar={false}
defaultScrollIndex={this.defaultScrollIndex}
paddingTop={this.padding.top} paddingTop={this.padding.top}
paddingBottom={this.padding.bottom} paddingBottom={this.padding.bottom}
onResize={this.handleVirtualListResize} onResize={this.handleVirtualListResize}

View File

@ -14,7 +14,7 @@ export default {
data () { data () {
return { return {
value: 'song0', value: 'song0',
value1: ['song0', 'song1'], value1: ['song0', 'song1', 'song11'],
options: [ options: [
{ {
label: "Everybody's Got Something to Hide Except Me and My Monkey", label: "Everybody's Got Something to Hide Except Me and My Monkey",