@dxc-technology/halstack-react 0.0.0-8710cb0 → 0.0.0-8719165

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.
Files changed (284) hide show
  1. package/BackgroundColorContext.d.ts +10 -0
  2. package/BackgroundColorContext.js +1 -4
  3. package/HalstackContext.d.ts +12 -0
  4. package/HalstackContext.js +295 -0
  5. package/accordion/Accordion.d.ts +1 -1
  6. package/accordion/Accordion.js +15 -47
  7. package/accordion/Accordion.stories.tsx +307 -0
  8. package/accordion/Accordion.test.js +72 -0
  9. package/accordion/types.d.ts +8 -8
  10. package/accordion-group/AccordionGroup.d.ts +1 -1
  11. package/accordion-group/AccordionGroup.js +16 -17
  12. package/accordion-group/AccordionGroup.stories.tsx +225 -0
  13. package/accordion-group/AccordionGroup.test.js +151 -0
  14. package/accordion-group/types.d.ts +8 -8
  15. package/alert/Alert.js +6 -3
  16. package/alert/Alert.test.js +92 -0
  17. package/alert/types.d.ts +1 -1
  18. package/badge/Badge.d.ts +4 -0
  19. package/badge/Badge.js +6 -4
  20. package/badge/types.d.ts +5 -0
  21. package/{radio → badge}/types.js +0 -0
  22. package/bleed/Bleed.d.ts +3 -0
  23. package/bleed/Bleed.js +51 -0
  24. package/bleed/Bleed.stories.tsx +341 -0
  25. package/bleed/types.d.ts +37 -0
  26. package/bleed/types.js +5 -0
  27. package/box/Box.js +24 -34
  28. package/box/Box.test.js +18 -0
  29. package/bulleted-list/BulletedList.d.ts +7 -0
  30. package/bulleted-list/BulletedList.js +123 -0
  31. package/bulleted-list/BulletedList.stories.tsx +200 -0
  32. package/bulleted-list/types.d.ts +11 -0
  33. package/bulleted-list/types.js +5 -0
  34. package/button/Button.d.ts +1 -1
  35. package/button/Button.js +62 -83
  36. package/button/Button.stories.tsx +15 -8
  37. package/button/Button.test.js +35 -0
  38. package/button/types.d.ts +10 -14
  39. package/card/Card.js +26 -30
  40. package/card/Card.stories.tsx +201 -0
  41. package/card/Card.test.js +50 -0
  42. package/card/ice-cream.jpg +0 -0
  43. package/card/types.d.ts +4 -6
  44. package/checkbox/Checkbox.d.ts +1 -1
  45. package/checkbox/Checkbox.js +45 -41
  46. package/checkbox/Checkbox.stories.tsx +124 -128
  47. package/checkbox/Checkbox.test.js +78 -0
  48. package/checkbox/types.d.ts +9 -5
  49. package/chip/Chip.d.ts +4 -0
  50. package/chip/Chip.js +16 -76
  51. package/chip/Chip.stories.tsx +6 -8
  52. package/chip/Chip.test.js +56 -0
  53. package/chip/types.d.ts +45 -0
  54. package/chip/types.js +5 -0
  55. package/common/variables.js +278 -355
  56. package/date-input/DateInput.js +66 -55
  57. package/date-input/DateInput.stories.tsx +7 -7
  58. package/date-input/DateInput.test.js +479 -0
  59. package/date-input/types.d.ts +16 -9
  60. package/dialog/Dialog.js +50 -53
  61. package/dialog/Dialog.stories.tsx +1 -2
  62. package/dialog/Dialog.test.js +70 -0
  63. package/dialog/types.d.ts +2 -2
  64. package/dropdown/Dropdown.d.ts +1 -1
  65. package/dropdown/Dropdown.js +242 -272
  66. package/dropdown/Dropdown.stories.tsx +312 -0
  67. package/dropdown/Dropdown.test.js +591 -0
  68. package/dropdown/DropdownMenu.d.ts +4 -0
  69. package/dropdown/DropdownMenu.js +80 -0
  70. package/dropdown/DropdownMenuItem.d.ts +4 -0
  71. package/dropdown/DropdownMenuItem.js +92 -0
  72. package/dropdown/types.d.ts +30 -19
  73. package/file-input/FileInput.d.ts +4 -0
  74. package/file-input/FileInput.js +172 -111
  75. package/file-input/FileInput.stories.tsx +507 -0
  76. package/file-input/FileInput.test.js +457 -0
  77. package/file-input/FileItem.d.ts +14 -0
  78. package/file-input/FileItem.js +16 -23
  79. package/file-input/types.d.ts +112 -0
  80. package/file-input/types.js +5 -0
  81. package/flex/Flex.d.ts +4 -0
  82. package/flex/Flex.js +57 -0
  83. package/flex/Flex.stories.tsx +103 -0
  84. package/flex/types.d.ts +21 -0
  85. package/flex/types.js +5 -0
  86. package/footer/Footer.d.ts +1 -1
  87. package/footer/Footer.js +32 -113
  88. package/footer/{Footer.stories.jsx → Footer.stories.tsx} +1 -22
  89. package/footer/Footer.test.js +109 -0
  90. package/footer/Icons.d.ts +2 -0
  91. package/footer/Icons.js +4 -4
  92. package/footer/types.d.ts +21 -17
  93. package/header/Header.js +29 -50
  94. package/header/Header.stories.tsx +46 -36
  95. package/header/Header.test.js +79 -0
  96. package/header/Icons.d.ts +2 -0
  97. package/header/types.d.ts +4 -2
  98. package/heading/Heading.js +1 -1
  99. package/heading/Heading.stories.tsx +54 -0
  100. package/heading/Heading.test.js +186 -0
  101. package/inset/Inset.d.ts +3 -0
  102. package/inset/Inset.js +51 -0
  103. package/inset/Inset.stories.tsx +229 -0
  104. package/inset/types.d.ts +37 -0
  105. package/inset/types.js +5 -0
  106. package/layout/ApplicationLayout.d.ts +20 -0
  107. package/layout/ApplicationLayout.js +71 -135
  108. package/layout/ApplicationLayout.stories.tsx +161 -0
  109. package/layout/Icons.d.ts +5 -0
  110. package/layout/Icons.js +13 -2
  111. package/layout/SidenavContext.d.ts +5 -0
  112. package/layout/SidenavContext.js +19 -0
  113. package/layout/types.d.ts +42 -0
  114. package/layout/types.js +5 -0
  115. package/link/Link.d.ts +3 -2
  116. package/link/Link.js +61 -86
  117. package/link/Link.stories.tsx +139 -16
  118. package/link/Link.test.js +83 -0
  119. package/link/types.d.ts +9 -29
  120. package/main.d.ts +12 -12
  121. package/main.js +64 -58
  122. package/number-input/NumberInput.js +14 -24
  123. package/number-input/NumberInput.stories.tsx +5 -5
  124. package/number-input/NumberInput.test.js +506 -0
  125. package/number-input/NumberInputContext.d.ts +4 -0
  126. package/number-input/NumberInputContext.js +5 -2
  127. package/number-input/numberInputContextTypes.d.ts +19 -0
  128. package/number-input/numberInputContextTypes.js +5 -0
  129. package/number-input/types.d.ts +17 -10
  130. package/package.json +14 -10
  131. package/paginator/Paginator.js +19 -46
  132. package/paginator/Paginator.test.js +308 -0
  133. package/paragraph/Paragraph.d.ts +6 -0
  134. package/paragraph/Paragraph.js +38 -0
  135. package/paragraph/Paragraph.stories.tsx +44 -0
  136. package/password-input/PasswordInput.js +23 -19
  137. package/password-input/PasswordInput.stories.tsx +3 -3
  138. package/password-input/PasswordInput.test.js +180 -0
  139. package/password-input/types.d.ts +29 -19
  140. package/progress-bar/ProgressBar.d.ts +2 -2
  141. package/progress-bar/ProgressBar.js +59 -53
  142. package/progress-bar/ProgressBar.stories.jsx +13 -11
  143. package/progress-bar/ProgressBar.test.js +110 -0
  144. package/progress-bar/types.d.ts +3 -4
  145. package/quick-nav/QuickNav.d.ts +4 -0
  146. package/quick-nav/QuickNav.js +118 -0
  147. package/quick-nav/QuickNav.stories.tsx +264 -0
  148. package/quick-nav/types.d.ts +21 -0
  149. package/quick-nav/types.js +5 -0
  150. package/radio-group/Radio.d.ts +4 -0
  151. package/radio-group/Radio.js +141 -0
  152. package/radio-group/RadioGroup.d.ts +4 -0
  153. package/radio-group/RadioGroup.js +281 -0
  154. package/radio-group/RadioGroup.stories.tsx +100 -0
  155. package/radio-group/RadioGroup.test.js +695 -0
  156. package/radio-group/types.d.ts +114 -0
  157. package/radio-group/types.js +5 -0
  158. package/resultsetTable/ResultsetTable.d.ts +4 -0
  159. package/resultsetTable/ResultsetTable.js +9 -29
  160. package/resultsetTable/ResultsetTable.stories.tsx +275 -0
  161. package/resultsetTable/ResultsetTable.test.js +348 -0
  162. package/resultsetTable/types.d.ts +67 -0
  163. package/resultsetTable/types.js +5 -0
  164. package/select/Icons.d.ts +10 -0
  165. package/select/Icons.js +93 -0
  166. package/select/Listbox.d.ts +4 -0
  167. package/select/Listbox.js +199 -0
  168. package/select/Option.d.ts +4 -0
  169. package/select/Option.js +110 -0
  170. package/select/Select.d.ts +4 -0
  171. package/select/Select.js +158 -380
  172. package/select/Select.stories.tsx +231 -176
  173. package/select/Select.test.js +2175 -0
  174. package/select/types.d.ts +210 -0
  175. package/select/types.js +5 -0
  176. package/sidenav/Sidenav.d.ts +6 -5
  177. package/sidenav/Sidenav.js +186 -54
  178. package/sidenav/Sidenav.stories.tsx +180 -0
  179. package/sidenav/Sidenav.test.js +44 -0
  180. package/sidenav/types.d.ts +50 -27
  181. package/slider/Slider.d.ts +1 -1
  182. package/slider/Slider.js +6 -5
  183. package/slider/Slider.stories.tsx +8 -8
  184. package/slider/Slider.test.js +150 -0
  185. package/slider/types.d.ts +4 -0
  186. package/spinner/Spinner.js +3 -3
  187. package/spinner/Spinner.stories.jsx +1 -0
  188. package/spinner/Spinner.test.js +64 -0
  189. package/switch/Switch.d.ts +2 -2
  190. package/switch/Switch.js +129 -57
  191. package/switch/Switch.stories.tsx +21 -43
  192. package/switch/Switch.test.js +212 -0
  193. package/switch/types.d.ts +9 -6
  194. package/table/Table.js +3 -3
  195. package/table/Table.stories.jsx +2 -1
  196. package/table/Table.test.js +26 -0
  197. package/tabs/Tabs.d.ts +1 -1
  198. package/tabs/Tabs.js +20 -20
  199. package/tabs/Tabs.stories.tsx +112 -0
  200. package/tabs/Tabs.test.js +140 -0
  201. package/tabs/types.d.ts +29 -18
  202. package/tabs-nav/NavTabs.d.ts +8 -0
  203. package/tabs-nav/NavTabs.js +125 -0
  204. package/tabs-nav/NavTabs.stories.tsx +170 -0
  205. package/tabs-nav/NavTabs.test.js +82 -0
  206. package/tabs-nav/Tab.d.ts +4 -0
  207. package/tabs-nav/Tab.js +130 -0
  208. package/tabs-nav/types.d.ts +53 -0
  209. package/tabs-nav/types.js +5 -0
  210. package/tag/Tag.d.ts +1 -1
  211. package/tag/Tag.js +18 -28
  212. package/tag/Tag.stories.tsx +26 -29
  213. package/tag/Tag.test.js +60 -0
  214. package/tag/types.d.ts +23 -14
  215. package/text-input/Suggestion.d.ts +4 -0
  216. package/text-input/Suggestion.js +55 -0
  217. package/text-input/TextInput.d.ts +4 -0
  218. package/text-input/TextInput.js +103 -167
  219. package/text-input/TextInput.stories.tsx +473 -0
  220. package/text-input/TextInput.test.js +1712 -0
  221. package/text-input/types.d.ts +178 -0
  222. package/text-input/types.js +5 -0
  223. package/textarea/Textarea.d.ts +4 -0
  224. package/textarea/Textarea.js +39 -79
  225. package/textarea/Textarea.stories.jsx +37 -15
  226. package/textarea/Textarea.test.js +437 -0
  227. package/textarea/types.d.ts +137 -0
  228. package/textarea/types.js +5 -0
  229. package/toggle-group/ToggleGroup.d.ts +4 -0
  230. package/toggle-group/ToggleGroup.js +18 -46
  231. package/toggle-group/ToggleGroup.stories.tsx +27 -32
  232. package/toggle-group/ToggleGroup.test.js +156 -0
  233. package/toggle-group/types.d.ts +105 -0
  234. package/toggle-group/types.js +5 -0
  235. package/typography/Typography.d.ts +4 -0
  236. package/typography/Typography.js +131 -0
  237. package/typography/Typography.stories.tsx +198 -0
  238. package/typography/types.d.ts +18 -0
  239. package/typography/types.js +5 -0
  240. package/useTheme.d.ts +2 -0
  241. package/useTheme.js +2 -2
  242. package/useTranslatedLabels.d.ts +2 -0
  243. package/useTranslatedLabels.js +20 -0
  244. package/wizard/Wizard.d.ts +1 -1
  245. package/wizard/Wizard.js +112 -58
  246. package/wizard/{Wizard.stories.jsx → Wizard.stories.tsx} +33 -24
  247. package/wizard/Wizard.test.js +141 -0
  248. package/wizard/types.d.ts +13 -12
  249. package/ThemeContext.js +0 -246
  250. package/V3Select/V3Select.js +0 -455
  251. package/V3Select/index.d.ts +0 -27
  252. package/V3Textarea/V3Textarea.js +0 -260
  253. package/V3Textarea/index.d.ts +0 -27
  254. package/chip/index.d.ts +0 -22
  255. package/date/Date.js +0 -373
  256. package/date/index.d.ts +0 -27
  257. package/file-input/index.d.ts +0 -81
  258. package/input-text/Icons.js +0 -22
  259. package/input-text/InputText.js +0 -611
  260. package/input-text/index.d.ts +0 -36
  261. package/radio/Radio.d.ts +0 -4
  262. package/radio/Radio.js +0 -174
  263. package/radio/Radio.stories.tsx +0 -192
  264. package/radio/types.d.ts +0 -54
  265. package/resultsetTable/index.d.ts +0 -19
  266. package/select/index.d.ts +0 -131
  267. package/text-input/index.d.ts +0 -135
  268. package/textarea/index.d.ts +0 -117
  269. package/toggle/Toggle.js +0 -186
  270. package/toggle/index.d.ts +0 -21
  271. package/toggle-group/index.d.ts +0 -21
  272. package/upload/Upload.js +0 -201
  273. package/upload/buttons-upload/ButtonsUpload.js +0 -111
  274. package/upload/buttons-upload/Icons.js +0 -40
  275. package/upload/dragAndDropArea/DragAndDropArea.js +0 -225
  276. package/upload/dragAndDropArea/Icons.js +0 -39
  277. package/upload/file-upload/FileToUpload.js +0 -115
  278. package/upload/file-upload/Icons.js +0 -66
  279. package/upload/files-upload/FilesToUpload.js +0 -109
  280. package/upload/index.d.ts +0 -15
  281. package/upload/transaction/Icons.js +0 -160
  282. package/upload/transaction/Transaction.js +0 -104
  283. package/upload/transactions/Transactions.js +0 -94
  284. package/wizard/Icons.js +0 -65
