mirror of
https://gitee.com/vant-contrib/vant.git
synced 2025-04-06 03:57:59 +08:00
feat(Cascader): scroll the selected option into view when switching tabs (#11869)
This commit is contained in:
parent
ee0a332e8a
commit
0670ebf8c1
@ -17,6 +17,9 @@ import {
|
|||||||
type Numeric,
|
type Numeric,
|
||||||
} from '../utils';
|
} from '../utils';
|
||||||
|
|
||||||
|
// Composables
|
||||||
|
import { useRefs } from '../composables/use-refs';
|
||||||
|
|
||||||
// Components
|
// Components
|
||||||
import { Tab } from '../tab';
|
import { Tab } from '../tab';
|
||||||
import { Tabs } from '../tabs';
|
import { Tabs } from '../tabs';
|
||||||
@ -53,6 +56,8 @@ export default defineComponent({
|
|||||||
setup(props, { slots, emit }) {
|
setup(props, { slots, emit }) {
|
||||||
const tabs = ref<CascaderTab[]>([]);
|
const tabs = ref<CascaderTab[]>([]);
|
||||||
const activeTab = ref(0);
|
const activeTab = ref(0);
|
||||||
|
const [selectedElementRefs, setSelectedElementRefs] =
|
||||||
|
useRefs<HTMLElement>();
|
||||||
|
|
||||||
const {
|
const {
|
||||||
text: textKey,
|
text: textKey,
|
||||||
@ -222,6 +227,7 @@ export default defineComponent({
|
|||||||
|
|
||||||
return (
|
return (
|
||||||
<li
|
<li
|
||||||
|
ref={selected ? setSelectedElementRefs(tabIndex) : undefined}
|
||||||
role="menuitemradio"
|
role="menuitemradio"
|
||||||
class={[bem('option', { selected, disabled }), option.className]}
|
class={[bem('option', { selected, disabled }), option.className]}
|
||||||
style={{ color }}
|
style={{ color }}
|
||||||
@ -283,7 +289,20 @@ export default defineComponent({
|
|||||||
</Tabs>
|
</Tabs>
|
||||||
);
|
);
|
||||||
|
|
||||||
|
const scrollIntoView = (el: HTMLElement) => {
|
||||||
|
const scrollParent = el.parentElement;
|
||||||
|
|
||||||
|
if (scrollParent) {
|
||||||
|
scrollParent.scrollTop =
|
||||||
|
el.offsetTop - (scrollParent.offsetHeight - el.offsetHeight) / 2;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
updateTabs();
|
updateTabs();
|
||||||
|
watch(activeTab, (value) => {
|
||||||
|
const el = selectedElementRefs.value[value];
|
||||||
|
if (el) scrollIntoView(el);
|
||||||
|
});
|
||||||
watch(() => props.options, updateTabs, { deep: true });
|
watch(() => props.options, updateTabs, { deep: true });
|
||||||
watch(
|
watch(
|
||||||
() => props.modelValue,
|
() => props.modelValue,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user