Skip to content

Commit 3167d57

Browse files
committed
feat(ui): select support dMonospaced prop
1 parent 3090bab commit 3167d57

2 files changed

Lines changed: 43 additions & 17 deletions

File tree

packages/ui/src/components/select/Select.tsx

Lines changed: 41 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ export interface DSelectProps<V extends DId, T extends DSelectItem<V>> extends O
5151
dClearable?: boolean;
5252
dDisabled?: boolean;
5353
dMultiple?: boolean;
54+
dMonospaced?: boolean;
5455
dVirtual?: boolean;
5556
dCustomItem?: (item: T) => React.ReactNode;
5657
dCustomSelected?: (select: T) => string;
@@ -89,6 +90,7 @@ function Select<V extends DId, T extends DSelectItem<V>>(
8990
dClearable = false,
9091
dDisabled = false,
9192
dMultiple = false,
93+
dMonospaced = true,
9294
dVirtual = false,
9395
dCustomItem,
9496
dCustomSelected,
@@ -306,22 +308,45 @@ function Select<V extends DId, T extends DSelectItem<V>>(
306308
const [transformOrigin, setTransformOrigin] = useState<string>();
307309
const updatePosition = useEventCallback(() => {
308310
if (visible && boxRef.current && popupRef.current) {
309-
const width = Math.min(boxRef.current.offsetWidth, window.innerWidth - WINDOW_SPACE * 2);
310-
const height = popupRef.current.offsetHeight;
311-
const { top, left, transformOrigin } = getVerticalSidePosition(
312-
boxRef.current,
313-
{ width, height },
314-
{
315-
placement: 'bottom',
316-
inWindow: WINDOW_SPACE,
317-
}
318-
);
319-
setPopupPositionStyle({
320-
top,
321-
left,
322-
width,
323-
});
324-
setTransformOrigin(transformOrigin);
311+
if (dMonospaced) {
312+
const width = Math.min(boxRef.current.offsetWidth, window.innerWidth - WINDOW_SPACE * 2);
313+
const height = popupRef.current.offsetHeight;
314+
const { top, left, transformOrigin } = getVerticalSidePosition(
315+
boxRef.current,
316+
{ width, height },
317+
{
318+
placement: 'bottom',
319+
inWindow: WINDOW_SPACE,
320+
}
321+
);
322+
setPopupPositionStyle({
323+
top,
324+
left,
325+
width,
326+
});
327+
setTransformOrigin(transformOrigin);
328+
} else {
329+
const boxWidth = boxRef.current.offsetWidth;
330+
const height = popupRef.current.offsetHeight;
331+
const maxWidth = window.innerWidth - WINDOW_SPACE * 2;
332+
const width = Math.min(Math.max(popupRef.current.scrollWidth, boxWidth), maxWidth);
333+
const { top, left, transformOrigin } = getVerticalSidePosition(
334+
boxRef.current,
335+
{ width, height },
336+
{
337+
placement: 'bottom-left',
338+
inWindow: WINDOW_SPACE,
339+
}
340+
);
341+
342+
setPopupPositionStyle({
343+
top,
344+
left,
345+
minWidth: Math.min(boxWidth, maxWidth),
346+
maxWidth,
347+
});
348+
setTransformOrigin(transformOrigin);
349+
}
325350
}
326351
});
327352

packages/ui/src/components/select/demos/1.Basic.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import { useState } from 'react';
1818
import { DSelect } from '@react-devui/ui';
1919

2020
const list = Array.from({ length: 100 }).map((_, index) => ({
21-
label: `Item ${index}`,
21+
label: `Long item ${index}`,
2222
value: index,
2323
disabled: index === 3,
2424
}));
@@ -28,6 +28,7 @@ export default function Demo() {
2828
return (
2929
<>
3030
<DSelect style={{ width: 200 }} dList={list} dPlaceholder="Basic" />
31+
<DSelect style={{ width: 100 }} dList={list} dPlaceholder="No Monospaced" dMonospaced={false} />
3132
<DSelect style={{ width: 200 }} dList={list} dPlaceholder="Disabled" dDisabled />
3233
<DSelect style={{ width: 200 }} dList={list} dPlaceholder="Clearable" dModel={select} dClearable onModelChange={setSelect} />
3334
</>

0 commit comments

Comments
 (0)