@@ -0,0 +1,64 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+
5
+ var _react = _interopRequireDefault(require("react"));
6
+
7
+ var _react2 = require("@testing-library/react");
8
+
9
+ var _Spinner = _interopRequireDefault(require("./Spinner"));
10
+
11
+ describe("Spinner component tests", function () {
12
+ test("Spinner renders with correct label", function () {
13
+ var _render = (0, _react2.render)( /*#__PURE__*/_react["default"].createElement(_Spinner["default"], {
14
+ label: "test-loading"
15
+ })),
16
+ getByText = _render.getByText;
17
+
18
+ expect(getByText("test-loading")).toBeTruthy();
19
+ });
20
+ test("Spinner shows value correctly", function () {
21
+ var _render2 = (0, _react2.render)( /*#__PURE__*/_react["default"].createElement(_Spinner["default"], {
22
+ label: "test-loading",
23
+ value: 75,
24
+ showValue: true
25
+ })),
26
+ getByText = _render2.getByText;
27
+
28
+ expect(getByText("75%")).toBeTruthy();
29
+ });
30
+ test("Small spinner hides value and label correctly", function () {
31
+ var _render3 = (0, _react2.render)( /*#__PURE__*/_react["default"].createElement(_Spinner["default"], {
32
+ mode: "small",
33
+ label: "test-loading",
34
+ value: 75,
35
+ showValue: true
36
+ })),
37
+ queryByText = _render3.queryByText;
38
+
39
+ expect(queryByText("test-loading")).toBeFalsy();
40
+ expect(queryByText("75%")).toBeFalsy();
41
+ });
42
+ test("Overlay spinner shows value and label correctly", function () {
43
+ var _render4 = (0, _react2.render)( /*#__PURE__*/_react["default"].createElement(_Spinner["default"], {
44
+ mode: "overlay",
45
+ label: "test-loading",
46
+ value: 75,
47
+ showValue: true
48
+ })),
49
+ getByText = _render4.getByText;
50
+
51
+ expect(getByText("test-loading")).toBeTruthy();
52
+ expect(getByText("75%")).toBeTruthy();
53
+ });
54
+ test("Get spinner by role", function () {
55
+ var _render5 = (0, _react2.render)( /*#__PURE__*/_react["default"].createElement(_Spinner["default"], {
56
+ label: "test-loading",
57
+ value: 75,
58
+ showValue: true
59
+ })),
60
+ getByRole = _render5.getByRole;
61
+
62
+ expect(getByRole("progressbar")).toBeTruthy();
63
+ });
64
+ });
@@ -1,4 +1,4 @@
1
1
  /// <reference types="react" />
