@hellobetterdigitalnz/betterui 0.0.3-337 → 0.0.3-338

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hellobetterdigitalnz/betterui",
3
- "version": "0.0.3-337",
3
+ "version": "0.0.3-338",
4
4
  "type": "module",
5
5
  "main": "dist/index.cjs.js",
6
6
  "module": "dist/index.es.js",
@@ -1,52 +1,48 @@
1
- import type { Meta, StoryObj } from '@storybook/react';
2
- import DropdownField from "./DropdownField";
3
- import {NameValueInterface} from "./DropdownFieldProps";
4
- import Users from "../../Icons/People/Users/Users";
5
-
6
- // More on how to set up stories at: https://storybook.js.org/docs/writing-stories#default-export
7
- const meta = {
8
- title: 'Form / Dropdown Field',
9
- component: DropdownField,
10
- parameters: {
11
- layout: 'centered',
12
- },
13
- tags: ['autodocs'],
14
- } satisfies Meta<typeof DropdownField>;
15
-
16
- export default meta;
17
- type Story = StoryObj<typeof DropdownField>;
18
-
19
- // More on writing stories with args: https://storybook.js.org/docs/writing-stories/args
20
- export const Default: Story = {
21
- args: {
22
- name: 'dropdown-field',
23
- placeholder: 'Super long placeholder text',
24
- options: [
25
- 'Lorem ipsum',
26
- 'Suspendisse sit amet',
27
- 'In sagittis metus'
28
- ]
29
- },
30
- };
31
-
32
- export const Props: Story = {
33
- args: {
34
- name: 'dropdown-field',
35
- placeholder: 'Placeholder text',
36
- options: [
37
- {
38
- label: 'Lorem ipsum',
39
- value: 1,
40
- icon: <Users/>
41
- },
42
- {
43
- label: 'In sagittis metus',
44
- value: 2
45
- },
46
- {
47
- label: 'Suspendisse potenti',
48
- value: 3
49
- },
50
- ] as NameValueInterface[]
51
- },
52
- };
1
+ import type { Meta, StoryObj } from "@storybook/react";
2
+ import DropdownField from "./DropdownField";
3
+ import { NameValueInterface } from "./DropdownFieldProps";
4
+ import Users from "../../Icons/People/Users/Users";
5
+
6
+ // More on how to set up stories at: https://storybook.js.org/docs/writing-stories#default-export
7
+ const meta = {
8
+ title: "Form / Dropdown Field",
9
+ component: DropdownField,
10
+ parameters: {
11
+ layout: "centered",
12
+ },
13
+ tags: ["autodocs"],
14
+ } satisfies Meta<typeof DropdownField>;
15
+
16
+ export default meta;
17
+ type Story = StoryObj<typeof DropdownField>;
18
+
19
+ // More on writing stories with args: https://storybook.js.org/docs/writing-stories/args
20
+ export const Default: Story = {
21
+ args: {
22
+ name: "dropdown-field",
23
+ placeholder: "Super long placeholder text",
24
+ options: ["Lorem ipsum", "Suspendisse sit amet", "In sagittis metus"],
25
+ },
26
+ };
27
+
28
+ export const Props: Story = {
29
+ args: {
30
+ name: "dropdown-field",
31
+ placeholder: "Placeholder text",
32
+ options: [
33
+ {
34
+ label: "Lorem ispsum",
35
+ value: 1,
36
+ icon: <Users />,
37
+ },
38
+ {
39
+ label: "In sagittis metus",
40
+ value: 2,
41
+ },
42
+ {
43
+ label: "Suspendisse potenti",
44
+ value: 3,
45
+ },
46
+ ] as NameValueInterface[],
47
+ },
48
+ };
@@ -1,243 +1,270 @@
1
- import cx from "classnames";
2
- import DropdownFieldProps, { NameValueInterface } from "./DropdownFieldProps";
3
- import styles from "./dropdown.module.scss";
4
- import { useEffect, useState, useRef, MouseEvent, useCallback } from "react";
5
- import CaretDown from "../../Icons/Arrows/CaretDown/CaretDown";
6
- import CaretUp from "../../Icons/Arrows/CaretUp/CaretUp";
7
- import DropdownFieldSelector from "./DropdownFieldSelector";
8
- import DropdownFieldItem from "./DropdownFieldItem";
9
- import LoadingInput from "../LoadingInput/LoadingInput.tsx";
10
- import { createPortal } from "react-dom";
11
-
12
- const DropdownField = (props: DropdownFieldProps) => {
13
- const {
14
- name,
15
- id,
16
- value,
17
- options,
18
- placeholder,
19
- extraClass,
20
- disabled = false,
21
- readonly = false,
22
- error,
23
- ariaLabel,
24
- required,
25
- emptyString,
26
- commentInDropdown,
27
- loading,
28
- onChange,
29
- } = props;
30
-
31
- const [opened, setOpened] = useState(false);
32
- const [selected, setSelected] = useState<string | number | undefined | null | boolean>();
33
- const [label, setLabel] = useState("");
34
- const [dropdownStyles, setDropdownStyles] = useState<React.CSSProperties>({});
35
-
36
- const dropdownRef = useRef<HTMLDivElement>(null);
37
- const dropdownSelectorRef = useRef<HTMLDivElement>(null);
38
- const controlRef = useRef<HTMLDivElement>(null);
39
- const lastPosition = useRef<DOMRect | null>(null);
40
-
41
- useEffect(() => {
42
- setSelected(value);
43
- }, [value]);
44
-
45
- useEffect(() => {
46
- for (const option of options) {
47
- let matched = null;
48
- if (typeof option === "string") {
49
- matched = option === selected ? option : null;
50
- } else {
51
- matched = option.value === selected ? option.label : null;
52
- }
53
- if (matched) {
54
- setLabel(matched);
55
- break;
56
- }
57
- }
58
- }, [selected, options]);
59
-
60
- const updateDropdownPosition = useCallback(() => {
61
- if (opened && controlRef.current && dropdownSelectorRef.current) {
62
- const rect = controlRef.current.getBoundingClientRect();
63
- const dropdownHeight = dropdownSelectorRef.current.offsetHeight;
64
- const spaceBelow = window.innerHeight - rect.bottom;
65
-
66
- const dropdownTop = spaceBelow < dropdownHeight ? rect.top - dropdownHeight : rect.bottom;
67
-
68
- setDropdownStyles({
69
- top: `${dropdownTop}px`,
70
- left: `${rect.left}px`,
71
- width: `${rect.width}px`,
72
- position: 'fixed',
73
- zIndex: 99999,
74
- });
75
- }
76
- }, [opened]);
77
-
78
- const handlePositionChanges = useCallback(() => {
79
- if (!controlRef.current) return;
80
-
81
- updateDropdownPosition();
82
- const currentPosition = controlRef.current.getBoundingClientRect();
83
-
84
- if (opened && lastPosition.current && (
85
- lastPosition.current.top !== currentPosition.top ||
86
- lastPosition.current.left !== currentPosition.left
87
- )) {
88
- setOpened(false);
89
- }
90
-
91
- lastPosition.current = currentPosition;
92
- }, [opened, updateDropdownPosition]);
93
-
94
- useEffect(() => {
95
- const handleClickOutside = (event: MouseEvent) => {
96
- if (
97
- dropdownRef.current &&
98
- !dropdownRef.current.contains(event.target as Node) &&
99
- dropdownSelectorRef.current &&
100
- !dropdownSelectorRef.current.contains(event.target as Node)
101
- ) {
102
- setOpened(false);
103
- }
104
- };
105
-
106
- // @ts-ignore
107
- document.addEventListener("mousedown", handleClickOutside);
108
-
109
- return () => {
110
- // @ts-ignore
111
- document.removeEventListener("mousedown", handleClickOutside);
112
- };
113
- }, []);
114
-
115
- useEffect(() => {
116
- if (opened) {
117
- window.addEventListener('resize', handlePositionChanges);
118
- window.addEventListener('scroll', handlePositionChanges, true);
119
- updateDropdownPosition();
120
- }
121
-
122
- return () => {
123
- window.removeEventListener('resize', handlePositionChanges);
124
- window.removeEventListener('scroll', handlePositionChanges, true);
125
- };
126
- }, [opened, handlePositionChanges, updateDropdownPosition]);
127
-
128
- useEffect(() => {
129
- if (!controlRef.current) return;
130
-
131
- const observer = new ResizeObserver(() => {
132
- if (!controlRef.current) return;
133
-
134
- const currentPosition = controlRef.current.getBoundingClientRect();
135
- if (lastPosition.current && (
136
- lastPosition.current.y !== currentPosition.y ||
137
- lastPosition.current.x !== currentPosition.x ||
138
- lastPosition.current.top !== currentPosition.top ||
139
- lastPosition.current.left !== currentPosition.left
140
- )) {
141
- setOpened(false);
142
- }
143
- lastPosition.current = currentPosition;
144
- });
145
-
146
- observer.observe(controlRef.current);
147
-
148
- return () => {
149
- observer.disconnect();
150
- };
151
- }, []);
152
-
153
- const classNames = [styles.dropdown];
154
-
155
- if (error) {
156
- classNames.push(styles.error);
157
- }
158
-
159
- if (extraClass) {
160
- classNames.push(extraClass);
161
- }
162
-
163
- if (disabled) {
164
- classNames.push(styles.disabled);
165
- }
166
-
167
- const handleOpenClose = () => {
168
- if (disabled || readonly) return;
169
- setOpened(!opened);
170
- };
171
- const handleSelect = (
172
- e: MouseEvent,
173
- value: string | number | undefined | null | boolean
174
- ) => {
175
- e.stopPropagation()
176
- setSelected(value);
177
- setOpened(false);
178
- if (onChange) {
179
- onChange(e, value);
180
- }
181
- };
182
-
183
-
184
- return (
185
- <div ref={dropdownRef} className={cx(classNames)}>
186
- {loading && <LoadingInput />}
187
- <div ref={controlRef} className={styles.control} onClick={handleOpenClose}>
188
- {!selected && placeholder && (
189
- <div className={styles.placeholder} aria-label={ariaLabel}>
190
- {placeholder}
191
- </div>
192
- )}
193
- {(selected || !placeholder) && (
194
- <div className={styles.label} aria-label={ariaLabel}>
195
- {label}
196
- {!!selected && <div className={styles.reset}></div>}
197
- </div>
198
- )}
199
- <div className={styles.chevron}>
200
- {!opened && <CaretDown />}
201
- {!!opened && <CaretUp />}
202
- </div>
203
- </div>
204
- { opened && (!readonly && !disabled) && createPortal(
205
- <DropdownFieldSelector ref={dropdownSelectorRef} style={{ ...dropdownStyles}}>
206
- {commentInDropdown && (
207
- <div className={styles.message}>{commentInDropdown}</div>
208
- )}
209
- {!!emptyString && (
210
- <DropdownFieldItem
211
- label={emptyString}
212
- value={''}
213
- handleSelect={handleSelect}
214
- />
215
- )}
216
- {options.map((option: string | NameValueInterface, index: number) => {
217
- return (
218
- <DropdownFieldItem
219
- key={index}
220
- handleSelect={handleSelect}
221
- label={typeof option === "object" ? option.label : option}
222
- value={typeof option === "object" ? option.value : option}
223
- />
224
- );
225
- })}
226
- </DropdownFieldSelector>,
227
- document.body
228
- )}
229
-
230
- <input
231
- name={name}
232
- value={selected ? selected.toString() : ""}
233
- id={id}
234
- type={"hidden"}
235
- required={required}
236
- disabled={disabled}
237
- readOnly={readonly}
238
- />
239
- </div>
240
- );
241
- };
242
-
243
- export default DropdownField;
1
+ import cx from "classnames";
2
+ import DropdownFieldProps, { NameValueInterface } from "./DropdownFieldProps";
3
+ import styles from "./dropdown.module.scss";
4
+ import { useEffect, useState, useRef, MouseEvent, useCallback } from "react";
5
+ import CaretDown from "../../Icons/Arrows/CaretDown/CaretDown";
6
+ import CaretUp from "../../Icons/Arrows/CaretUp/CaretUp";
7
+ import DropdownFieldSelector from "./DropdownFieldSelector";
8
+ import DropdownFieldItem from "./DropdownFieldItem";
9
+ import LoadingInput from "../LoadingInput/LoadingInput.tsx";
10
+ import { createPortal } from "react-dom";
11
+
12
+ const DropdownField = (props: DropdownFieldProps) => {
13
+ const {
14
+ name,
15
+ id,
16
+ value,
17
+ options,
18
+ placeholder,
19
+ extraClass,
20
+ disabled = false,
21
+ readonly = false,
22
+ error,
23
+ ariaLabel,
24
+ required,
25
+ emptyString,
26
+ commentInDropdown,
27
+ loading,
28
+ onChange,
29
+ } = props;
30
+
31
+ const [opened, setOpened] = useState(false);
32
+ const [selected, setSelected] = useState<
33
+ string | number | undefined | null | boolean
34
+ >();
35
+ const [label, setLabel] = useState("");
36
+ const [dropdownStyles, setDropdownStyles] = useState<React.CSSProperties>({});
37
+
38
+ const dropdownRef = useRef<HTMLDivElement>(null);
39
+ const dropdownSelectorRef = useRef<HTMLDivElement>(null);
40
+ const controlRef = useRef<HTMLDivElement>(null);
41
+ const lastPosition = useRef<DOMRect | null>(null);
42
+ const [selectedIcon, setSelectedIcon] = useState<React.ReactNode>(null);
43
+
44
+ useEffect(() => {
45
+ setSelected(value);
46
+ }, [value]);
47
+
48
+ useEffect(() => {
49
+ for (const option of options) {
50
+ let matched = null;
51
+ if (typeof option === "string") {
52
+ matched = option === selected ? option : null;
53
+ } else {
54
+ matched = option.value === selected ? option.label : null;
55
+ }
56
+ if (matched) {
57
+ setLabel(matched);
58
+ setSelectedIcon(
59
+ typeof option === "object" ? (option.icon ?? null) : null,
60
+ );
61
+ break;
62
+ }
63
+ }
64
+ }, [selected, options]);
65
+
66
+ const updateDropdownPosition = useCallback(() => {
67
+ if (opened && controlRef.current && dropdownSelectorRef.current) {
68
+ const rect = controlRef.current.getBoundingClientRect();
69
+ const dropdownHeight = dropdownSelectorRef.current.offsetHeight;
70
+ const spaceBelow = window.innerHeight - rect.bottom;
71
+
72
+ const dropdownTop =
73
+ spaceBelow < dropdownHeight ? rect.top - dropdownHeight : rect.bottom;
74
+
75
+ setDropdownStyles({
76
+ top: `${dropdownTop}px`,
77
+ left: `${rect.left}px`,
78
+ width: `${rect.width}px`,
79
+ position: "fixed",
80
+ zIndex: 99999,
81
+ });
82
+ }
83
+ }, [opened]);
84
+
85
+ const handlePositionChanges = useCallback(() => {
86
+ if (!controlRef.current) return;
87
+
88
+ updateDropdownPosition();
89
+ const currentPosition = controlRef.current.getBoundingClientRect();
90
+
91
+ if (
92
+ opened &&
93
+ lastPosition.current &&
94
+ (lastPosition.current.top !== currentPosition.top ||
95
+ lastPosition.current.left !== currentPosition.left)
96
+ ) {
97
+ setOpened(false);
98
+ }
99
+
100
+ lastPosition.current = currentPosition;
101
+ }, [opened, updateDropdownPosition]);
102
+
103
+ useEffect(() => {
104
+ const handleClickOutside = (event: MouseEvent) => {
105
+ if (
106
+ dropdownRef.current &&
107
+ !dropdownRef.current.contains(event.target as Node) &&
108
+ dropdownSelectorRef.current &&
109
+ !dropdownSelectorRef.current.contains(event.target as Node)
110
+ ) {
111
+ setOpened(false);
112
+ }
113
+ };
114
+
115
+ // @ts-ignore
116
+ document.addEventListener("mousedown", handleClickOutside);
117
+
118
+ return () => {
119
+ // @ts-ignore
120
+ document.removeEventListener("mousedown", handleClickOutside);
121
+ };
122
+ }, []);
123
+
124
+ useEffect(() => {
125
+ if (opened) {
126
+ window.addEventListener("resize", handlePositionChanges);
127
+ window.addEventListener("scroll", handlePositionChanges, true);
128
+ updateDropdownPosition();
129
+ }
130
+
131
+ return () => {
132
+ window.removeEventListener("resize", handlePositionChanges);
133
+ window.removeEventListener("scroll", handlePositionChanges, true);
134
+ };
135
+ }, [opened, handlePositionChanges, updateDropdownPosition]);
136
+
137
+ useEffect(() => {
138
+ if (!controlRef.current) return;
139
+
140
+ const observer = new ResizeObserver(() => {
141
+ if (!controlRef.current) return;
142
+
143
+ const currentPosition = controlRef.current.getBoundingClientRect();
144
+ if (
145
+ lastPosition.current &&
146
+ (lastPosition.current.y !== currentPosition.y ||
147
+ lastPosition.current.x !== currentPosition.x ||
148
+ lastPosition.current.top !== currentPosition.top ||
149
+ lastPosition.current.left !== currentPosition.left)
150
+ ) {
151
+ setOpened(false);
152
+ }
153
+ lastPosition.current = currentPosition;
154
+ });
155
+
156
+ observer.observe(controlRef.current);
157
+
158
+ return () => {
159
+ observer.disconnect();
160
+ };
161
+ }, []);
162
+
163
+ const classNames = [styles.dropdown];
164
+
165
+ if (error) {
166
+ classNames.push(styles.error);
167
+ }
168
+
169
+ if (extraClass) {
170
+ classNames.push(extraClass);
171
+ }
172
+
173
+ if (disabled) {
174
+ classNames.push(styles.disabled);
175
+ }
176
+
177
+ const handleOpenClose = () => {
178
+ if (disabled || readonly) return;
179
+ setOpened(!opened);
180
+ };
181
+ const handleSelect = (
182
+ e: MouseEvent,
183
+ value: string | number | undefined | null | boolean,
184
+ ) => {
185
+ e.stopPropagation();
186
+ setSelected(value);
187
+ setOpened(false);
188
+ if (onChange) {
189
+ onChange(e, value);
190
+ }
191
+ };
192
+
193
+ return (
194
+ <div ref={dropdownRef} className={cx(classNames)}>
195
+ {loading && <LoadingInput />}
196
+ <div
197
+ ref={controlRef}
198
+ className={styles.control}
199
+ onClick={handleOpenClose}
200
+ >
201
+ {!selected && placeholder && (
202
+ <div className={styles.placeholder} aria-label={ariaLabel}>
203
+ {placeholder}
204
+ </div>
205
+ )}
206
+ {(selected || !placeholder) && (
207
+ <div className={styles.label} aria-label={ariaLabel}>
208
+ {selectedIcon && (
209
+ <div className={`${styles.labelIcon} labelIcon`}>
210
+ {selectedIcon}
211
+ </div>
212
+ )}
213
+ {label}
214
+ {!!selected && <div className={styles.reset}></div>}
215
+ </div>
216
+ )}
217
+ <div className={styles.chevron}>
218
+ {!opened && <CaretDown />}
219
+ {!!opened && <CaretUp />}
220
+ </div>
221
+ </div>
222
+ {opened &&
223
+ !readonly &&
224
+ !disabled &&
225
+ createPortal(
226
+ <DropdownFieldSelector
227
+ ref={dropdownSelectorRef}
228
+ style={{ ...dropdownStyles }}
229
+ >
230
+ {commentInDropdown && (
231
+ <div className={styles.message}>{commentInDropdown}</div>
232
+ )}
233
+ {!!emptyString && (
234
+ <DropdownFieldItem
235
+ label={emptyString}
236
+ value={""}
237
+ handleSelect={handleSelect}
238
+ />
239
+ )}
240
+ {options.map(
241
+ (option: string | NameValueInterface, index: number) => {
242
+ return (
243
+ <DropdownFieldItem
244
+ key={index}
245
+ handleSelect={handleSelect}
246
+ label={typeof option === "object" ? option.label : option}
247
+ value={typeof option === "object" ? option.value : option}
248
+ icon={typeof option === "object" ? option.icon : undefined}
249
+ />
250
+ );
251
+ },
252
+ )}
253
+ </DropdownFieldSelector>,
254
+ document.body,
255
+ )}
256
+
257
+ <input
258
+ name={name}
259
+ value={selected ? selected.toString() : ""}
260
+ id={id}
261
+ type={"hidden"}
262
+ required={required}
263
+ disabled={disabled}
264
+ readOnly={readonly}
265
+ />
266
+ </div>
267
+ );
268
+ };
269
+
270
+ export default DropdownField;