Skip to content

Commit

Permalink
fix(select): doesn't scroll to selected item when menu is opened (#348)
Browse files Browse the repository at this point in the history
  • Loading branch information
07akioni authored Jul 1, 2021
1 parent 8564f95 commit 30d0299
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 22 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.en-US.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
- Remove `n-calendar`'s useless `console.log`.
- 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)

Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
- 修复 `n-log` 在未设定语言时仍然对缺少 highlight.js 报错,关闭 [#327](https://github.com/TuSimple/naive-ui/issues/327)
- 移除 `n-calendar` 无用的 console.log
- 修复 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)

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@
"vdirs": "^0.1.4",
"vfonts": "^0.1.0",
"vooks": "^0.2.6",
"vueuc": "^0.4.7"
"vueuc": "^0.4.9"
},
"sideEffects": false,
"homepage": "https://www.naiveui.com",
Expand Down
45 changes: 25 additions & 20 deletions src/_internal/select-menu/src/SelectMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ import {
watch,
toRef,
renderSlot,
provide
provide,
nextTick,
watchEffect
} from 'vue'
import { TreeNode, createIndexGetter } from 'treemate'
import { VirtualList, VirtualListInst } from 'vueuc'
Expand Down Expand Up @@ -131,13 +133,15 @@ export default defineComponent({
: null
)
}
watch(toRef(props, 'show'), (value) => {
if (value) initPendingNode()
})
initPendingNode()
const defaultScrollIndex = pendingNodeRef.value
? fIndexGetterRef.value(pendingNodeRef.value.key) ?? undefined
: undefined
onMounted(() => {
watchEffect(() => {
if (props.show) {
initPendingNode()
void nextTick(scrollToPendingNode)
}
})
})
const itemSizeRef = computed(() => {
return depx(themeRef.value.self[createKey('optionHeight', props.size)])
})
Expand Down Expand Up @@ -231,17 +235,20 @@ export default defineComponent({
doScroll = false
): void {
pendingNodeRef.value = tmNode
if (doScroll && tmNode) {
const fIndex = fIndexGetterRef.value(tmNode.key)
if (fIndex === null) return
if (props.virtualScroll) {
virtualListRef.value?.scrollTo({ index: fIndex })
} else {
scrollbarRef.value?.scrollTo({
index: fIndex,
elSize: itemSizeRef.value
})
}
if (doScroll) scrollToPendingNode()
}
function scrollToPendingNode (): void {
const tmNode = pendingNodeRef.value
if (!tmNode) return
const fIndex = fIndexGetterRef.value(tmNode.key)
if (fIndex === null) return
if (props.virtualScroll) {
virtualListRef.value?.scrollTo({ index: fIndex })
} else {
scrollbarRef.value?.scrollTo({
index: fIndex,
elSize: itemSizeRef.value
})
}
}
function handleFocusin (e: FocusEvent): void {
Expand Down Expand Up @@ -327,7 +334,6 @@ export default defineComponent({
virtualListRef,
scrollbarRef,
style: styleRef,
defaultScrollIndex,
itemSize: itemSizeRef,
padding: paddingRef,
flattenedNodes: flattenedNodesRef,
Expand Down Expand Up @@ -391,7 +397,6 @@ export default defineComponent({
items={this.flattenedNodes}
itemSize={this.itemSize}
showScrollbar={false}
defaultScrollIndex={this.defaultScrollIndex}
paddingTop={this.padding.top}
paddingBottom={this.padding.bottom}
onResize={this.handleVirtualListResize}
Expand Down
2 changes: 1 addition & 1 deletion src/select/demos/zhCN/menu-debug.demo.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export default {
data () {
return {
value: 'song0',
value1: ['song0', 'song1'],
value1: ['song0', 'song1', 'song11'],
options: [
{
label: "Everybody's Got Something to Hide Except Me and My Monkey",
Expand Down

0 comments on commit 30d0299

Please # to comment.