2
- import SwitchPropsType from "./types";
3
- declare const DxcSwitch: ({ checked, value, label, labelPosition, name, disabled, onChange, required, margin, size, tabIndex, }: SwitchPropsType) => JSX.Element;
2
+ import { SwitchPropsType } from "./types";
3
+ declare const DxcSwitch: ({ defaultChecked, checked, value, label, labelPosition, name, disabled, optional, onChange, margin, size, tabIndex, }: SwitchPropsType) => JSX.Element;
4
4
  export default DxcSwitch;
package/switch/Switch.js CHANGED
@@ -19,26 +19,29 @@ var _react = _interopRequireWildcard(require("react"));
19
19
 
20
20
  var _styledComponents = _interopRequireWildcard(require("styled-components"));
21
21
 
22
- var _core = require("@material-ui/core");
23
-
24
- var _RequiredComponent = _interopRequireDefault(require("../common/RequiredComponent"));
22
+ var _uuid = require("uuid");
25
23
 
26
24
  var _variables = require("../common/variables.js");
27
25
 
28
26
  var _utils = require("../common/utils.js");
29
27
 
30
- var _useTheme = _interopRequireDefault(require("../useTheme.js"));
28
+ var _useTheme = _interopRequireDefault(require("../useTheme"));
29
+
30
+ var _useTranslatedLabels = _interopRequireDefault(require("../useTranslatedLabels"));
31
31
 
