@dt-dds/react-dropdown 1.0.0-beta.31

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/dist/index.js ADDED
@@ -0,0 +1,453 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+
30
+ // index.ts
31
+ var dropdown_exports = {};
32
+ __export(dropdown_exports, {
33
+ Dropdown: () => Dropdown_default,
34
+ DropdownContainer: () => DropdownContainer,
35
+ DropdownContext: () => DropdownContext,
36
+ DropdownContextProvider: () => DropdownContextProvider,
37
+ DropdownDetail: () => DropdownDetail,
38
+ DropdownMenu: () => DropdownMenu,
39
+ DropdownOption: () => DropdownOption,
40
+ DropdownSelect: () => DropdownSelect,
41
+ useDropdownContext: () => useDropdownContext
42
+ });
43
+ module.exports = __toCommonJS(dropdown_exports);
44
+
45
+ // src/components/detail/DropdownDetail.tsx
46
+ var import_react_typography = require("@dt-dds/react-typography");
47
+
48
+ // src/components/detail/DropdownDetail.styled.ts
49
+ var import_styled = __toESM(require("@emotion/styled"));
50
+ var DropdownDetailStyled = import_styled.default.div`
51
+ display: flex;
52
+ align-items: center;
53
+ padding-left: 15px;
54
+ margin-top: 4px;
55
+ `;
56
+
57
+ // src/components/detail/DropdownDetail.tsx
58
+ var import_jsx_runtime = require("react/jsx-runtime");
59
+ var DropdownDetail = ({
60
+ dataTestId,
61
+ isDisabled = false,
62
+ hasError = false,
63
+ children = ""
64
+ }) => {
65
+ const messageColor = isDisabled ? "content.light" : "content.medium";
66
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(DropdownDetailStyled, { "data-testid": dataTestId != null ? dataTestId : "dropdown-text", children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
67
+ import_react_typography.Typography,
68
+ {
69
+ color: hasError ? "error.default" : messageColor,
70
+ element: "span",
71
+ fontStyles: "body3",
72
+ children
73
+ }
74
+ ) });
75
+ };
76
+
77
+ // src/context/DropdownProvider.tsx
78
+ var import_react = require("react");
79
+ var import_jsx_runtime2 = require("react/jsx-runtime");
80
+ var DEFAULT_VALUE = {
81
+ state: {
82
+ text: "",
83
+ value: ""
84
+ },
85
+ isOpen: false,
86
+ setState: () => null,
87
+ setIsOpen: () => null
88
+ };
89
+ var DropdownContext = (0, import_react.createContext)(DEFAULT_VALUE);
90
+ var DropdownContextProvider = ({
91
+ children,
92
+ defaultValue,
93
+ name
94
+ }) => {
95
+ const [state, setState] = (0, import_react.useState)(defaultValue != null ? defaultValue : DEFAULT_VALUE.state);
96
+ const [isOpen, setIsOpen] = (0, import_react.useState)(DEFAULT_VALUE.isOpen);
97
+ (0, import_react.useEffect)(() => defaultValue && setState(defaultValue), [defaultValue]);
98
+ return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
99
+ DropdownContext.Provider,
100
+ {
101
+ value: {
102
+ state,
103
+ setState,
104
+ isOpen,
105
+ setIsOpen,
106
+ name
107
+ },
108
+ children
109
+ }
110
+ );
111
+ };
112
+ var useDropdownContext = () => {
113
+ const context = (0, import_react.useContext)(DropdownContext);
114
+ if (!context) {
115
+ throw new Error(
116
+ "Dropdown compound components cannot be rendered outside the Dropdown component"
117
+ );
118
+ }
119
+ return context;
120
+ };
121
+
122
+ // src/components/option/DropdownOption.styled.ts
123
+ var import_styled2 = __toESM(require("@emotion/styled"));
124
+ var DropdownOptionStyled = import_styled2.default.li`
125
+ ${({ theme, disabled, isSelected }) => `
126
+ ${theme.fontStyles[isSelected ? "body2Bold" : "body2"]};
127
+ color: ${theme.palette.content.default};
128
+ list-style: none;
129
+ padding: ${theme.spacing["4xs"]} ${theme.spacing["2xs"]};
130
+ text-overflow: ellipsis;
131
+ overflow-x: hidden;
132
+
133
+ &:hover {
134
+ background: ${disabled ? theme.palette.surface.light : theme.palette.primary.light};
135
+ cursor: ${disabled ? "not-allowed" : "pointer"};
136
+ }
137
+
138
+ ${disabled && `
139
+ color: ${theme.palette.content.light};
140
+ background: ${theme.palette.surface.light};
141
+ `}
142
+ `}
143
+ `;
144
+
145
+ // src/components/option/DropdownOption.tsx
146
+ var import_jsx_runtime3 = require("react/jsx-runtime");
147
+ var DropdownOption = ({
148
+ dataTestId,
149
+ option,
150
+ children,
151
+ style,
152
+ isDisabled,
153
+ onClick
154
+ }) => {
155
+ var _a;
156
+ const { state, setState, setIsOpen, name } = useDropdownContext();
157
+ const testId = dataTestId != null ? dataTestId : `dropdown-option-${option.value}`;
158
+ const value = {
159
+ text: (_a = option.text) != null ? _a : option.value,
160
+ value: option.value
161
+ };
162
+ const handleClick = (event) => {
163
+ if (isDisabled) {
164
+ return;
165
+ }
166
+ setIsOpen(false);
167
+ setState(value);
168
+ onClick && onClick(option.value, name, event);
169
+ };
170
+ return /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(
171
+ DropdownOptionStyled,
172
+ {
173
+ "data-testid": testId,
174
+ disabled: isDisabled,
175
+ isSelected: state.value === option.value,
176
+ onClick: handleClick,
177
+ role: "option",
178
+ style,
179
+ children
180
+ },
181
+ option.value
182
+ );
183
+ };
184
+
185
+ // src/components/select/DropdownSelect.tsx
186
+ var import_react_box = require("@dt-dds/react-box");
187
+ var import_react_icon = require("@dt-dds/react-icon");
188
+ var import_react_icon_button = require("@dt-dds/react-icon-button");
189
+ var import_react_typography2 = require("@dt-dds/react-typography");
190
+ var import_react2 = require("@emotion/react");
191
+ var import_react3 = require("react");
192
+
193
+ // src/components/menu/DropdownMenu.styled.ts
194
+ var import_react_core = require("@dt-dds/react-core");
195
+ var import_styled3 = __toESM(require("@emotion/styled"));
196
+ var DropdownMenuStyled = import_styled3.default.ul`
197
+ ${({ theme }) => `
198
+ background: ${theme.palette.surface.contrast};
199
+ border: 1px solid ${theme.palette.border.default};
200
+ border-radius: ${theme.shape.dropdown};
201
+ width: 100%;
202
+ padding:${theme.spacing["2xs"]} ${theme.spacing.none};
203
+ margin: ${theme.spacing["5xs"]} ${theme.spacing.none};
204
+ position: absolute;
205
+ right: 0;
206
+ z-index: ${import_react_core.DROPDOWN_MENU_Z_INDEX};
207
+ max-height: 180px;
208
+ overflow: auto;
209
+ `}
210
+ `;
211
+
212
+ // src/components/menu/DropdownMenu.tsx
213
+ var import_jsx_runtime4 = require("react/jsx-runtime");
214
+ var DropdownMenu = ({ children, dataTestId, style }) => {
215
+ return /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(
216
+ DropdownMenuStyled,
217
+ {
218
+ "data-testid": `${dataTestId ? dataTestId + "-" : ""}menu`,
219
+ style,
220
+ children
221
+ }
222
+ );
223
+ };
224
+
225
+ // src/components/select/DropdownSelect.styled.ts
226
+ var import_styled4 = __toESM(require("@emotion/styled"));
227
+ var getThemedBackgroundFill = (fill, theme) => ({
228
+ default: theme.palette.surface.default,
229
+ contrast: theme.palette.surface.contrast,
230
+ light: theme.palette.surface.light
231
+ })[fill];
232
+ var SelectDropdownStyled = import_styled4.default.button`
233
+ ${({
234
+ theme,
235
+ hasBorder = true,
236
+ hasError = false,
237
+ isOpen = false,
238
+ variant = "outlined",
239
+ fill = "default"
240
+ }) => {
241
+ const borderColor = theme.palette.border.medium;
242
+ const activeBorderColor = theme.palette.content.dark;
243
+ const errorBorderColor = theme.palette.error.default;
244
+ return `
245
+ background: ${theme.palette.surface.contrast};
246
+ padding: ${theme.spacing.xsmall} ${hasBorder ? theme.spacing.xmedium : "0"};
247
+ position: relative;
248
+ display: flex;
249
+ align-items: center;
250
+ justify-content: space-between;
251
+ text-align: left;
252
+ width: 100%;
253
+ height: 53px;
254
+ cursor: pointer;
255
+ outline: none;
256
+
257
+ background-color: ${getThemedBackgroundFill(fill, theme)};
258
+
259
+ border-width: ${variant === "outlined" ? "1px" : "0 0 1px"};
260
+ border-color: ${isOpen ? activeBorderColor : borderColor};
261
+ border-style: solid;
262
+
263
+ &:focus, &:hover {
264
+ border-color: ${hasError ? errorBorderColor : activeBorderColor};
265
+ }
266
+
267
+ ${hasError && `border-color: ${errorBorderColor}`};
268
+ ${!hasBorder && "border: none"};
269
+
270
+ &:disabled {
271
+ pointer-events: none;
272
+ }
273
+ `;
274
+ }}
275
+ `;
276
+
277
+ // src/components/select/DropdownSelect.tsx
278
+ var import_jsx_runtime5 = require("react/jsx-runtime");
279
+ var DropdownSelect = ({
280
+ children,
281
+ label,
282
+ style,
283
+ dataTestId = "dropdown-select",
284
+ isDisabled = false,
285
+ isRequired,
286
+ hasBorder = true,
287
+ hasError = false,
288
+ hasDeselect = false,
289
+ variant = "outlined",
290
+ fill = "default"
291
+ }) => {
292
+ const { state, setState, isOpen, setIsOpen } = useDropdownContext();
293
+ const theme = (0, import_react2.useTheme)();
294
+ const childCount = import_react3.Children.count(children);
295
+ const hasOneChild = childCount === 1;
296
+ const disabled = isDisabled || hasOneChild;
297
+ const disabledIconColor = disabled ? theme.palette.content.light : theme.palette.content.default;
298
+ const handleClick = (event) => {
299
+ event.preventDefault();
300
+ setIsOpen((prev) => !prev);
301
+ };
302
+ const handleDeselectClick = (event) => {
303
+ event.stopPropagation();
304
+ setState({ text: "", value: "" });
305
+ };
306
+ (0, import_react3.useEffect)(() => {
307
+ const hasOption = import_react3.Children.toArray(children).find(
308
+ (child) => child.props.option.value === state.value
309
+ );
310
+ if (!hasOption) {
311
+ setState({ text: "", value: "" });
312
+ }
313
+ }, [children, setState, state.value]);
314
+ (0, import_react3.useEffect)(() => {
315
+ var _a;
316
+ if (hasOneChild) {
317
+ const options = import_react3.Children.map(
318
+ children,
319
+ (child) => child && child.props.option
320
+ );
321
+ if (options && options[0]) {
322
+ const option = {
323
+ text: (_a = options[0].text) != null ? _a : options[0].value,
324
+ value: options[0].value
325
+ };
326
+ setState(option);
327
+ }
328
+ }
329
+ }, [hasOneChild, setState, children]);
330
+ return /* @__PURE__ */ (0, import_jsx_runtime5.jsxs)(import_jsx_runtime5.Fragment, { children: [
331
+ /* @__PURE__ */ (0, import_jsx_runtime5.jsxs)(
332
+ SelectDropdownStyled,
333
+ {
334
+ "data-testid": dataTestId,
335
+ disabled,
336
+ fill,
337
+ hasBorder,
338
+ hasError,
339
+ isOpen,
340
+ onClick: handleClick,
341
+ style,
342
+ variant,
343
+ children: [
344
+ /* @__PURE__ */ (0, import_jsx_runtime5.jsxs)("div", { style: { overflow: "hidden" }, children: [
345
+ /* @__PURE__ */ (0, import_jsx_runtime5.jsxs)(
346
+ import_react_typography2.Typography,
347
+ {
348
+ color: disabled ? "content.light" : "content.default",
349
+ fontStyles: "body3",
350
+ children: [
351
+ label,
352
+ isRequired ? /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(
353
+ import_react_typography2.Typography,
354
+ {
355
+ color: "error.default",
356
+ element: "span",
357
+ fontStyles: "body3",
358
+ children: "*"
359
+ }
360
+ ) : null
361
+ ]
362
+ }
363
+ ),
364
+ /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(
365
+ import_react_typography2.Typography,
366
+ {
367
+ color: disabled ? "content.light" : "content.default",
368
+ fontStyles: "body2",
369
+ style: { textOverflow: "ellipsis", overflow: "hidden" },
370
+ children: !state.value ? "Select an option" : state.text
371
+ }
372
+ )
373
+ ] }),
374
+ /* @__PURE__ */ (0, import_jsx_runtime5.jsxs)(import_react_box.Box, { style: { flexDirection: "row", gap: "0.5rem" }, children: [
375
+ hasDeselect && !!state.value ? /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(import_react_icon_button.IconButton, { onClick: handleDeselectClick, children: /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(
376
+ import_react_icon.Icon,
377
+ {
378
+ code: "close",
379
+ color: disabledIconColor,
380
+ dataTestId: "deselect-value",
381
+ size: "s"
382
+ }
383
+ ) }) : null,
384
+ /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(
385
+ import_react_icon.Icon,
386
+ {
387
+ code: isOpen ? "expand_less" : "expand_more",
388
+ color: disabledIconColor,
389
+ size: "l"
390
+ }
391
+ )
392
+ ] })
393
+ ]
394
+ }
395
+ ),
396
+ isOpen ? /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(DropdownMenu, { dataTestId, children }) : null
397
+ ] });
398
+ };
399
+
400
+ // src/components/container/DropdownContainer.tsx
401
+ var import_react_core2 = require("@dt-dds/react-core");
402
+ var import_react4 = require("react");
403
+ var import_jsx_runtime6 = require("react/jsx-runtime");
404
+ var DropdownContainer = ({
405
+ children,
406
+ style,
407
+ dataTestId
408
+ }) => {
409
+ const { setIsOpen } = useDropdownContext();
410
+ const ref = (0, import_react4.useRef)(null);
411
+ (0, import_react_core2.useClickOutside)({ ref, handler: () => setIsOpen(false) });
412
+ return /* @__PURE__ */ (0, import_jsx_runtime6.jsx)("div", { "data-testid": dataTestId, ref, style, children });
413
+ };
414
+
415
+ // src/Dropdown.styled.ts
416
+ var import_styled5 = __toESM(require("@emotion/styled"));
417
+ var DropdownStyled = import_styled5.default.div`
418
+ ${({ theme, style }) => `
419
+ margin: ${theme.spacing.none};
420
+ display: inline-block;
421
+ position: relative;
422
+ width: 100%;
423
+ ${style}
424
+ `}
425
+ `;
426
+
427
+ // src/Dropdown.tsx
428
+ var import_jsx_runtime7 = require("react/jsx-runtime");
429
+ var Dropdown = ({
430
+ children,
431
+ defaultValue,
432
+ style,
433
+ name,
434
+ dataTestId = "dropdown"
435
+ }) => /* @__PURE__ */ (0, import_jsx_runtime7.jsx)(DropdownContextProvider, { defaultValue, name, children: /* @__PURE__ */ (0, import_jsx_runtime7.jsx)(DropdownStyled, { "data-testid": dataTestId, role: "menu", style, children }) });
436
+ Dropdown.Container = DropdownContainer;
437
+ Dropdown.Detail = DropdownDetail;
438
+ Dropdown.Select = DropdownSelect;
439
+ Dropdown.Option = DropdownOption;
440
+ Dropdown.Menu = DropdownMenu;
441
+ var Dropdown_default = Dropdown;
442
+ // Annotate the CommonJS export names for ESM import in node:
443
+ 0 && (module.exports = {
444
+ Dropdown,
445
+ DropdownContainer,
446
+ DropdownContext,
447
+ DropdownContextProvider,
448
+ DropdownDetail,
449
+ DropdownMenu,
450
+ DropdownOption,
451
+ DropdownSelect,
452
+ useDropdownContext
453
+ });