Skip to content

Commit 132ca0d

Browse files
committed
Handle overflow in SelectNext
1 parent de1df29 commit 132ca0d

File tree

2 files changed

+61
-18
lines changed

2 files changed

+61
-18
lines changed

src/components/input/SelectNext.tsx

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ function SelectMain<T>({
194194
id={buttonId ?? defaultButtonId}
195195
variant="custom"
196196
classes={classnames(
197-
'w-full flex',
197+
'w-full flex justify-between',
198198
'bg-grey-0 disabled:bg-grey-1 disabled:text-grey-6',
199199
// Add inherited rounded corners so that the toggle is consistent with
200200
// the wrapper, which is the element rendering borders.
@@ -217,16 +217,15 @@ function SelectMain<T>({
217217
}}
218218
data-testid="select-toggle-button"
219219
>
220-
{buttonContent ?? label}
221-
<div className="grow" />
220+
<div className="truncate">{buttonContent ?? label}</div>
222221
<div className="text-grey-6">
223222
{listboxOpen ? <MenuCollapseIcon /> : <MenuExpandIcon />}
224223
</div>
225224
</Button>
226225
<SelectContext.Provider value={{ selectValue, value }}>
227226
<ul
228227
className={classnames(
229-
'absolute z-5 w-full max-h-80 overflow-y-auto',
228+
'absolute z-5 min-w-full max-h-80 overflow-y-auto',
230229
'rounded border bg-white shadow hover:shadow-md focus-within:shadow-md',
231230
{
232231
'top-full mt-1': !shouldListboxDropUp,

src/pattern-library/components/patterns/prototype/SelectNextPage.tsx

Lines changed: 58 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,12 @@ const defaultItems = [
1717
function SelectExample({
1818
disabled,
1919
textOnly,
20+
classes,
2021
items = defaultItems,
2122
}: {
2223
disabled?: boolean;
2324
textOnly?: boolean;
25+
classes?: string;
2426
items?: typeof defaultItems;
2527
}) {
2628
const [value, setValue] = useState<(typeof items)[number]>();
@@ -29,13 +31,17 @@ function SelectExample({
2931
<SelectNext
3032
value={value}
3133
onChange={setValue}
34+
classes={classes}
3235
buttonContent={
3336
value ? (
3437
<>
35-
{value.name}
38+
{textOnly && value.name}
3639
{!textOnly && (
37-
<div className="rounded px-2 bg-grey-7 text-white">
38-
{value.id}
40+
<div className="flex">
41+
<div className="truncate">{value.name}</div>
42+
<div className="rounded px-2 ml-2 bg-grey-7 text-white">
43+
{value.id}
44+
</div>
3945
</div>
4046
)}
4147
</>
@@ -56,7 +62,7 @@ function SelectExample({
5662
<>
5763
{item.name}
5864
<div className="grow" />
59-
<div className="rounded px-2 bg-grey-7 text-white">
65+
<div className="rounded px-2 ml-2 bg-grey-7 text-white">
6066
{item.id}
6167
</div>
6268
</>
@@ -68,7 +74,7 @@ function SelectExample({
6874
);
6975
}
7076

71-
function InputGroupSelectExample() {
77+
function InputGroupSelectExample({ classes }: { classes?: string }) {
7278
const [selected, setSelected] = useState<(typeof defaultItems)[number]>();
7379
const selectedIndex = useMemo(
7480
() => (!selected ? -1 : defaultItems.findIndex(item => item === selected)),
@@ -95,14 +101,15 @@ function InputGroupSelectExample() {
95101
<SelectNext
96102
value={selected}
97103
onChange={setSelected}
104+
classes={classes}
98105
buttonContent={
99106
selected ? (
100-
<>
101-
{selected.name}
102-
<div className="rounded px-2 bg-grey-7 text-white">
107+
<div className="flex">
108+
<div className="truncate">{selected.name}</div>
109+
<div className="rounded px-2 ml-2 bg-grey-7 text-white">
103110
{selected.id}
104111
</div>
105-
</>
112+
</div>
106113
) : (
107114
<>Select one...</>
108115
)
@@ -115,7 +122,9 @@ function InputGroupSelectExample() {
115122
{item.name}
116123
<div className="grow" />
117124
<div
118-
className={classnames('rounded px-2 text-white bg-grey-7')}
125+
className={classnames(
126+
'rounded px-2 ml-2 text-white bg-grey-7',
127+
)}
119128
>
120129
{item.id}
121130
</div>
@@ -152,7 +161,7 @@ export default function SelectNextPage() {
152161

153162
<Library.Example>
154163
<Library.Demo title="Basic Select">
155-
<div className="w-[350px] mx-auto">
164+
<div className="w-96 mx-auto">
156165
<SelectExample textOnly />
157166
</div>
158167
</Library.Demo>
@@ -181,7 +190,7 @@ export default function SelectNextPage() {
181190

182191
<Library.Example title="Select with many options">
183192
<Library.Demo title="Select with many options">
184-
<div className="w-[350px] mx-auto">
193+
<div className="w-96 mx-auto">
185194
<SelectExample
186195
items={[
187196
...defaultItems.map(({ id, name }) => ({
@@ -216,11 +225,46 @@ export default function SelectNextPage() {
216225

217226
<Library.Example title="Disabled Select">
218227
<Library.Demo title="Disabled Select">
219-
<div className="w-[350px] mx-auto">
228+
<div className="w-96 mx-auto">
220229
<SelectExample disabled />
221230
</div>
222231
</Library.Demo>
223232
</Library.Example>
233+
234+
<Library.Example title="Select with long content">
235+
<p>
236+
<code>SelectNext</code> makes sure the button content never
237+
overflows, and applies <code>text-overflow: ellipsis</code>.
238+
</p>
239+
<p>
240+
If you provide more complex children to <code>buttonContent</code>
241+
, and the default hidden overflow logic does not work for your use
242+
case, it is up to you to handle the overflow logic in your
243+
components.
244+
</p>
245+
<p>
246+
On the other hand, the listbox will always grow to fit its
247+
options.
248+
</p>
249+
250+
<Library.Demo title="Plain text">
251+
<div className="mx-auto">
252+
<SelectExample textOnly classes="!w-36" />
253+
</div>
254+
</Library.Demo>
255+
256+
<Library.Demo title="Custom options">
257+
<div className="mx-auto">
258+
<SelectExample classes="!w-36" />
259+
</div>
260+
</Library.Demo>
261+
262+
<Library.Demo title="Input group">
263+
<div className="mx-auto">
264+
<InputGroupSelectExample classes="!w-36" />
265+
</div>
266+
</Library.Demo>
267+
</Library.Example>
224268
</Library.Pattern>
225269

226270
<Library.Pattern title="Component API">
@@ -303,7 +347,7 @@ export default function SelectNextPage() {
303347
value ? (
304348
<>
305349
{value.name}
306-
<div className="rounded px-2 bg-grey-7 text-white">
350+
<div className="rounded px-2 ml-2 bg-grey-7 text-white">
307351
{value.id}
308352
</div>
309353
</>

0 commit comments

Comments
 (0)