32
- var _BackgroundColorContext = _interopRequireDefault(require("../BackgroundColorContext.js"));
32
+ var _BackgroundColorContext = _interopRequireDefault(require("../BackgroundColorContext"));
33
33
 
34
- var _templateObject, _templateObject2;
34
+ var _templateObject, _templateObject2, _templateObject3, _templateObject4, _templateObject5, _templateObject6;
35
35
 
36
36
  function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
37
37
 
38
38
  function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof3(obj) !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
39
39
 
40
40
  var DxcSwitch = function DxcSwitch(_ref) {
41
- var checked = _ref.checked,
41
+ var _ref2;
42
+
43
+ var defaultChecked = _ref.defaultChecked,
44
+ checked = _ref.checked,
42
45
  value = _ref.value,
43
46
  _ref$label = _ref.label,
44
47
  label = _ref$label === void 0 ? "" : _ref$label,
@@ -48,22 +51,52 @@ var DxcSwitch = function DxcSwitch(_ref) {
48
51
  name = _ref$name === void 0 ? "" : _ref$name,
49
52
  _ref$disabled = _ref.disabled,
50
53
  disabled = _ref$disabled === void 0 ? false : _ref$disabled,
54
+ _ref$optional = _ref.optional,
55
+ optional = _ref$optional === void 0 ? false : _ref$optional,
51
56
  onChange = _ref.onChange,
52
- _ref$required = _ref.required,
53
- required = _ref$required === void 0 ? false : _ref$required,
54
57
  margin = _ref.margin,
55
58
  _ref$size = _ref.size,
56
59
  size = _ref$size === void 0 ? "fitContent" : _ref$size,
57
60
  _ref$tabIndex = _ref.tabIndex,
58
61
  tabIndex = _ref$tabIndex === void 0 ? 0 : _ref$tabIndex;
59
62
 
60
- var _useState = (0, _react.useState)(false),
61
- _useState2 = (0, _slicedToArray2["default"])(_useState, 2),
62
- innerChecked = _useState2[0],
63
- setInnerChecked = _useState2[1];
63
+ var _useState = (0, _react.useState)("switch-".concat((0, _uuid.v4)())),
64
+ _useState2 = (0, _slicedToArray2["default"])(_useState, 1),
65
+ switchId = _useState2[0];
66
+
67
+ var labelId = "label-".concat(switchId);
68
+
69
+ var _useState3 = (0, _react.useState)(defaultChecked !== null && defaultChecked !== void 0 ? defaultChecked : false),
70
+ _useState4 = (0, _slicedToArray2["default"])(_useState3, 2),
71
+ innerChecked = _useState4[0],
72
+ setInnerChecked = _useState4[1];
73
+
74
+ var _useState5 = (0, _react.useState)((_ref2 = label !== "" && label !== null && label !== undefined) !== null && _ref2 !== void 0 ? _ref2 : false),
75
+ _useState6 = (0, _slicedToArray2["default"])(_useState5, 1),
76
+ hasLabel = _useState6[0];
64
77
 
65
78
  var colorsTheme = (0, _useTheme["default"])();
79
+ var translatedLabels = (0, _useTranslatedLabels["default"])();
66
80
  var backgroundType = (0, _react.useContext)(_BackgroundColorContext["default"]);
81
+ var refTrack = (0, _react.useRef)(null);
82
+
83
+ var handleOnKeyDown = function handleOnKeyDown(event) {
84
+ switch (event.key) {
85
+ case "Enter":
86
+ case " ":
87
+ //Space
88
+ event.preventDefault();
89
+ refTrack.current.focus();
90
+ var isChecked = !(checked !== null && checked !== void 0 ? checked : innerChecked);
91
+ setInnerChecked(isChecked);
92
+
93
+ if (typeof onChange === "function") {
94
+ onChange(isChecked);
95
+ }
96
+
97
+ break;
98
+ }
99
+ };
67
100
 
68
101
  var handlerSwitchChange = function handlerSwitchChange(event) {
69
102
  if (checked === undefined) {
@@ -71,34 +104,64 @@ var DxcSwitch = function DxcSwitch(_ref) {
71
104
 
72
105
  var isChecked = (_event$target$checked = event.target.checked) !== null && _event$target$checked !== void 0 ? _event$target$checked : !innerChecked;
73
106
  setInnerChecked(isChecked);
74
- onChange === null || onChange === void 0 ? void 0 : onChange(isChecked);
75
- } else onChange === null || onChange === void 0 ? void 0 : onChange(!checked);
107
+
108
+ if (typeof onChange === "function") {
109
+ onChange(isChecked);
110
+ }
111
+ } else {
112
+ if (typeof onChange === "function") {
113
+ onChange(!checked);
114
+ }
115
+ }
76
116
  };
77
117
 
78
118
  return /*#__PURE__*/_react["default"].createElement(_styledComponents.ThemeProvider, {
79
119
  theme: colorsTheme["switch"]
80
120
  }, /*#__PURE__*/_react["default"].createElement(SwitchContainer, {
81
121
  margin: margin,
122
+ size: size,
123
+ onKeyDown: handleOnKeyDown
124
+ }, labelPosition === "before" && hasLabel && /*#__PURE__*/_react["default"].createElement(LabelContainer, {
125
+ id: labelId,
126
+ labelPosition: labelPosition,
127
+ onClick: !disabled ? handlerSwitchChange : undefined,
82
128
  disabled: disabled,
129
+ backgroundType: backgroundType,
130
+ hasLabel: hasLabel
131
+ }, label, " ", optional && /*#__PURE__*/_react["default"].createElement(_react["default"].Fragment, null, translatedLabels.formFields.optionalLabel)), /*#__PURE__*/_react["default"].createElement(SwitchBase, {
83
132
  labelPosition: labelPosition,
84
- size: size,
85
- backgroundType: backgroundType
86
- }, /*#__PURE__*/_react["default"].createElement(_core.Switch, {
87
- checked: checked !== null && checked !== void 0 ? checked : innerChecked,
88
- inputProps: {
89
- name: name,
90
- tabIndex: tabIndex
91
- },
92
- onChange: handlerSwitchChange,
93
- value: value,
133
+ hasLabel: hasLabel,
134
+ htmlFor: labelId,
135
+ onClick: disabled === true ? function () {} : handlerSwitchChange
136
+ }, /*#__PURE__*/_react["default"].createElement(SwitchInput, {
137
+ type: "checkbox",
138
+ role: "switch",
139
+ name: name,
140
+ id: labelId,
94
141
  disabled: disabled,
95
- disableRipple: true
96
- }), /*#__PURE__*/_react["default"].createElement(LabelContainer, {
142
+ value: value,
143
+ "aria-labelledby": labelId,
144
+ "aria-label": hasLabel ? label : undefined,
145
+ "aria-checked": checked !== null && checked !== void 0 ? checked : innerChecked,
146
+ defaultChecked: defaultChecked !== null && defaultChecked !== void 0 ? defaultChecked : undefined,
147
+ tabIndex: -1
148
+ }), disabled ? /*#__PURE__*/_react["default"].createElement(DisabledSwitchTrack, {
149
+ backgroundType: backgroundType,
150
+ "data-checked": checked !== null && checked !== void 0 ? checked : innerChecked ? innerChecked : undefined,
151
+ tabIndex: -1
152
+ }) : /*#__PURE__*/_react["default"].createElement(SwitchTrack, {
153
+ backgroundType: backgroundType,
154
+ "data-checked": checked !== null && checked !== void 0 ? checked : innerChecked ? innerChecked : undefined,
155
+ tabIndex: tabIndex,
156
+ ref: refTrack
157
+ })), labelPosition === "after" && hasLabel && /*#__PURE__*/_react["default"].createElement(LabelContainer, {
158
+ id: labelId,
97
159
  labelPosition: labelPosition,
98
- onClick: !disabled && handlerSwitchChange,
160
+ onClick: !disabled ? handlerSwitchChange : undefined,
99
161
  disabled: disabled,
100
- backgroundType: backgroundType
101
- }, required && /*#__PURE__*/_react["default"].createElement(_RequiredComponent["default"], null), label)));
162
+ backgroundType: backgroundType,
163
+ hasLabel: hasLabel
164
+ }, optional && /*#__PURE__*/_react["default"].createElement(_react["default"].Fragment, null, translatedLabels.formFields.optionalLabel), " ", label)));
102
165
  };
