Skip to content

Commit 5b42133

Browse files
Merge pull request #5813 from matuzalemsteles/LPD-23824
feat(@clayui/core): adds new width API for the picker
2 parents cd71751 + 2e82f28 commit 5b42133

File tree

6 files changed

+153
-4
lines changed

6 files changed

+153
-4
lines changed

packages/clay-core/docs/picker.js

+44
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,49 @@ import Layout from '@clayui/layout';
1313
import {useId} from '@clayui/shared';
1414
import React from 'react';
1515

16+
const PickerWidthExample = () => {
17+
const imports = `import {Option, Picker} from '@clayui/core';
18+
import Form from '@clayui/form';
19+
import React from 'react';`;
20+
21+
const code = `const CustomWidth = () => {
22+
return (
23+
<div style={{width: '100px'}}>
24+
<Form.Group>
25+
<label htmlFor="picker" id="picker-label">
26+
Year
27+
</label>
28+
<Picker
29+
width={85}
30+
aria-labelledby="picker-label"
31+
id="picker"
32+
items={[
33+
'2020',
34+
'2021',
35+
'2022',
36+
'2023',
37+
'2024',
38+
'2025',
39+
'2026',
40+
'2027',
41+
'2028',
42+
]}
43+
placeholder="Year"
44+
>
45+
{(item) => <Option key={item}>{item}</Option>}
46+
</Picker>
47+
</Form.Group>
48+
</div>
49+
);
50+
};
51+
52+
render(<CustomWidth />)`;
53+
54+
return (
55+
<Editor code={code} imports={imports} scope={{Form, Option, Picker}} />
56+
);
57+
};
58+
1659
const exampleImports = `import {Option, Picker} from '@clayui/core';
1760
import Form from '@clayui/form';
1861
import React from 'react';`;
@@ -262,6 +305,7 @@ const PickerGroupExample = () => {
262305
);
263306
};
264307
export {
308+
PickerWidthExample,
265309
PickerExample,
266310
PickerGroupExample,
267311
PickerTriggerExample,

packages/clay-core/docs/picker.mdx

+6
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ storybookPath: 'design-system-components-picker'
77
---
88

99
import {
10+
PickerWidthExample,
1011
PickerExample,
1112
PickerGroupExample,
1213
PickerTriggerExample,
@@ -26,6 +27,7 @@ import {
2627
- [Custom Trigger](#custom-trigger)
2728
- [Custom Options](#custom-options)
2829
- [Group](#group)
30+
- [Flexibe width](#flexibe-width)
2931
- [Hybrid component](#hybrid-component)
3032

3133
</div>
@@ -153,6 +155,10 @@ The composition allows you to customize the component or add new features. See s
153155

154156
<PickerGroupExample />
155157

158+
## Flexible width
159+
160+
<PickerWidthExample />
161+
156162
## Hybrid component
157163

158164
Native select for mobile devices offers a better experience compared to Picker in some cases. The Picker offers the possibility to render using the native selector of the browser of the device when it is detected that it is on a mobile device, by default this property is disabled but it can be enabled by setting the `native` property to `true`.

packages/clay-core/src/picker/Picker.tsx

+19-2
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,11 @@ export type Props<T> = {
126126
*/
127127
selectedKey?: React.Key;
128128

129+
/**
130+
* Sets the fixed width of the panel.
131+
*/
132+
width?: number;
133+
129134
/**
130135
* Sets the className for the React.Portal Menu element.
131136
*/
@@ -163,6 +168,7 @@ export function Picker<T extends Record<string, any> | string | number>({
163168
onSelectionChange,
164169
placeholder = 'Select an option',
165170
selectedKey: externalSelectedKey,
171+
width,
166172
...otherProps
167173
}: Props<T>) {
168174
const [active, setActive] = useControlledState({
@@ -521,14 +527,25 @@ export function Picker<T extends Record<string, any> | string | number>({
521527
>
522528
<div
523529
className={classNames(
524-
'dropdown-menu dropdown-menu-indicator-start dropdown-menu-select show',
530+
'dropdown-menu dropdown-menu-indicator-start dropdown-menu-select dropdown-menu-width-shrink show',
525531
{
526-
'dropdown-menu-height-lg dropdown-menu-width-shrink':
532+
'dropdown-menu-height-lg':
527533
UNSAFE_behavior === 'secondary',
528534
}
529535
)}
530536
ref={menuRef}
531537
role="presentation"
538+
style={{
539+
maxWidth: 'none',
540+
width: `${
541+
typeof width === 'number'
542+
? width
543+
: (triggerRef.current?.clientWidth || 0) >
544+
160
545+
? triggerRef.current?.clientWidth
546+
: 160
547+
}px`,
548+
}}
532549
>
533550
{UNSAFE_behavior === 'secondary' &&
534551
(isArrowVisible === 'top' ||

packages/clay-core/src/picker/__tests__/__snapshots__/BasicRendering.tsx.snap

+2-2
Original file line numberDiff line numberDiff line change
@@ -132,9 +132,9 @@ exports[`Picker basic rendering renders open component by default 1`] = `
132132
</div>
133133
<div>
134134
<div
135-
class="dropdown-menu dropdown-menu-indicator-start dropdown-menu-select show"
135+
class="dropdown-menu dropdown-menu-indicator-start dropdown-menu-select dropdown-menu-width-shrink show"
136136
role="presentation"
137-
style="left: -999px; top: -995px;"
137+
style="max-width: none; width: 160px; left: -999px; top: -995px;"
138138
>
139139
<ul
140140
class="inline-scroller list-unstyled"

packages/clay-core/stories/Picker.stories.tsx

+81
Original file line numberDiff line numberDiff line change
@@ -225,3 +225,84 @@ export const CustomGroup = () => {
225225
</div>
226226
);
227227
};
228+
229+
export const Width = () => {
230+
const pickerId = useId();
231+
const labelId = useId();
232+
233+
return (
234+
<>
235+
<div style={{width: '250px'}}>
236+
<Form.Group>
237+
<label htmlFor={pickerId} id={labelId}>
238+
Choose a fruit
239+
</label>
240+
<Picker aria-labelledby={labelId} id={pickerId}>
241+
<Option key="apple">Apple</Option>
242+
<Option disabled key="banana">
243+
Banana
244+
</Option>
245+
<Option key="mangos">Mangos</Option>
246+
<Option key="blueberry">Blueberry</Option>
247+
<Option key="boysenberry">Boysenberry</Option>
248+
<Option key="cherry">Cherry</Option>
249+
<Option key="cranberry">Cranberry</Option>
250+
<Option key="eggplant">Eggplant</Option>
251+
<Option key="fig">Fig</Option>
252+
<Option key="grape">Grape</Option>
253+
<Option key="guava">Guava</Option>
254+
<Option key="huckleberry">Huckleberry</Option>
255+
</Picker>
256+
</Form.Group>
257+
</div>
258+
259+
<div style={{width: '100px'}}>
260+
<Form.Group>
261+
<label htmlFor={pickerId} id={labelId}>
262+
Choose a fruit
263+
</label>
264+
<Picker
265+
aria-labelledby={labelId}
266+
id={pickerId}
267+
placeholder="Fruit"
268+
>
269+
<Option key="apple">Apple</Option>
270+
<Option disabled key="banana">
271+
Banana
272+
</Option>
273+
<Option key="mangos">Mangos</Option>
274+
<Option key="blueberry">Blueberry</Option>
275+
<Option key="boysenberry">Boysenberry</Option>
276+
<Option key="cherry">Cherry</Option>
277+
<Option key="cranberry">Cranberry</Option>
278+
<Option key="eggplant">Eggplant</Option>
279+
<Option key="fig">Fig</Option>
280+
<Option key="grape">Grape</Option>
281+
<Option key="guava">Guava</Option>
282+
<Option key="huckleberry">Huckleberry</Option>
283+
</Picker>
284+
</Form.Group>
285+
</div>
286+
287+
<div style={{width: '100px'}}>
288+
<Form.Group>
289+
<label htmlFor={pickerId} id={labelId}>
290+
Year
291+
</label>
292+
<Picker
293+
aria-labelledby={labelId}
294+
id={pickerId}
295+
placeholder="Year"
296+
width={85}
297+
>
298+
<Option key="2020">2020</Option>
299+
<Option key="2021">2021</Option>
300+
<Option key="2022">2022</Option>
301+
<Option key="2023">2023</Option>
302+
<Option key="2024">2024</Option>
303+
</Picker>
304+
</Form.Group>
305+
</div>
306+
</>
307+
);
308+
};

packages/clay-date-picker/src/DateNavigation.tsx

+1
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ const ClayDatePickerDateNavigation = ({
109109
)
110110
}
111111
selectedKey={String(currentMonth.getFullYear())}
112+
width={85}
112113
>
113114
{(item) => (
114115
<Option key={item.value}>

0 commit comments

Comments
 (0)