@dt-dds/react-segmented-control 1.0.0-beta.29

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/CHANGELOG.md ADDED
@@ -0,0 +1,314 @@
1
+ # @dt-ui/react-segmented-control
2
+
3
+ ## 1.0.0-beta.29
4
+
5
+ ### Major Changes
6
+
7
+ - refactor!: rename pkg and publish to npmjs
8
+
9
+ ### Patch Changes
10
+
11
+ - Updated dependencies
12
+ - @dt-dds/react-core@1.0.0-beta.41
13
+ - @dt-dds/react-icon@1.0.0-beta.42
14
+ - @dt-dds/react-tooltip@1.0.0-beta.49
15
+
16
+ ## 0.1.0-beta.28
17
+
18
+ ### Patch Changes
19
+
20
+ - refactor: drop icon funcional state
21
+ - Updated dependencies
22
+ - @dt-ui/react-icon@0.1.0-beta.41
23
+
24
+ ## 0.1.0-beta.27
25
+
26
+ ### Patch Changes
27
+
28
+ - refactor: theme typography values
29
+ - Updated dependencies
30
+ - @dt-ui/react-core@0.1.0-beta.40
31
+ - @dt-ui/react-icon@0.1.0-beta.40
32
+ - @dt-ui/react-tooltip@0.1.0-beta.48
33
+
34
+ ## 0.1.0-beta.26
35
+
36
+ ### Minor Changes
37
+
38
+ - feat: update shadow theme values
39
+
40
+ ### Patch Changes
41
+
42
+ - Updated dependencies
43
+ - @dt-ui/react-core@0.1.0-beta.39
44
+ - @dt-ui/react-tooltip@0.1.0-beta.47
45
+ - @dt-ui/react-icon@0.1.0-beta.39
46
+
47
+ ## 0.1.0-beta.25
48
+
49
+ ### Minor Changes
50
+
51
+ - feat: add new icon font sizes
52
+
53
+ ### Patch Changes
54
+
55
+ - Updated dependencies
56
+ - @dt-ui/react-icon@0.1.0-beta.38
57
+
58
+ ## 0.1.0-beta.24
59
+
60
+ ### Patch Changes
61
+
62
+ - chore: update theme font-styles
63
+ - Updated dependencies
64
+ - @dt-ui/react-core@0.1.0-beta.38
65
+ - @dt-ui/react-icon@0.1.0-beta.37
66
+ - @dt-ui/react-tooltip@0.1.0-beta.46
67
+
68
+ ## 0.1.0-beta.23
69
+
70
+ ### Patch Changes
71
+
72
+ - chore: review OSS files
73
+ - Updated dependencies
74
+ - @dt-ui/react-core@0.1.0-beta.37
75
+ - @dt-ui/react-icon@0.1.0-beta.36
76
+ - @dt-ui/react-tooltip@0.1.0-beta.45
77
+
78
+ ## 0.1.0-beta.22
79
+
80
+ ### Patch Changes
81
+
82
+ - chore: update contextual colors
83
+ - Updated dependencies
84
+ - @dt-ui/react-core@0.1.0-beta.36
85
+ - @dt-ui/react-icon@0.1.0-beta.35
86
+ - @dt-ui/react-tooltip@0.1.0-beta.44
87
+
88
+ ## 0.1.0-beta.21
89
+
90
+ ### Minor Changes
91
+
92
+ - feat: implement IconButton contrast and size
93
+
94
+ ### Patch Changes
95
+
96
+ - Updated dependencies
97
+ - @dt-ui/react-icon@0.1.0-beta.34
98
+ - @dt-ui/react-tooltip@0.1.0-beta.43
99
+
100
+ ## 0.1.0-beta.20
101
+
102
+ ### Patch Changes
103
+
104
+ - fix: circular dependencies
105
+ - Updated dependencies
106
+ - @dt-ui/react-tooltip@0.1.0-beta.42
107
+
108
+ ## 0.1.0-beta.19
109
+
110
+ ### Patch Changes
111
+
112
+ - fix: change content.light contextual color
113
+ - Updated dependencies
114
+ - @dt-ui/react-core@0.1.0-beta.35
115
+ - @dt-ui/react-icon@0.1.0-beta.33
116
+ - @dt-ui/react-tooltip@0.1.0-beta.41
117
+
118
+ ## 0.1.0-beta.18
119
+
120
+ ### Minor Changes
121
+
122
+ - feat: move icon-button to independent
123
+
124
+ ### Patch Changes
125
+
126
+ - Updated dependencies
127
+ - @dt-ui/react-icon@0.1.0-beta.32
128
+ - @dt-ui/react-tooltip@0.1.0-beta.40
129
+
130
+ ## 0.1.0-beta.17
131
+
132
+ ### Patch Changes
133
+
134
+ - fix: add accent to colors type
135
+ - Updated dependencies
136
+ - @dt-ui/react-core@0.1.0-beta.34
137
+ - @dt-ui/react-icon@0.1.0-beta.31
138
+ - @dt-ui/react-tooltip@0.1.0-beta.39
139
+
140
+ ## 0.1.0-beta.16
141
+
142
+ ### Patch Changes
143
+
144
+ - fix: change button text according to ux
145
+ - Updated dependencies
146
+ - @dt-ui/react-core@0.1.0-beta.33
147
+ - @dt-ui/react-icon@0.1.0-beta.30
148
+ - @dt-ui/react-tooltip@0.1.0-beta.38
149
+
150
+ ## 0.1.0-beta.15
151
+
152
+ ### Patch Changes
153
+
154
+ - chore: update contexual colors
155
+ - Updated dependencies
156
+ - @dt-ui/react-core@0.1.0-beta.32
157
+ - @dt-ui/react-icon@0.1.0-beta.29
158
+ - @dt-ui/react-tooltip@0.1.0-beta.37
159
+
160
+ ## 0.1.0-beta.14
161
+
162
+ ### Patch Changes
163
+
164
+ - style: update responsive font styles
165
+ - Updated dependencies
166
+ - @dt-ui/react-core@0.1.0-beta.31
167
+ - @dt-ui/react-icon@0.1.0-beta.28
168
+ - @dt-ui/react-tooltip@0.1.0-beta.36
169
+
170
+ ## 0.1.0-beta.13
171
+
172
+ ### Minor Changes
173
+
174
+ - feat: add spinner size 'extra-small' & update specs
175
+
176
+ ### Patch Changes
177
+
178
+ - Updated dependencies
179
+ - @dt-ui/react-core@0.1.0-beta.30
180
+ - @dt-ui/react-icon@0.1.0-beta.27
181
+ - @dt-ui/react-tooltip@0.1.0-beta.35
182
+
183
+ ## 0.1.0-beta.12
184
+
185
+ ### Patch Changes
186
+
187
+ - chore: add theme breakpoints
188
+ - Updated dependencies
189
+ - @dt-ui/react-core@0.1.0-beta.29
190
+ - @dt-ui/react-icon@0.1.0-beta.26
191
+ - @dt-ui/react-tooltip@0.1.0-beta.34
192
+
193
+ ## 0.1.0-beta.11
194
+
195
+ ### Patch Changes
196
+
197
+ - fix(DOMRect API): prevent breaking consumers CI/CD pipeline
198
+ - Updated dependencies
199
+ - @dt-ui/react-tooltip@0.1.0-beta.33
200
+
201
+ ## 0.1.0-beta.10
202
+
203
+ ### Patch Changes
204
+
205
+ - fix(tooltip): avoid browser window crop
206
+ - Updated dependencies
207
+ - @dt-ui/react-tooltip@0.1.0-beta.32
208
+
209
+ ## 0.1.0-beta.9
210
+
211
+ ### Minor Changes
212
+
213
+ - feat: add id to base props
214
+
215
+ ### Patch Changes
216
+
217
+ - Updated dependencies
218
+ - @dt-ui/react-core@0.1.0-beta.28
219
+ - @dt-ui/react-icon@0.1.0-beta.25
220
+ - @dt-ui/react-tooltip@0.1.0-beta.31
221
+
222
+ ## 0.1.0-beta.8
223
+
224
+ ### Minor Changes
225
+
226
+ - feat: change breakpoints values
227
+
228
+ ### Patch Changes
229
+
230
+ - Updated dependencies
231
+ - @dt-ui/react-core@0.1.0-beta.27
232
+ - @dt-ui/react-icon@0.1.0-beta.24
233
+ - @dt-ui/react-tooltip@0.1.0-beta.30
234
+
235
+ ## 0.1.0-beta.7
236
+
237
+ ### Patch Changes
238
+
239
+ - fix: replace body to correct one
240
+ - Updated dependencies
241
+ - @dt-ui/react-core@0.1.0-beta.26
242
+ - @dt-ui/react-icon@0.1.0-beta.23
243
+ - @dt-ui/react-tooltip@0.1.0-beta.29
244
+
245
+ ## 0.1.0-beta.6
246
+
247
+ ### Patch Changes
248
+
249
+ - chore: add theme colors missing types
250
+ - Updated dependencies
251
+ - @dt-ui/react-core@0.1.0-beta.25
252
+ - @dt-ui/react-icon@0.1.0-beta.22
253
+ - @dt-ui/react-tooltip@0.1.0-beta.28
254
+
255
+ ## 0.1.0-beta.5
256
+
257
+ ### Patch Changes
258
+
259
+ - refactor: drop truckapi theme
260
+ - Updated dependencies
261
+ - @dt-ui/react-core@0.1.0-beta.24
262
+ - @dt-ui/react-icon@0.1.0-beta.21
263
+ - @dt-ui/react-tooltip@0.1.0-beta.27
264
+
265
+ ## 0.1.0-beta.4
266
+
267
+ ### Minor Changes
268
+
269
+ - feat: update core and semantic colors
270
+ - feat: remove greenlane theme
271
+
272
+ ### Patch Changes
273
+
274
+ - Updated dependencies
275
+ - Updated dependencies
276
+ - @dt-ui/react-core@0.1.0-beta.23
277
+ - @dt-ui/react-icon@0.1.0-beta.20
278
+ - @dt-ui/react-tooltip@0.1.0-beta.26
279
+
280
+ ## 0.1.0-beta.3
281
+
282
+ ### Minor Changes
283
+
284
+ - feat: add icon option for truckapi theme
285
+
286
+ ### Patch Changes
287
+
288
+ - Updated dependencies
289
+ - @dt-ui/react-core@0.1.0-beta.22
290
+ - @dt-ui/react-icon@0.1.0-beta.19
291
+ - @dt-ui/react-tooltip@0.1.0-beta.25
292
+
293
+ ## 0.1.0-beta.2
294
+
295
+ ### Patch Changes
296
+
297
+ - fix: tooltip z index
298
+ - Updated dependencies
299
+ - @dt-ui/react-core@0.1.0-beta.21
300
+ - @dt-ui/react-tooltip@0.1.0-beta.24
301
+ - @dt-ui/react-icon@0.1.0-beta.18
302
+
303
+ ## 0.1.0-beta.1
304
+
305
+ ### Minor Changes
306
+
307
+ - feat: add segmented control component
308
+
309
+ ### Patch Changes
310
+
311
+ - Updated dependencies
312
+ - @dt-ui/react-core@0.1.0-beta.20
313
+ - @dt-ui/react-icon@0.1.0-beta.17
314
+ - @dt-ui/react-tooltip@0.1.0-beta.23
package/LICENSE.md ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Daimler Truck AG.
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,90 @@
1
+ # SegmentedControl Package
2
+
3
+ Segmented Control is frequently used to let users toggle between different formatting or to filter, like a grid view and a table view.
4
+
5
+ ## SegmentedControl Usage
6
+
7
+ ```jsx
8
+ import { SegmentedControl } from '@dt-dds/react-segmented-control';
9
+
10
+ export const App = () => {
11
+ return (
12
+ <SegmentedControl
13
+ selectedValue='list'
14
+ showLabel={false}
15
+ onChangeControl={handleChanges}
16
+ >
17
+ <SegmentedControl.Option label='List' value='list' iconCode='list' />
18
+ <SegmentedControl.Option
19
+ label='Gallery'
20
+ value='gallery'
21
+ iconCode='grid_view'
22
+ disabled
23
+ />
24
+ </SegmentedControl>
25
+ );
26
+ };
27
+ ```
28
+
29
+ ## SegmentedControl Properties
30
+
31
+ | Property | Type | Default | Description |
32
+ | ----------------- | ---------- | ------- | ------------------------------------------------------------ |
33
+ | `selectedValue` | `string` | - | sets the selected value, useful to fulfill the initial value |
34
+ | `showLabel` | `boolean` | false | makes the labels visible or not for all options |
35
+ | `onChangeControl` | `function` | - | callback function to receive the control value changed |
36
+ | `dataTestId` | `string` | - | sets the id for testing purposes |
37
+
38
+ ## SegmentedControl.Option Properties
39
+
40
+ | Property | Type | Default | Description |
41
+ | ---------- | --------- | ------- | ----------------------------------------------------------------------------- |
42
+ | `label` | `string` | - | sets the option label, visible when showLabel is true or the icon is missing |
43
+ | `value` | `string` | false | value to control the option |
44
+ | `iconCode` | `Code` | - | sets the code for the icon to be used, check the package from `@dt-dds/icons` |
45
+ | `disabled` | `boolean` | false | disables the option |
46
+
47
+ ## Stack
48
+
49
+ - [TypeScript](https://www.typescriptlang.org/) for static type checking
50
+ - [React](https://reactjs.org/) — JavaScript library for user interfaces
51
+ - [Emotion](https://emotion.sh/docs/introduction) — for writing css styles with JavaScript
52
+ - [Storybook](https://storybook.js.org/) — UI component environment powered by Vite
53
+ - [Jest](https://jestjs.io/) - JavaScript Testing Framework
54
+ - [React Testing Library](https://testing-library.com/) - to test UI components in a user-centric way
55
+ - [ESLint](https://eslint.org/) for code linting
56
+ - [Prettier](https://prettier.io) for code formatting
57
+ - [Tsup](https://github.com/egoist/tsup) — TypeScript bundler powered by esbuild
58
+ - [Yarn](https://yarnpkg.com/) from managing packages
59
+
60
+ ## Commands
61
+
62
+ - `yarn build` - Build the package
63
+ - `yarn dev` - Run the package locally
64
+ - `yarn lint` - Lint all files within this package
65
+ - `yarn test` - Run all unit tests
66
+ - `yarn test:report` - Open the test coverage report
67
+ - `yarn test:update:snapshot` - Run all unit tests and update the snapshot
68
+
69
+ ## Compilation
70
+
71
+ Running `yarn build` from the root of the package will use [tsup](https://tsup.egoist.dev/) to compile the raw TypeScript and React code to plain JavaScript.
72
+
73
+ The `/dist` folder contains the compiled output.
74
+
75
+ ```bash
76
+ segmented-control
77
+ └── dist
78
+ ├── index.d.ts <-- Types
79
+ ├── index.js <-- CommonJS version
80
+ └── index.mjs <-- ES Modules version
81
+ ...
82
+ ```
83
+
84
+ ## Versioning
85
+
86
+ Follows [semantic versioning](https://semver.org/)
87
+
88
+ ## &copy; License
89
+
90
+ Licensed under [MIT License](LICENSE.md)
@@ -0,0 +1,28 @@
1
+ import { BaseProps, Theme } from '@dt-dds/react-core';
2
+ import { Code } from '@dt-dds/icons';
3
+ import * as react_jsx_runtime from 'react/jsx-runtime';
4
+ import { ComponentPropsWithoutRef } from 'react';
5
+
6
+ interface SegmentedControlProps$1 extends BaseProps {
7
+ label: string;
8
+ value: string;
9
+ iconCode?: Code;
10
+ disabled?: boolean;
11
+ }
12
+
13
+ interface SegmentedControlProps extends ComponentPropsWithoutRef<'div'>, BaseProps {
14
+ selectedValue?: string | undefined;
15
+ showLabel?: boolean;
16
+ onChangeControl?: (value: string) => void;
17
+ }
18
+ declare const SegmentedControl: {
19
+ ({ dataTestId, children, showLabel, selectedValue, onChangeControl, ...props }: SegmentedControlProps): react_jsx_runtime.JSX.Element;
20
+ Option: ({ label, iconCode, value, disabled, }: SegmentedControlProps$1) => react_jsx_runtime.JSX.Element;
21
+ };
22
+
23
+ declare module '@emotion/react' {
24
+ interface Theme extends Theme {
25
+ }
26
+ }
27
+
28
+ export { SegmentedControl };
package/dist/index.js ADDED
@@ -0,0 +1,237 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __defProps = Object.defineProperties;
5
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
6
+ var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
7
+ var __getOwnPropNames = Object.getOwnPropertyNames;
8
+ var __getOwnPropSymbols = Object.getOwnPropertySymbols;
9
+ var __getProtoOf = Object.getPrototypeOf;
10
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
11
+ var __propIsEnum = Object.prototype.propertyIsEnumerable;
12
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
13
+ var __spreadValues = (a, b) => {
14
+ for (var prop in b || (b = {}))
15
+ if (__hasOwnProp.call(b, prop))
16
+ __defNormalProp(a, prop, b[prop]);
17
+ if (__getOwnPropSymbols)
18
+ for (var prop of __getOwnPropSymbols(b)) {
19
+ if (__propIsEnum.call(b, prop))
20
+ __defNormalProp(a, prop, b[prop]);
21
+ }
22
+ return a;
23
+ };
24
+ var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
25
+ var __objRest = (source, exclude) => {
26
+ var target = {};
27
+ for (var prop in source)
28
+ if (__hasOwnProp.call(source, prop) && exclude.indexOf(prop) < 0)
29
+ target[prop] = source[prop];
30
+ if (source != null && __getOwnPropSymbols)
31
+ for (var prop of __getOwnPropSymbols(source)) {
32
+ if (exclude.indexOf(prop) < 0 && __propIsEnum.call(source, prop))
33
+ target[prop] = source[prop];
34
+ }
35
+ return target;
36
+ };
37
+ var __export = (target, all) => {
38
+ for (var name in all)
39
+ __defProp(target, name, { get: all[name], enumerable: true });
40
+ };
41
+ var __copyProps = (to, from, except, desc) => {
42
+ if (from && typeof from === "object" || typeof from === "function") {
43
+ for (let key of __getOwnPropNames(from))
44
+ if (!__hasOwnProp.call(to, key) && key !== except)
45
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
46
+ }
47
+ return to;
48
+ };
49
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
50
+ // If the importer is in node compatibility mode or this is not an ESM
51
+ // file that has been converted to a CommonJS file using a Babel-
52
+ // compatible transform (i.e. "__esModule" has not been set), then set
53
+ // "default" to the CommonJS "module.exports" for node compatibility.
54
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
55
+ mod
56
+ ));
57
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
58
+
59
+ // index.ts
60
+ var segmented_control_exports = {};
61
+ __export(segmented_control_exports, {
62
+ SegmentedControl: () => SegmentedControl
63
+ });
64
+ module.exports = __toCommonJS(segmented_control_exports);
65
+
66
+ // src/components/SegmentedControlOption.tsx
67
+ var import_react_icon = require("@dt-dds/react-icon");
68
+ var import_react_tooltip = require("@dt-dds/react-tooltip");
69
+
70
+ // src/context/SegmentedControlProvider.tsx
71
+ var import_react = require("react");
72
+ var import_jsx_runtime = require("react/jsx-runtime");
73
+ var DEFAULT_VALUE = {
74
+ selectedValue: void 0,
75
+ showLabel: true,
76
+ onChange: () => null,
77
+ setSelectedValue: () => null
78
+ };
79
+ var SegmentedControlContext = (0, import_react.createContext)(DEFAULT_VALUE);
80
+ var useSegmentedControlContext = () => {
81
+ const context = (0, import_react.useContext)(SegmentedControlContext);
82
+ if (!context) {
83
+ throw new Error(
84
+ "useSegmentedControlContext must be used within a SegmentedControlProvider"
85
+ );
86
+ }
87
+ return context;
88
+ };
89
+ var SegmentedControlProvider = ({
90
+ children,
91
+ selectedValue,
92
+ showLabel,
93
+ onChange
94
+ }) => {
95
+ const [value, setValue] = (0, import_react.useState)(selectedValue);
96
+ (0, import_react.useEffect)(() => {
97
+ setValue(selectedValue);
98
+ }, [selectedValue]);
99
+ const handleChangeValue = (value2) => {
100
+ onChange(value2);
101
+ setValue(value2);
102
+ };
103
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
104
+ SegmentedControlContext.Provider,
105
+ {
106
+ value: {
107
+ selectedValue: value,
108
+ setSelectedValue: handleChangeValue,
109
+ showLabel,
110
+ onChange
111
+ },
112
+ children
113
+ }
114
+ );
115
+ };
116
+
117
+ // src/components/SegmentedControlOption.styled.ts
118
+ var import_styled = __toESM(require("@emotion/styled"));
119
+ var SegmentedControlOptionStyled = import_styled.default.button`
120
+ ${({ theme, showLabel }) => `
121
+ ${theme.fontStyles.body2};
122
+
123
+ display: flex;
124
+ align-items: center;
125
+ gap: ${theme.spacing["5xs"]};
126
+ cursor: pointer;
127
+ background-color: ${theme.palette.surface.contrast};
128
+ color: ${theme.palette.content.default};
129
+ border: none;
130
+ border-radius: ${theme.shape.button};
131
+ padding: ${theme.spacing["5xs"]} ${theme.spacing[showLabel ? "3xs" : "5xs"]};
132
+
133
+ &:not(:disabled):hover {
134
+ background-color: ${theme.palette.primary.light};
135
+ color: ${theme.palette.content.default};
136
+
137
+ i {
138
+ color: ${theme.palette.content.dark};
139
+ }
140
+ }
141
+
142
+ &:not(:disabled)[aria-pressed="true"] {
143
+ background-color: ${theme.palette.primary.default};
144
+ color: ${theme.palette.content.contrast};
145
+
146
+ i {
147
+ color: ${theme.palette.content.contrast};
148
+ }
149
+ }
150
+
151
+ &:disabled {
152
+ color: ${theme.palette.content.light};
153
+ cursor: not-allowed;
154
+
155
+ i {
156
+ color: ${theme.palette.content.light};
157
+ }
158
+ }
159
+
160
+ `}
161
+ `;
162
+
163
+ // src/components/SegmentedControlOption.tsx
164
+ var import_jsx_runtime2 = require("react/jsx-runtime");
165
+ var SegmentedControlOption = ({
166
+ label,
167
+ iconCode,
168
+ value,
169
+ disabled
170
+ }) => {
171
+ const { setSelectedValue, showLabel, selectedValue } = useSegmentedControlContext();
172
+ const isSelected = selectedValue === value;
173
+ const isLabelVisible = showLabel || !iconCode;
174
+ const renderOption = /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)(
175
+ SegmentedControlOptionStyled,
176
+ {
177
+ "aria-label": label,
178
+ "aria-pressed": isSelected ? "true" : "false",
179
+ disabled,
180
+ onClick: () => setSelectedValue(value),
181
+ showLabel: isLabelVisible,
182
+ type: "button",
183
+ children: [
184
+ iconCode ? /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(import_react_icon.Icon, { code: iconCode, size: "s" }) : null,
185
+ isLabelVisible ? label : null
186
+ ]
187
+ }
188
+ );
189
+ return isLabelVisible ? renderOption : /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)(import_react_tooltip.Tooltip, { children: [
190
+ renderOption,
191
+ /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(import_react_tooltip.Tooltip.Content, { children: label })
192
+ ] });
193
+ };
194
+
195
+ // src/SegmentedControl.styled.ts
196
+ var import_styled2 = __toESM(require("@emotion/styled"));
197
+ var SegmentedControlStyled = import_styled2.default.div`
198
+ ${({ theme }) => `
199
+ display: inline-flex;
200
+ align-items: center;
201
+ padding: ${theme.spacing["5xs"]};
202
+ border: 1px solid ${theme.palette.border.default};
203
+ border-radius: ${theme.shape.segmentedControl};
204
+ `}
205
+ `;
206
+
207
+ // src/SegmentedControl.tsx
208
+ var import_jsx_runtime3 = require("react/jsx-runtime");
209
+ var SegmentedControl = (_a) => {
210
+ var _b = _a, {
211
+ dataTestId,
212
+ children,
213
+ showLabel = false,
214
+ selectedValue,
215
+ onChangeControl = () => null
216
+ } = _b, props = __objRest(_b, [
217
+ "dataTestId",
218
+ "children",
219
+ "showLabel",
220
+ "selectedValue",
221
+ "onChangeControl"
222
+ ]);
223
+ return /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(
224
+ SegmentedControlProvider,
225
+ {
226
+ onChange: onChangeControl,
227
+ selectedValue,
228
+ showLabel,
229
+ children: /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(SegmentedControlStyled, __spreadProps(__spreadValues({ "data-testid": dataTestId, role: "group" }, props), { children }))
230
+ }
231
+ );
232
+ };
233
+ SegmentedControl.Option = SegmentedControlOption;
234
+ // Annotate the CommonJS export names for ESM import in node:
235
+ 0 && (module.exports = {
236
+ SegmentedControl
237
+ });
package/dist/index.mjs ADDED
@@ -0,0 +1,208 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __defProps = Object.defineProperties;
3
+ var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
4
+ var __getOwnPropSymbols = Object.getOwnPropertySymbols;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __propIsEnum = Object.prototype.propertyIsEnumerable;
7
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
8
+ var __spreadValues = (a, b) => {
9
+ for (var prop in b || (b = {}))
10
+ if (__hasOwnProp.call(b, prop))
11
+ __defNormalProp(a, prop, b[prop]);
12
+ if (__getOwnPropSymbols)
13
+ for (var prop of __getOwnPropSymbols(b)) {
14
+ if (__propIsEnum.call(b, prop))
15
+ __defNormalProp(a, prop, b[prop]);
16
+ }
17
+ return a;
18
+ };
19
+ var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
20
+ var __objRest = (source, exclude) => {
21
+ var target = {};
22
+ for (var prop in source)
23
+ if (__hasOwnProp.call(source, prop) && exclude.indexOf(prop) < 0)
24
+ target[prop] = source[prop];
25
+ if (source != null && __getOwnPropSymbols)
26
+ for (var prop of __getOwnPropSymbols(source)) {
27
+ if (exclude.indexOf(prop) < 0 && __propIsEnum.call(source, prop))
28
+ target[prop] = source[prop];
29
+ }
30
+ return target;
31
+ };
32
+
33
+ // src/components/SegmentedControlOption.tsx
34
+ import { Icon } from "@dt-dds/react-icon";
35
+ import { Tooltip } from "@dt-dds/react-tooltip";
36
+
37
+ // src/context/SegmentedControlProvider.tsx
38
+ import {
39
+ createContext,
40
+ useContext,
41
+ useState,
42
+ useEffect
43
+ } from "react";
44
+ import { jsx } from "react/jsx-runtime";
45
+ var DEFAULT_VALUE = {
46
+ selectedValue: void 0,
47
+ showLabel: true,
48
+ onChange: () => null,
49
+ setSelectedValue: () => null
50
+ };
51
+ var SegmentedControlContext = createContext(DEFAULT_VALUE);
52
+ var useSegmentedControlContext = () => {
53
+ const context = useContext(SegmentedControlContext);
54
+ if (!context) {
55
+ throw new Error(
56
+ "useSegmentedControlContext must be used within a SegmentedControlProvider"
57
+ );
58
+ }
59
+ return context;
60
+ };
61
+ var SegmentedControlProvider = ({
62
+ children,
63
+ selectedValue,
64
+ showLabel,
65
+ onChange
66
+ }) => {
67
+ const [value, setValue] = useState(selectedValue);
68
+ useEffect(() => {
69
+ setValue(selectedValue);
70
+ }, [selectedValue]);
71
+ const handleChangeValue = (value2) => {
72
+ onChange(value2);
73
+ setValue(value2);
74
+ };
75
+ return /* @__PURE__ */ jsx(
76
+ SegmentedControlContext.Provider,
77
+ {
78
+ value: {
79
+ selectedValue: value,
80
+ setSelectedValue: handleChangeValue,
81
+ showLabel,
82
+ onChange
83
+ },
84
+ children
85
+ }
86
+ );
87
+ };
88
+
89
+ // src/components/SegmentedControlOption.styled.ts
90
+ import styled from "@emotion/styled";
91
+ var SegmentedControlOptionStyled = styled.button`
92
+ ${({ theme, showLabel }) => `
93
+ ${theme.fontStyles.body2};
94
+
95
+ display: flex;
96
+ align-items: center;
97
+ gap: ${theme.spacing["5xs"]};
98
+ cursor: pointer;
99
+ background-color: ${theme.palette.surface.contrast};
100
+ color: ${theme.palette.content.default};
101
+ border: none;
102
+ border-radius: ${theme.shape.button};
103
+ padding: ${theme.spacing["5xs"]} ${theme.spacing[showLabel ? "3xs" : "5xs"]};
104
+
105
+ &:not(:disabled):hover {
106
+ background-color: ${theme.palette.primary.light};
107
+ color: ${theme.palette.content.default};
108
+
109
+ i {
110
+ color: ${theme.palette.content.dark};
111
+ }
112
+ }
113
+
114
+ &:not(:disabled)[aria-pressed="true"] {
115
+ background-color: ${theme.palette.primary.default};
116
+ color: ${theme.palette.content.contrast};
117
+
118
+ i {
119
+ color: ${theme.palette.content.contrast};
120
+ }
121
+ }
122
+
123
+ &:disabled {
124
+ color: ${theme.palette.content.light};
125
+ cursor: not-allowed;
126
+
127
+ i {
128
+ color: ${theme.palette.content.light};
129
+ }
130
+ }
131
+
132
+ `}
133
+ `;
134
+
135
+ // src/components/SegmentedControlOption.tsx
136
+ import { jsx as jsx2, jsxs } from "react/jsx-runtime";
137
+ var SegmentedControlOption = ({
138
+ label,
139
+ iconCode,
140
+ value,
141
+ disabled
142
+ }) => {
143
+ const { setSelectedValue, showLabel, selectedValue } = useSegmentedControlContext();
144
+ const isSelected = selectedValue === value;
145
+ const isLabelVisible = showLabel || !iconCode;
146
+ const renderOption = /* @__PURE__ */ jsxs(
147
+ SegmentedControlOptionStyled,
148
+ {
149
+ "aria-label": label,
150
+ "aria-pressed": isSelected ? "true" : "false",
151
+ disabled,
152
+ onClick: () => setSelectedValue(value),
153
+ showLabel: isLabelVisible,
154
+ type: "button",
155
+ children: [
156
+ iconCode ? /* @__PURE__ */ jsx2(Icon, { code: iconCode, size: "s" }) : null,
157
+ isLabelVisible ? label : null
158
+ ]
159
+ }
160
+ );
161
+ return isLabelVisible ? renderOption : /* @__PURE__ */ jsxs(Tooltip, { children: [
162
+ renderOption,
163
+ /* @__PURE__ */ jsx2(Tooltip.Content, { children: label })
164
+ ] });
165
+ };
166
+
167
+ // src/SegmentedControl.styled.ts
168
+ import styled2 from "@emotion/styled";
169
+ var SegmentedControlStyled = styled2.div`
170
+ ${({ theme }) => `
171
+ display: inline-flex;
172
+ align-items: center;
173
+ padding: ${theme.spacing["5xs"]};
174
+ border: 1px solid ${theme.palette.border.default};
175
+ border-radius: ${theme.shape.segmentedControl};
176
+ `}
177
+ `;
178
+
179
+ // src/SegmentedControl.tsx
180
+ import { jsx as jsx3 } from "react/jsx-runtime";
181
+ var SegmentedControl = (_a) => {
182
+ var _b = _a, {
183
+ dataTestId,
184
+ children,
185
+ showLabel = false,
186
+ selectedValue,
187
+ onChangeControl = () => null
188
+ } = _b, props = __objRest(_b, [
189
+ "dataTestId",
190
+ "children",
191
+ "showLabel",
192
+ "selectedValue",
193
+ "onChangeControl"
194
+ ]);
195
+ return /* @__PURE__ */ jsx3(
196
+ SegmentedControlProvider,
197
+ {
198
+ onChange: onChangeControl,
199
+ selectedValue,
200
+ showLabel,
201
+ children: /* @__PURE__ */ jsx3(SegmentedControlStyled, __spreadProps(__spreadValues({ "data-testid": dataTestId, role: "group" }, props), { children }))
202
+ }
203
+ );
204
+ };
205
+ SegmentedControl.Option = SegmentedControlOption;
206
+ export {
207
+ SegmentedControl
208
+ };
package/package.json ADDED
@@ -0,0 +1,56 @@
1
+ {
2
+ "name": "@dt-dds/react-segmented-control",
3
+ "version": "1.0.0-beta.29",
4
+ "license": "MIT",
5
+ "exports": {
6
+ ".": "./dist/index.js"
7
+ },
8
+ "main": "./dist/index.js",
9
+ "module": "./dist/index.mjs",
10
+ "types": "./dist/index.d.ts",
11
+ "files": [
12
+ "dist/**"
13
+ ],
14
+ "scripts": {
15
+ "build": "tsup",
16
+ "dev": "tsup --watch",
17
+ "lint": "eslint --cache .",
18
+ "test": "jest",
19
+ "test:report": "open ./jest-coverage/lcov-report/index.html",
20
+ "test:update:snapshot": "jest -u"
21
+ },
22
+ "dependencies": {
23
+ "@dt-dds/react-core": "1.0.0-beta.41",
24
+ "@dt-dds/react-icon": "1.0.0-beta.42",
25
+ "@dt-dds/react-tooltip": "1.0.0-beta.49"
26
+ },
27
+ "devDependencies": {
28
+ "@babel/core": "^7.22.9",
29
+ "@babel/preset-env": "^7.22.9",
30
+ "@babel/preset-react": "^7.22.5",
31
+ "@babel/preset-typescript": "^7.23.3",
32
+ "@emotion/babel-plugin": "^11.11.0",
33
+ "@emotion/css": "^11.7.1",
34
+ "@emotion/jest": "^11.10.0",
35
+ "@emotion/react": "^11.8.2",
36
+ "@emotion/styled": "^11.8.1",
37
+ "@types/react": "^18.0.9",
38
+ "@types/react-dom": "^18.0.4",
39
+ "babel-loader": "^8.3.0",
40
+ "eslint-config-custom": "*",
41
+ "eslint-plugin-storybook": "^9.1.0",
42
+ "jest-config": "*",
43
+ "react": "^18.1.0",
44
+ "react-dom": "^18.2.0",
45
+ "tsconfig": "*",
46
+ "tsup": "^6.6.3",
47
+ "typescript": "^4.5.3"
48
+ },
49
+ "peerDependencies": {
50
+ "@emotion/css": "^11.7.1",
51
+ "@emotion/react": "^11.8.2",
52
+ "@emotion/styled": "^11.8.1",
53
+ "react": ">=17.0.2",
54
+ "react-dom": ">=17.0.2"
55
+ }
56
+ }