103
166
 
104
167
  var sizes = {
@@ -113,10 +176,8 @@ var calculateWidth = function calculateWidth(margin, size) {
113
176
  return size === "fillParent" ? "calc(".concat(sizes[size], " - ").concat((0, _utils.getMargin)(margin, "left"), " - ").concat((0, _utils.getMargin)(margin, "right"), ")") : sizes[size];
114
177
  };
115
178
 
116
- var SwitchContainer = _styledComponents["default"].div(_templateObject || (_templateObject = (0, _taggedTemplateLiteral2["default"])(["\n width: ", ";\n display: inline-flex;\n align-items: center;\n flex-direction: ", ";\n \n\n margin: ", ";\n margin-top: ", ";\n margin-right: ", ";\n margin-bottom: ", ";\n margin-left: ", ";\n cursor: ", ";\n\n .MuiSwitch-root {\n align-items: center;\n width: ", ";\n height: 45px;\n margin: 3px;\n\n .Mui-focusVisible {\n outline: ", ";\n outline-offset: -3px;\n }\n\n .MuiSwitch-track {\n /*Enabled and unchecked bar*/\n background-color: ", ";\n height: ", ";\n }\n\n .MuiSwitch-switchBase + .MuiSwitch-track {\n opacity: 1;\n }\n\n .MuiIconButton-root {\n /*Enabled and unchecked*/\n top: unset;\n .MuiSwitch-thumb {\n /*Only for thumb in all states*/\n width: ", ";\n height: ", ";\n }\n color: ", ";\n &:hover {\n background-color: transparent;\n }\n &.Mui-disabled {\n /*Disabled and unchecked*/\n color: ", ";\n + .MuiSwitch-track {\n /*Disabled and unchecked bar*/\n background-color: ", ";\n }\n }\n &.Mui-disabled.Mui-checked {\n /*Disabled and checked*/\n color: ", ";\n + .MuiSwitch-track {\n /*Disabled and checked bar*/\n background-color: ", ";\n }\n }\n &.Mui-checked {\n /*Enabled and checked*/\n color: ", ";\n transform: translateX(", ");\n &:hover {\n background-color: transparent;\n }\n + .MuiSwitch-track {\n /*Enabled and checked bar*/\n background-color: ", ";\n }\n }\n }\n }\n"])), function (props) {
179
+ var SwitchContainer = _styledComponents["default"].div(_templateObject || (_templateObject = (0, _taggedTemplateLiteral2["default"])(["\n display: inline-flex;\n align-items: center;\n width: ", ";\n height: 40px;\n\n margin: ", ";\n margin-top: ", ";\n margin-right: ", ";\n margin-bottom: ", ";\n margin-left: ", ";\n"])), function (props) {
117
180
  return calculateWidth(props.margin, props.size);
118
- }, function (props) {
119
- return props.labelPosition === "after" ? "row" : "row-reverse";
120
181
  }, function (props) {
121
182
  return props.margin && (0, _typeof2["default"])(props.margin) !== "object" ? _variables.spaces[props.margin] : "0px";
122
183
  }, function (props) {
@@ -127,16 +188,38 @@ var SwitchContainer = _styledComponents["default"].div(_templateObject || (_temp
127
188
  return props.margin && (0, _typeof2["default"])(props.margin) === "object" && props.margin.bottom ? _variables.spaces[props.margin.bottom] : "";
128
189
  }, function (props) {
129
190
  return props.margin && (0, _typeof2["default"])(props.margin) === "object" && props.margin.left ? _variables.spaces[props.margin.left] : "";
191
+ });
192
+
193
+ var LabelContainer = _styledComponents["default"].span(_templateObject2 || (_templateObject2 = (0, _taggedTemplateLiteral2["default"])(["\n overflow: hidden;\n text-overflow: ellipsis;\n white-space: nowrap;\n color: ", ";\n opacity: 1;\n font-family: ", ";\n font-size: ", ";\n font-style: ", ";\n font-weight: ", ";\n cursor: ", ";\n\n ", ";\n\n ", "\n"])), function (props) {
194
+ return props.disabled ? props.backgroundType === "dark" ? props.theme.disabledLabelFontColorOnDark : props.theme.disabledLabelFontColor : props.backgroundType === "dark" ? props.theme.labelFontColorOnDark : props.theme.labelFontColor;
130
195
  }, function (props) {
131
- return props.disabled ? "not-allowed" : "default";
196
+ return props.theme.labelFontFamily;
132
197
  }, function (props) {
133
- return props.theme.trackWidth;
198
+ return props.theme.labelFontSize;
134
199
  }, function (props) {
135
- return "".concat(props.backgroundType === "dark" ? props.theme.thumbFocusColorOnDark : props.theme.thumbFocusColor, " solid 2px");
200
+ return props.disabled ? props.theme.disabledLabelFontStyle : props.theme.labelFontStyle;
136
201
  }, function (props) {
137
- return props.backgroundType === "dark" ? props.theme.uncheckedTrackBackgroundColorOnDark : props.theme.uncheckedTrackBackgroundColor;
202
+ return props.theme.labelFontWeight;
203
+ }, function (props) {
204
+ return props.disabled === true ? "not-allowed" : "pointer";
205
+ }, function (props) {
206
+ return !props.hasLabel ? "margin: 0px;" : props.labelPosition === "after" ? "margin-left: ".concat(props.theme.spaceBetweenLabelSwitch, ";") : "margin-right: ".concat(props.theme.spaceBetweenLabelSwitch, ";");
207
+ }, function (props) {
208
+ return props.labelPosition === "before" && "order: -1";
209
+ });
210
+
211
+ var SwitchBase = _styledComponents["default"].label(_templateObject3 || (_templateObject3 = (0, _taggedTemplateLiteral2["default"])(["\n display: flex;\n align-items: center;\n justify-content: space-between;\n cursor: pointer;\n margin: ", ";\n"])), function (props) {
212
+ return !props.hasLabel ? "0px 4px" : props.labelPosition === "before" ? "0 4px 0 12px" : "0 12px 0 4px";
213
+ });
214
+
215
+ var SwitchInput = _styledComponents["default"].input(_templateObject4 || (_templateObject4 = (0, _taggedTemplateLiteral2["default"])(["\n opacity: 0;\n width: 0;\n height: 0;\n margin: 0px;\n"])));
216
+
217
+ var SwitchTrack = _styledComponents["default"].span(_templateObject5 || (_templateObject5 = (0, _taggedTemplateLiteral2["default"])(["\n border-radius: 15px;\n width: ", ";\n height: ", ";\n position: relative;\n transition: transform 0.2s ease;\n\n &:focus-visible {\n outline: none;\n ::before {\n outline: ", ";\n outline-offset: 6px;\n }\n }\n\n /* Thumb element */\n ::before {\n content: \"\";\n transform: initial;\n transition: transform 0.2s ease;\n position: absolute;\n width: ", ";\n height: ", ";\n border-radius: 50%;\n z-index: 1;\n box-shadow: 0px 2px 1px -1px rgb(0 0 0 / 20%), 0px 1px 1px 0px rgb(0 0 0 / 14%), 0px 1px 3px 0px rgb(0 0 0 / 12%);\n bottom: -6px;\n left: -4px;\n transform: translateX(0px);\n background-color: ", ";\n }\n\n /* Unchecked */\n background-color: ", ";\n\n /* Checked */\n &[data-checked=\"true\"] {\n background-color: ", ";\n ::before {\n transform: translateX(", ");\n background-color: ", ";\n }\n }\n"])), function (props) {
218
+ return props.theme.trackWidth;
138
219
  }, function (props) {
139
220
  return props.theme.trackHeight;
221
+ }, function (props) {
222
+ return "".concat(props.backgroundType === "dark" ? props.theme.thumbFocusColorOnDark : props.theme.thumbFocusColor, " solid 2px");
140
223
  }, function (props) {
141
224
  return props.theme.thumbWidth;
142
225
  }, function (props) {
@@ -144,36 +227,25 @@ var SwitchContainer = _styledComponents["default"].div(_templateObject || (_temp
144
227
  }, function (props) {
145
228
  return props.backgroundType === "dark" ? props.theme.uncheckedThumbBackgroundColorOnDark : props.theme.uncheckedThumbBackgroundColor;
146
229
  }, function (props) {
147
- return props.backgroundType === "dark" ? props.theme.disabledUncheckedThumbBackgroundColorOnDark : props.theme.disabledUncheckedThumbBackgroundColor;
148
- }, function (props) {
149
- return props.backgroundType === "dark" ? props.theme.disabledUncheckedTrackBackgroundColorOnDark : props.theme.disabledUncheckedTrackBackgroundColor;
150
- }, function (props) {
151
- return props.backgroundType === "dark" ? props.theme.disabledCheckedThumbBackgroundColorOnDark : props.theme.disabledCheckedThumbBackgroundColor;
152
- }, function (props) {
153
- return props.backgroundType === "dark" ? props.theme.disabledCheckedTrackBackgroundColorOnDark : props.theme.disabledCheckedTrackBackgroundColor;
230
+ return props.backgroundType === "dark" ? props.theme.uncheckedTrackBackgroundColorOnDark : props.theme.uncheckedTrackBackgroundColor;
154
231
  }, function (props) {
155
- return props.backgroundType === "dark" ? props.theme.checkedThumbBackgroundColorOnDark : props.theme.checkedThumbBackgroundColor;
232
+ return props.backgroundType === "dark" ? props.theme.checkedTrackBackgroundColorOnDark : props.theme.checkedTrackBackgroundColor;
156
233
  }, function (props) {
157
234
  return props.theme.thumbShift;
158
235
  }, function (props) {
159
- return props.backgroundType === "dark" ? props.theme.checkedTrackBackgroundColorOnDark : props.theme.checkedTrackBackgroundColor;
236
+ return props.backgroundType === "dark" ? props.theme.checkedThumbBackgroundColorOnDark : props.theme.checkedThumbBackgroundColor;
160
237
  });
161
238
 
162
- var LabelContainer = _styledComponents["default"].span(_templateObject2 || (_templateObject2 = (0, _taggedTemplateLiteral2["default"])(["\n overflow: hidden;\n text-overflow: ellipsis;\n white-space: nowrap;\n color: ", ";\n opacity: 1;\n font-family: ", ";\n font-size: ", ";\n font-style: ", ";\n font-weight: ", ";\n cursor: ", ";\n ", "\n"])), function (props) {
163
- return props.disabled ? props.backgroundType === "dark" ? props.theme.disabledLabelFontColorOnDark : props.theme.disabledLabelFontColor : props.backgroundType === "dark" ? props.theme.labelFontColorOnDark : props.theme.labelFontColor;
164
- }, function (props) {
165
- return props.theme.labelFontFamily;
166
- }, function (props) {
167
- return props.theme.labelFontSize;
239
+ var DisabledSwitchTrack = (0, _styledComponents["default"])(SwitchTrack)(_templateObject6 || (_templateObject6 = (0, _taggedTemplateLiteral2["default"])(["\n cursor: not-allowed;\n\n /* Unchecked */\n background-color: ", ";\n\n ::before {\n background-color: ", ";\n }\n\n /* Checked */\n &[data-checked=\"true\"] {\n background-color: ", ";\n\n ::before {\n transform: translateX(", ");\n background-color: ", ";\n }\n }\n"])), function (props) {
240
+ return props.backgroundType === "dark" ? props.theme.disabledUncheckedTrackBackgroundColorOnDark : props.theme.disabledUncheckedTrackBackgroundColor;
168
241
  }, function (props) {
169
- return props.disabled ? props.theme.disabledLabelFontStyle : props.theme.labelFontStyle;
242
+ return props.backgroundType === "dark" ? props.theme.disabledUncheckedThumbBackgroundColorOnDark : props.theme.disabledUncheckedThumbBackgroundColor;
170
243
  }, function (props) {
171
- return props.theme.labelFontWeight;
244
+ return props.backgroundType === "dark" ? props.theme.disabledCheckedTrackBackgroundColorOnDark : props.theme.disabledCheckedTrackBackgroundColor;
172
245
  }, function (props) {
173
- return props.disabled === true ? "not-allowed" : "pointer";
246
+ return props.theme.thumbShift;
174
247
  }, function (props) {
175
- return props.labelPosition === "after" ? "margin-left: ".concat(props.theme.spaceBetweenLabelSwitch, ";") : "margin-right: ".concat(props.theme.spaceBetweenLabelSwitch, ";");
248
+ return props.backgroundType === "dark" ? props.theme.disabledCheckedThumbBackgroundColorOnDark : props.theme.disabledCheckedThumbBackgroundColor;
176
249
  });
177
-
178
250
  var _default = DxcSwitch;
179
251
  exports["default"] = _default;
@@ -1,5 +1,4 @@
1
1
  import React from "react";
2
- import { userEvent, within } from "@storybook/testing-library";
3
2
  import DxcSwitch from "./Switch";
4
3
  import { BackgroundColorProvider } from "../BackgroundColorContext";
5
4
  import Title from "../../.storybook/components/Title";
@@ -21,25 +20,29 @@ export const Chromatic = () => (
21
20
  <Title title="Without label" theme="light" level={4} />
22
21
  <DxcSwitch />
23
22
  </ExampleContainer>
23
+ <ExampleContainer pseudoState="pseudo-focus-visible">
24
+ <Title title="Focused" theme="light" level={4} />
25
+ <DxcSwitch label="Switch" labelPosition="after" />
26
+ </ExampleContainer>
24
27
  <ExampleContainer>
25
28
  <Title title="Checked" theme="light" level={4} />
26
- <DxcSwitch label="Switch" checked />
29
+ <DxcSwitch label="Switch" defaultChecked />
27
30
  </ExampleContainer>
28
31
  <ExampleContainer>
29
- <Title title="Required" theme="light" level={4} />
30
- <DxcSwitch label="Switch" required />
32
+ <Title title="Optional" theme="light" level={4} />
33
+ <DxcSwitch label="Switch" optional />
31
34
  </ExampleContainer>
32
35
  <ExampleContainer>
33
36
  <Title title="Disabled" theme="light" level={4} />
34
37
  <DxcSwitch label="Switch" disabled />
35
38
  </ExampleContainer>
36
39
  <ExampleContainer>
37
- <Title title="Disabled required" theme="light" level={4} />
38
- <DxcSwitch label="Switch" disabled required labelPosition="after" />
40
+ <Title title="Disabled optional" theme="light" level={4} />
41
+ <DxcSwitch label="Switch" disabled optional labelPosition="after" />
39
42
  </ExampleContainer>
40
43
  <ExampleContainer>
41
44
  <Title title="Disabled checked" theme="light" level={4} />
42
- <DxcSwitch label="Switch" disabled checked labelPosition="after" />
45
+ <DxcSwitch label="Switch" disabled defaultChecked labelPosition="after" />
43
46
  </ExampleContainer>
44
47
  <BackgroundColorProvider color="#333333">
45
48
  <DarkContainer>
@@ -47,32 +50,36 @@ export const Chromatic = () => (
47
50
  <Title title="With label" theme="dark" level={4} />
48
51
  <DxcSwitch label="Switch" />
49
52
  </ExampleContainer>
53
+ <ExampleContainer pseudoState="pseudo-focus-visible">
54
+ <Title title="Focused" theme="dark" level={4} />
55
+ <DxcSwitch label="Switch" labelPosition="after" />
56
+ </ExampleContainer>
50
57
  <ExampleContainer>
51
58
  <Title title="Checked" theme="dark" level={4} />
52
- <DxcSwitch label="Switch" checked />
59
+ <DxcSwitch label="Switch" defaultChecked />
53
60
  </ExampleContainer>
54
61
  <ExampleContainer>
55
- <Title title="Required" theme="dark" level={4} />
56
- <DxcSwitch label="Switch" required />
62
+ <Title title="Optional" theme="dark" level={4} />
63
+ <DxcSwitch label="Switch" optional />
57
64
  </ExampleContainer>
58
65
  <ExampleContainer>
59
66
  <Title title="Disabled" theme="dark" level={4} />
60
67
  <DxcSwitch label="Switch" disabled />
61
68
  </ExampleContainer>
62
69
  <ExampleContainer>
63
- <Title title="Disabled required" theme="dark" level={4} />
64
- <DxcSwitch label="Switch" disabled required labelPosition="after" />
70
+ <Title title="Disabled optional" theme="dark" level={4} />
71
+ <DxcSwitch label="Switch" disabled optional labelPosition="after" />
65
72
  </ExampleContainer>
66
73
  <ExampleContainer>
67
74
  <Title title="Disabled checked" theme="dark" level={4} />
68
- <DxcSwitch label="Switch" disabled checked labelPosition="after" />
75
+ <DxcSwitch label="Switch" disabled defaultChecked labelPosition="after" />
69
76
  </ExampleContainer>
70
77
  </DarkContainer>
71
78
  </BackgroundColorProvider>
72
79
  <Title title="Margins" theme="light" level={2} />
73
80
  <ExampleContainer>
74
81
  <Title title="Xxsmall margin" theme="light" level={4} />
75
- <DxcSwitch label="Xxsmmall" margin="xxsmall" />
82
+ <DxcSwitch label="Xxsmall" margin="xxsmall" />
76
83
  </ExampleContainer>
77
84
  <ExampleContainer>
78
85
  <Title title="Xsmall margin" theme="light" level={4} />
@@ -129,32 +136,3 @@ export const Chromatic = () => (
129
136
  </ExampleContainer>
130
137
  </>
131
138
  );
132
-
133
- const Switch = () => (
134
- <ExampleContainer>
135
- <Title title="Focused" theme="light" level={4} />
136
- <DxcSwitch label="Switch" />
137
- </ExampleContainer>
138
- );
139
- export const FocusedSwitch = Switch.bind({});
140
- FocusedSwitch.play = async ({ canvasElement }) => {
141
- const canvas = within(canvasElement);
142
- canvas.getByRole("checkbox").focus();
143
- };
144
-
145
- const DarkSwitch = () => (
146
- <BackgroundColorProvider color="#333333">
147
- <DarkContainer>
148
- <ExampleContainer>
149
- <Title title="Focused" theme="dark" level={4} />
150
- <DxcSwitch label="Switch" />
151
- </ExampleContainer>
152
- </DarkContainer>
153
- </BackgroundColorProvider>
154
- );
155
-
156
- export const FocusedSwitchOnDark = DarkSwitch.bind({});
157
- FocusedSwitchOnDark.play = async ({ canvasElement }) => {
158
- const canvas = within(canvasElement);
159
- canvas.getByRole("checkbox").focus();
160
- };