Skip to content

Commit c8f05b7

Browse files
feat: add select divider (#9)
* feat: add select divider * chore: update logic and examples * test: update snapshots
1 parent 354e4c7 commit c8f05b7

File tree

15 files changed

+238
-50
lines changed

15 files changed

+238
-50
lines changed

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/components/base/src/accordion/specs/__snapshots__/index.snapshot.test.tsx.snap

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ exports[`snapshot accordion renders correctly 1`] = `
1515
class="header"
1616
>
1717
<span
18-
class="root size_text skin_default"
18+
class="VsR_m2r2hGIr7NfX85BU YEVFelv_bfcF2Ee4WG8g sf93RNkSSnS5VkGCtSLh"
1919
>
2020
<svg
2121
class="icon"
@@ -56,7 +56,7 @@ exports[`snapshot accordion renders correctly 1`] = `
5656
class="header"
5757
>
5858
<span
59-
class="root size_text skin_default"
59+
class="VsR_m2r2hGIr7NfX85BU YEVFelv_bfcF2Ee4WG8g sf93RNkSSnS5VkGCtSLh"
6060
>
6161
<svg
6262
class="icon"

packages/components/base/src/button/specs/__snapshots__/index.snapshot.test.tsx.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ exports[`snapshot Button renders correctly 1`] = `
1414
class="root text"
1515
>
1616
<span
17-
class="root size_l skin_default"
17+
class="VsR_m2r2hGIr7NfX85BU YicCROjsCroMsOGSwMAE sf93RNkSSnS5VkGCtSLh"
1818
>
1919
<svg
2020
fill="currentColor"

packages/components/base/src/collapse/specs/__snapshots__/index.snapshot.test.tsx.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ exports[`snapshot Collapse renders correctly 1`] = `
99
class="header"
1010
>
1111
<span
12-
class="root size_text skin_default"
12+
class="VsR_m2r2hGIr7NfX85BU YEVFelv_bfcF2Ee4WG8g sf93RNkSSnS5VkGCtSLh"
1313
>
1414
<svg
1515
class="icon"

packages/components/base/src/menu/styles/menu-divider.module.scss

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
@use "@react-ck/scss-utils";
44

55
.root {
6+
background-color: theme.get-color(neutral-light-300);
67
display: block;
78
box-sizing: border-box;
8-
background-color: theme.get-color(neutral-light-300);
99

1010
&.text {
1111
@include text.text-base(extra-small);

packages/components/base/src/pagination/specs/__snapshots__/index.snapshot.test.tsx.snap

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ exports[`snapshot Pagination renders correctly 1`] = `
1717
class="root text"
1818
>
1919
<span
20-
class="root size_text skin_default"
20+
class="VsR_m2r2hGIr7NfX85BU YEVFelv_bfcF2Ee4WG8g sf93RNkSSnS5VkGCtSLh"
2121
>
2222
<svg
2323
fill="currentColor"
@@ -48,7 +48,7 @@ exports[`snapshot Pagination renders correctly 1`] = `
4848
class="root text"
4949
>
5050
<span
51-
class="root size_text skin_default"
51+
class="VsR_m2r2hGIr7NfX85BU YEVFelv_bfcF2Ee4WG8g sf93RNkSSnS5VkGCtSLh"
5252
>
5353
<svg
5454
fill="currentColor"
@@ -79,7 +79,7 @@ exports[`snapshot Pagination renders correctly 1`] = `
7979
class="root text"
8080
>
8181
<span
82-
class="root size_text skin_default"
82+
class="VsR_m2r2hGIr7NfX85BU YEVFelv_bfcF2Ee4WG8g sf93RNkSSnS5VkGCtSLh"
8383
>
8484
<svg
8585
fill="currentColor"
@@ -110,7 +110,7 @@ exports[`snapshot Pagination renders correctly 1`] = `
110110
class="root text"
111111
>
112112
<span
113-
class="root size_text skin_default"
113+
class="VsR_m2r2hGIr7NfX85BU YEVFelv_bfcF2Ee4WG8g sf93RNkSSnS5VkGCtSLh"
114114
>
115115
<svg
116116
fill="currentColor"
@@ -141,7 +141,7 @@ exports[`snapshot Pagination renders correctly 1`] = `
141141
class="root text"
142142
>
143143
<span
144-
class="root size_text skin_default"
144+
class="VsR_m2r2hGIr7NfX85BU YEVFelv_bfcF2Ee4WG8g sf93RNkSSnS5VkGCtSLh"
145145
>
146146
<svg
147147
fill="currentColor"
@@ -172,7 +172,7 @@ exports[`snapshot Pagination renders correctly 1`] = `
172172
class="root text"
173173
>
174174
<span
175-
class="root size_text skin_default"
175+
class="VsR_m2r2hGIr7NfX85BU YEVFelv_bfcF2Ee4WG8g sf93RNkSSnS5VkGCtSLh"
176176
>
177177
<svg
178178
fill="currentColor"
@@ -203,7 +203,7 @@ exports[`snapshot Pagination renders correctly 1`] = `
203203
class="root text"
204204
>
205205
<span
206-
class="root size_text skin_default"
206+
class="VsR_m2r2hGIr7NfX85BU YEVFelv_bfcF2Ee4WG8g sf93RNkSSnS5VkGCtSLh"
207207
>
208208
<svg
209209
fill="currentColor"
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import React from "react";
2+
import { DISPLAY_NAME_ATTRIBUTE } from "@react-ck/react-utils";
3+
import type { SelectDividerProps } from "./types";
4+
import styles from "./styles/index.module.scss";
5+
import classNames from "classnames";
6+
7+
/**
8+
* A divider component for the Select component.
9+
* Used to group options visually within a select dropdown.
10+
*
11+
* @param props - Component props {@link SelectDividerProps}
12+
* @returns React element
13+
*/
14+
function SelectDivider({
15+
className,
16+
children,
17+
...props
18+
}: Readonly<SelectDividerProps>): React.ReactElement {
19+
return (
20+
<div
21+
className={classNames(styles.divider, Boolean(children) && styles.divider_text, className)}
22+
{...props}>
23+
{children}
24+
</div>
25+
);
26+
}
27+
28+
SelectDivider[DISPLAY_NAME_ATTRIBUTE] = "SelectDivider";
29+
30+
export { SelectDivider };

packages/components/base/src/select/SelectOption.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { Menu } from "../menu";
22
import React, { useContext, useMemo } from "react";
33
import { SelectContext } from "./context";
44
import { DISPLAY_NAME_ATTRIBUTE } from "@react-ck/react-utils";
5-
import { type SelectOptionProps } from "./types";
5+
import type { SelectOptionProps } from "./types";
66

77
const SelectOption = ({
88
value,

packages/components/base/src/select/index.tsx

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
11
import styles from "./styles/index.module.scss";
22
import React, { useCallback, useEffect, useMemo, useRef, useState } from "react";
33
import { SelectOption } from "./SelectOption";
4+
import { SelectDivider } from "./SelectDivider";
45
import { Menu } from "../menu";
56
import { Dropdown } from "../dropdown";
67
import { Input } from "../input";
78
import classNames from "classnames";
8-
import { megeRefs, raf } from "@react-ck/react-utils";
9+
import { mergeRefs, raf } from "@react-ck/react-utils";
910
import { EmptyState } from "../empty-state";
1011
import { getChildrenData, simplifyString, valueAsArray } from "./utils";
1112
import { type SelectProps, type ChangeHandler } from "./types";
1213
import { SelectContext, type SelectContextProps } from "./context";
1314
import { useFormFieldContext } from "../form-field";
15+
import { Spinner } from "../spinner";
16+
import { Icon } from "@react-ck/icon";
17+
import { IconChevronDown } from "@react-ck/icon/icons/IconChevronDown";
1418

1519
/** Default positions to exclude from auto-positioning */
1620
const defaultExclude: SelectProps["excludeAutoPosition"] = [
@@ -77,6 +81,7 @@ const Select = ({
7781
disabled,
7882
displayValueDivider = ",",
7983
fullWidth,
84+
loading,
8085
position,
8186
excludeAutoPosition = defaultExclude,
8287
ref,
@@ -256,7 +261,9 @@ const Select = ({
256261
const resizeObserver = new ResizeObserver(() => {
257262
if (!valueSlotRefCurrent || !sizeSetterRef.current) return;
258263

259-
valueSlotRefCurrent.style.width = `${sizeSetterRef.current.clientWidth + 10}px`;
264+
if (valueSlotRefCurrent.clientWidth < sizeSetterRef.current.clientWidth) {
265+
valueSlotRefCurrent.style.width = `${sizeSetterRef.current.clientWidth + 10}px`;
266+
}
260267
});
261268
resizeObserver.observe(sizeSetterRef.current);
262269

@@ -278,7 +285,7 @@ const Select = ({
278285
styles[`skin_${computedSkin}`],
279286
formFieldContext === undefined && styles.standalone,
280287
(disabled || formFieldContext?.disabled) && styles.disabled,
281-
288+
loading && styles.loading,
282289
(fullWidth ?? formFieldContext?.fullWidth) && styles.full_width,
283290
className,
284291
)}
@@ -291,11 +298,17 @@ const Select = ({
291298
onBlur?.(e);
292299
}}>
293300
<div ref={valueSlotRef} className={styles.value_slot}>
294-
{displayValue || <span className={styles.placeholder}>{placeholder}</span>}
301+
<div className={styles.value_content}>
302+
{displayValue || <span className={styles.placeholder}>{placeholder}</span>}
303+
</div>
304+
{loading && <Spinner size="l" />}
305+
<Icon size="l">
306+
<IconChevronDown />
307+
</Icon>
295308
</div>
296309

297310
<select
298-
ref={megeRefs(ref, selectRef)}
311+
ref={mergeRefs(ref, selectRef)}
299312
id={computedId}
300313
name={selectName}
301314
multiple={selectMultiple}
@@ -372,15 +385,17 @@ const Select = ({
372385

373386
type SelectWithOption = typeof Select & {
374387
Option: typeof SelectOption;
388+
Divider: typeof SelectDivider;
375389
};
376390

377-
// Add the Option property
391+
// Add the Option and Divider properties
378392

379393
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions -- needed for compound component definition with forward ref
380394
const CompoundSelect: SelectWithOption = Select as SelectWithOption;
381395

382396
CompoundSelect.Option = SelectOption;
397+
CompoundSelect.Divider = SelectDivider;
383398

384399
export { CompoundSelect as Select };
385400

386-
export { type SelectOptionProps, type SelectProps } from "./types";
401+
export { type SelectOptionProps, type SelectProps, type SelectDividerProps } from "./types";

packages/components/base/src/select/specs/__snapshots__/index.snapshot.test.tsx.snap

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,31 @@ exports[`snapshot Select renders correctly 1`] = `
1010
<div
1111
class="value_slot"
1212
>
13+
<div
14+
class="value_content"
15+
>
16+
<span
17+
class="placeholder"
18+
/>
19+
</div>
1320
<span
14-
class="placeholder"
15-
/>
21+
class="VsR_m2r2hGIr7NfX85BU YicCROjsCroMsOGSwMAE sf93RNkSSnS5VkGCtSLh"
22+
>
23+
<svg
24+
fill="none"
25+
height="1em"
26+
stroke="currentColor"
27+
stroke-width="0"
28+
viewBox="0 0 24 24"
29+
width="1em"
30+
xmlns="http://www.w3.org/2000/svg"
31+
>
32+
<path
33+
d="M6.34317 7.75732L4.92896 9.17154L12 16.2426L19.0711 9.17157L17.6569 7.75735L12 13.4142L6.34317 7.75732Z"
34+
fill="currentColor"
35+
/>
36+
</svg>
37+
</span>
1638
</div>
1739
<select
1840
class="native_element"

0 commit comments

Comments
 (0)