Skip to content

Commit 90b5daf

Browse files
fix(InputMenu/Select/SelectMenu): show placeholder when model value is falsy (#4825)
Co-authored-by: Benjamin Canac <[email protected]>
1 parent ec55435 commit 90b5daf

File tree

1 file changed

+38
-1
lines changed

1 file changed

+38
-1
lines changed

src/runtime/utils/index.ts

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,43 @@ export function compare<T>(value?: T, currentValue?: T, comparator?: string | ((
8383
return isEqual(value, currentValue)
8484
}
8585

86+
export function isEmpty(value: unknown): boolean {
87+
if (value == null) {
88+
return true
89+
}
90+
91+
if (typeof value === 'boolean' || typeof value === 'number') {
92+
return false
93+
}
94+
95+
if (typeof value === 'string') {
96+
return value.trim().length === 0
97+
}
98+
99+
if (Array.isArray(value)) {
100+
return value.length === 0
101+
}
102+
103+
if (value instanceof Map || value instanceof Set) {
104+
return value.size === 0
105+
}
106+
107+
if (value instanceof Date || value instanceof RegExp || typeof value === 'function') {
108+
return false
109+
}
110+
111+
if (typeof value === 'object') {
112+
for (const _ in value as object) {
113+
if (Object.prototype.hasOwnProperty.call(value, _)) {
114+
return false
115+
}
116+
}
117+
return true
118+
}
119+
120+
return false
121+
}
122+
86123
export function getDisplayValue<T, V>(
87124
items: T[],
88125
value: V | undefined | null,
@@ -93,7 +130,7 @@ export function getDisplayValue<T, V>(
93130
): string | undefined {
94131
const { valueKey, labelKey } = options
95132

96-
if (value === null || value === undefined) {
133+
if (isEmpty(value)) {
97134
return undefined
98135
}
99136

0 commit comments

Comments
 (0)