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

feat(cascader): support scroll to first selected node #3335

Merged
merged 2 commits into from
Sep 24, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 22 additions & 3 deletions src/cascader/cascader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ import FakeArrow from '../common-components/fake-arrow';
import props from './props';

import { useCascaderContext } from './hooks';
import {
CascaderValue, TdSelectInputProps, TdCascaderProps,
} from './interface';
import { CascaderValue, TdSelectInputProps, TdCascaderProps } from './interface';
import { useConfig, usePrefixClass, useCommonClassName } from '../hooks/useConfig';
import { PopupVisibleChangeContext } from '../popup';
import { closeIconClickEffect, handleRemoveTagEffect } from './core/effect';
Expand Down Expand Up @@ -69,6 +67,25 @@ export default defineComponent({
};
});

const updateScrollTop = (content: HTMLDivElement) => {
const cascaderMenuList = content.querySelectorAll(`.${COMPONENT_NAME.value}__menu`);
cascaderMenuList.forEach((menu: HTMLDivElement) => {
const firstSelectedNode: HTMLDivElement = menu?.querySelector(`.${classPrefix.value}-is-selected`);
if (!firstSelectedNode || !menu) return;

const { paddingBottom } = getComputedStyle(firstSelectedNode);
const { marginBottom } = getComputedStyle(menu);
const elementBottomHeight = parseInt(paddingBottom, 10) + parseInt(marginBottom, 10);

const updateValue = firstSelectedNode.offsetTop
- menu.offsetTop
- (menu.clientHeight - firstSelectedNode.clientHeight)
+ elementBottomHeight;
// eslint-disable-next-line no-param-reassign
menu.scrollTop = updateValue;
});
};

return {
COMPONENT_NAME,
overlayClassName,
Expand All @@ -83,6 +100,7 @@ export default defineComponent({
cascaderContext,
emit,
valueDisplayParams,
updateScrollTop,
};
},
render() {
Expand Down Expand Up @@ -148,6 +166,7 @@ export default defineComponent({
loading: this.loading,
status: this.status,
tips: this.tips,
updateScrollTop: this.updateScrollTop,
suffixIcon: () => renderSuffixIcon(),
popupProps: {
...(this.popupProps as TdCascaderProps['popupProps']),
Expand Down
2 changes: 1 addition & 1 deletion src/popup/popup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ export default mixins(classPrefixMixins).extend({

// popup弹出第一次初始化暴露事件
popupMounted() {
// 用于select定位事件
// 用于下拉组件定位事件
const overlayEl = this.$refs?.overlay as HTMLElement;
if (overlayEl) {
this.updateScrollTop?.(overlayEl);
Expand Down
Loading