Skip to content

Commit c075b15

Browse files
committed
fix: resize
1 parent 7056f2d commit c075b15

2 files changed

Lines changed: 40 additions & 11 deletions

File tree

packages/components/select-input/useSingle.tsx

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -98,12 +98,29 @@ export default function useSingle(props: SelectInputProps) {
9898
return;
9999
}
100100
const el = customElementRef.current;
101-
// 测量真实内容宽度时,临时强制 nowrap,避免被父级容器(受 suffixSpace 影响)压缩换行导致测量值偏小
102-
const prevWhiteSpace = el.style.whiteSpace;
103-
el.style.whiteSpace = 'nowrap';
104-
const { width } = el.getBoundingClientRect();
105-
el.style.whiteSpace = prevWhiteSpace;
106-
inputEl.style.minWidth = width > 0 ? `${width}px` : '';
101+
102+
const measure = () => {
103+
// 测量真实内容宽度时,临时强制 nowrap,避免被父级容器(受 suffixSpace 影响)压缩换行导致测量值偏小
104+
const prevWhiteSpace = el.style.whiteSpace;
105+
el.style.whiteSpace = 'nowrap';
106+
const { width } = el.getBoundingClientRect();
107+
el.style.whiteSpace = prevWhiteSpace;
108+
inputEl.style.minWidth = width > 0 ? `${width}px` : '';
109+
};
110+
111+
measure();
112+
113+
let ro: ResizeObserver | null = null;
114+
if (typeof ResizeObserver !== 'undefined') {
115+
ro = new ResizeObserver(() => {
116+
measure();
117+
});
118+
ro.observe(el);
119+
}
120+
121+
return () => {
122+
ro?.disconnect();
123+
};
107124
}, [autoWidth, showCustomElement, singleValueDisplay]);
108125

109126
useEffect(() => {

packages/components/select/base/Select.tsx

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,11 @@ const Select = forwardRefWithStatics(
202202
const handleRemove = (removeIndex, trigger, e, label) => {
203203
const values = getSelectValueArr(value, value[removeIndex], true, valueType, keys);
204204
const { currentSelectedOptions } = getSelectedOptions(values, multiple, valueType, keys, valueToOption);
205-
onChange(values, { e, trigger, selectedOptions: currentSelectedOptions });
205+
onChange(values, {
206+
e,
207+
trigger,
208+
selectedOptions: currentSelectedOptions,
209+
});
206210
onRemove?.({
207211
value: value[removeIndex],
208212
data: {
@@ -356,7 +360,9 @@ const Select = forwardRefWithStatics(
356360
return;
357361
}
358362
if (isFunction(onSearch)) {
359-
onSearch(value, { e: context.e as React.KeyboardEvent<HTMLDivElement> });
363+
onSearch(value, {
364+
e: context.e as React.KeyboardEvent<HTMLDivElement>,
365+
});
360366
return;
361367
}
362368
};
@@ -482,7 +488,10 @@ const Select = forwardRefWithStatics(
482488
if (multiple) {
483489
return ({ onClose }) => parseContentTNode(valueDisplay, { value: selectedOptions, onClose });
484490
}
485-
return parseContentTNode(valueDisplay, { value: selectedLabel, onClose: noop });
491+
return parseContentTNode(valueDisplay, {
492+
value: selectedLabel,
493+
onClose: noop,
494+
});
486495
// eslint-disable-next-line react-hooks/exhaustive-deps
487496
}, [
488497
valueDisplay,
@@ -556,7 +565,7 @@ const Select = forwardRefWithStatics(
556565
autoWidth={!style?.width && autoWidth}
557566
ref={composeRefs(ref, selectInputRef)}
558567
className={name}
559-
readonly={readOnly}
568+
readOnly={readOnly}
560569
autofocus={props.autofocus}
561570
allowInput={(filterable ?? local.filterable) || isFunction(filter)}
562571
multiple={multiple}
@@ -602,7 +611,10 @@ const Select = forwardRefWithStatics(
602611
onFocus={onFocus}
603612
onEnter={handleEnter}
604613
onBlur={(_, context) => {
605-
onBlur?.({ value, e: context.e as React.FocusEvent<HTMLDivElement> });
614+
onBlur?.({
615+
value,
616+
e: context.e as React.FocusEvent<HTMLDivElement>,
617+
});
606618
}}
607619
onClear={handleClear}
608620
{...selectInputProps}

0 commit comments

Comments
 (0)