Skip to content

Commit 18b23af

Browse files
PeikyLiuwzc520pyfm
andauthored
fix(suggestion): fix the dropdown width not adapting when block (#358)
* fix(suggestion): fix the dropdown width not adapting when block prop is true * refactor(suggestion): optimize component and element width hook * refactor: replace useElementWidth with VueUse useElementSize * build(project): add vueuse-core to deps --------- Co-authored-by: wzc520pyfm <1528857653@qq.com>
1 parent 8853c26 commit 18b23af

File tree

3 files changed

+52
-33
lines changed

3 files changed

+52
-33
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,6 @@
108108
"@vitest/coverage-v8": "^2.1.1",
109109
"@vue/shared": "^3.5.13",
110110
"@vue/test-utils": "^2.4.6",
111-
"@vueuse/core": "^12.2.0",
112111
"ant-design-vue": "^4.2.6",
113112
"changelogen": "^0.5.7",
114113
"eslint": "^8.57.1",
@@ -139,6 +138,7 @@
139138
"@ant-design/fast-color": "^2.0.6",
140139
"@emotion/hash": "^0.9.2",
141140
"@emotion/unitless": "^0.10.0",
141+
"@vueuse/core": "^13.4.0",
142142
"classnames": "^2.5.1",
143143
"csstype": "^3.1.3",
144144
"stylis": "^4.3.4"

pnpm-lock.yaml

Lines changed: 35 additions & 30 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/suggestion/Suggestion.vue

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@ import type { RenderChildrenProps, SuggestionItem, SuggestionProps } from './int
44
import { useXProviderContext } from '../x-provider';
55
import useXComponentConfig from '../_util/hooks/use-x-component-config';
66
import useStyle from './style';
7-
import { computed, type VNode } from 'vue';
7+
import { computed, type VNode, ref } from 'vue';
88
import useState from '../_util/hooks/use-state';
99
import { Cascader, type CascaderProps } from 'ant-design-vue';
1010
import useActive from './useActive';
11+
import { useElementSize } from '@vueuse/core';
1112
1213
defineOptions({ name: 'AXSuggestion' });
1314
@@ -38,12 +39,24 @@ const isRTL = computed(() => direction.value === 'rtl');
3839
// ===================== Component Config =========================
3940
const contextConfig = useXComponentConfig('suggestion');
4041
42+
// ============================ Refs =============================
43+
const cascaderSlotRef = ref<HTMLElement>();
44+
4145
// ============================ Styles ============================
4246
const [wrapCSSVar, hashId, cssVarCls] = useStyle(prefixCls);
4347
48+
const {width: slotActiveWidth} = useElementSize(cascaderSlotRef)
49+
50+
const dropdownStyle = computed(() => {
51+
if(!block) return undefined
52+
if (!slotActiveWidth.value) return undefined
53+
return {width: `${slotActiveWidth.value}px`}
54+
})
55+
4456
// =========================== Trigger ============================
4557
const [mergedOpen, setOpen] = useState(open);
4658
59+
4760
const [info, setInfo] = useState<T | undefined>();
4861
4962
const triggerOpen = (nextOpen: boolean) => {
@@ -116,10 +129,11 @@ defineRender(() => {
116129
[`${prefixCls.value}-block`]: block,
117130
})}
118131
onChange={onInternalChange as CascaderProps['onChange']}
119-
dropdownMatchSelectWidth={block}
132+
dropdownStyle={dropdownStyle.value}
120133
>
121134
{{
122135
default: () => <div
136+
ref={cascaderSlotRef}
123137
class={classnames(
124138
prefixCls.value,
125139
contextConfig.value.className,

0 commit comments

Comments
 (0)