@cypress-design/react-checkbox 0.2.1 → 0.4.0

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 CHANGED
@@ -1,5 +1,32 @@
1
1
  # @cypress-design/react-checkbox
2
2
 
3
+ ## 0.4.0
4
+
5
+ ### Minor Changes
6
+
7
+ - [#51](https://github.com/cypress-io/cypress-design/pull/51) [`ccd8f9a`](https://github.com/cypress-io/cypress-design/commit/ccd8f9a8feb624c0a52deaa9754c76969f43fc1e) Thanks [@elevatebart](https://github.com/elevatebart)! - in the build asset, add a comment containing all used windicss classes in the component
8
+
9
+ ### Patch Changes
10
+
11
+ - [#57](https://github.com/cypress-io/cypress-design/pull/57) [`6fa7d52`](https://github.com/cypress-io/cypress-design/commit/6fa7d52e409762bd1b32a8b04edaf78765830754) Thanks [@elevatebart](https://github.com/elevatebart)! - fix: move the input for checkboxes outside of label for accessibility
12
+
13
+ Source: https://www.a11ymatters.com/pattern/checkbox/
14
+
15
+ - Updated dependencies [[`ccd8f9a`](https://github.com/cypress-io/cypress-design/commit/ccd8f9a8feb624c0a52deaa9754c76969f43fc1e), [`0866c65`](https://github.com/cypress-io/cypress-design/commit/0866c654f24c36951c98468d789462748606b428), [`43b53eb`](https://github.com/cypress-io/cypress-design/commit/43b53eb6bd10111629239a87374cfcc894eda0e3)]:
16
+ - @cypress-design/react-icon@0.4.0
17
+
18
+ ## 0.3.0
19
+
20
+ ### Minor Changes
21
+
22
+ - [#44](https://github.com/cypress-io/cypress-design/pull/44) [`2fdaf6b`](https://github.com/cypress-io/cypress-design/commit/2fdaf6be6f81a2a851761258347ed213577c5b26) Thanks [@elevatebart](https://github.com/elevatebart)! - Add missing descriptions of props in button, tooltip, checkbox
23
+
24
+ ## 0.2.2
25
+
26
+ ### Patch Changes
27
+
28
+ - [`e3eb9c7`](https://github.com/cypress-io/cypress-design/commit/e3eb9c7fee2d7a6e0a773e85ed4b73be04d83587) Thanks [@elevatebart](https://github.com/elevatebart)! - downgrade typescript to 4.4 for compat with app
29
+
3
30
  ## 0.2.1
4
31
 
5
32
  ### Patch Changes
package/Checkbox.tsx CHANGED
@@ -4,22 +4,52 @@ import type { FunctionComponent, HTMLProps, ReactNode } from 'react'
4
4
  import { IconCheckmarkSmall } from '@cypress-design/react-icon'
5
5
 
6
6
  export interface CheckboxProps
7
- extends Omit<HTMLProps<HTMLLabelElement>, 'label' | 'onChange'> {
7
+ extends Omit<HTMLProps<HTMLDivElement>, 'label' | 'onChange' | 'name'> {
8
+ /**
9
+ * A unique identifier for the checkbox on the whole page.
10
+ * It will be used to give match label with input for a11y.
11
+ */
8
12
  id?: string
9
- checked: boolean
10
- onChange: (event: React.ChangeEvent<HTMLInputElement>) => void
11
- color?: 'indigo' | 'red' | 'jade'
12
- label?: ReactNode
13
+ /**
14
+ * Name attribute of the <input type="checkbox"/>.
15
+ */
16
+ name?: string
17
+ /**
18
+ * Is the checkbox checked when it is first rendered.
19
+ */
20
+ checked?: boolean
21
+ /**
22
+ * The color of the background in the checkbox.
23
+ * The checkmark will always be white.
24
+ */
25
+ color?: 'red' | 'indigo' | 'jade'
26
+ /**
27
+ * If the checkbox is disabled, it will not be clickable.
28
+ */
13
29
  disabled?: boolean
30
+ /**
31
+ * Label for the checkbox.
32
+ * It is very important to set this to make the checkbox accessible.
33
+ */
34
+ label?: ReactNode
35
+ onChange: (event: React.ChangeEvent<HTMLInputElement>) => void
14
36
  }
15
37
 
38
+ const uid = () =>
39
+ String(Date.now().toString(32) + Math.random().toString(16)).replace(
40
+ /\./g,
41
+ ''
42
+ )
43
+
16
44
  export const Checkbox: FunctionComponent<CheckboxProps> = ({
17
- id = crypto.randomUUID(),
45
+ id = uid(),
18
46
  checked = false,
19
47
  onChange,
20
48
  color = 'indigo',
21
49
  label,
22
50
  disabled,
51
+ className,
52
+ name,
23
53
  ...rest
24
54
  }) => {
25
55
  const [localChecked, setChecked] = React.useState(checked)
@@ -30,15 +60,12 @@ export const Checkbox: FunctionComponent<CheckboxProps> = ({
30
60
  }
31
61
 
32
62
  return (
33
- <label
34
- {...rest}
35
- className={clsx(rest.className, 'relative flex items-center')}
36
- >
63
+ <div className={clsx(className, 'relative flex items-center')} {...rest}>
37
64
  <input
38
65
  id={id}
39
66
  className="absolute inset-0 w-0 h-0 opacity-0"
40
67
  aria-describedby={`${id}-description`}
41
- name={id}
68
+ name={name || id}
42
69
  type="checkbox"
43
70
  onChange={onChangeInput}
44
71
  disabled={disabled}
@@ -61,17 +88,19 @@ export const Checkbox: FunctionComponent<CheckboxProps> = ({
61
88
  : 'border-gray-200 bg-white',
62
89
  ])}
63
90
  />
64
- {label && (
65
- <span
66
- className={clsx([
67
- disabled ? 'text-gray-500' : 'text-gray-800',
68
- 'block ml-2 text-16px leading-normal font-light select-none',
69
- ])}
70
- >
71
- {label}
72
- </span>
73
- )}
74
- </label>
91
+ <label htmlFor={id}>
92
+ {label && (
93
+ <span
94
+ className={clsx([
95
+ disabled ? 'text-gray-500' : 'text-gray-800',
96
+ 'block ml-2 text-16px leading-normal font-light select-none',
97
+ ])}
98
+ >
99
+ {label}
100
+ </span>
101
+ )}
102
+ </label>
103
+ </div>
75
104
  )
76
105
  }
77
106
 
@@ -1,12 +1,34 @@
1
1
  import * as React from 'react';
2
2
  import type { FunctionComponent, HTMLProps, ReactNode } from 'react';
3
- export interface CheckboxProps extends Omit<HTMLProps<HTMLLabelElement>, 'label' | 'onChange'> {
3
+ export interface CheckboxProps extends Omit<HTMLProps<HTMLDivElement>, 'label' | 'onChange' | 'name'> {
4
+ /**
5
+ * A unique identifier for the checkbox on the whole page.
6
+ * It will be used to give match label with input for a11y.
7
+ */
4
8
  id?: string;
5
- checked: boolean;
6
- onChange: (event: React.ChangeEvent<HTMLInputElement>) => void;
7
- color?: 'indigo' | 'red' | 'jade';
8
- label?: ReactNode;
9
+ /**
10
+ * Name attribute of the <input type="checkbox"/>.
11
+ */
12
+ name?: string;
13
+ /**
14
+ * Is the checkbox checked when it is first rendered.
15
+ */
16
+ checked?: boolean;
17
+ /**
18
+ * The color of the background in the checkbox.
19
+ * The checkmark will always be white.
20
+ */
21
+ color?: 'red' | 'indigo' | 'jade';
22
+ /**
23
+ * If the checkbox is disabled, it will not be clickable.
24
+ */
9
25
  disabled?: boolean;
26
+ /**
27
+ * Label for the checkbox.
28
+ * It is very important to set this to make the checkbox accessible.
29
+ */
30
+ label?: ReactNode;
31
+ onChange: (event: React.ChangeEvent<HTMLInputElement>) => void;
10
32
  }
11
33
  export declare const Checkbox: FunctionComponent<CheckboxProps>;
12
34
  export default Checkbox;
@@ -1 +1 @@
1
- {"version":3,"file":"Checkbox.d.ts","sourceRoot":"","sources":["../Checkbox.tsx"],"names":[],"mappings":"AACA,OAAO,KAAK,KAAK,MAAM,OAAO,CAAA;AAC9B,OAAO,KAAK,EAAE,iBAAiB,EAAE,SAAS,EAAE,SAAS,EAAE,MAAM,OAAO,CAAA;AAGpE,MAAM,WAAW,aACf,SAAQ,IAAI,CAAC,SAAS,CAAC,gBAAgB,CAAC,EAAE,OAAO,GAAG,UAAU,CAAC;IAC/D,EAAE,CAAC,EAAE,MAAM,CAAA;IACX,OAAO,EAAE,OAAO,CAAA;IAChB,QAAQ,EAAE,CAAC,KAAK,EAAE,KAAK,CAAC,WAAW,CAAC,gBAAgB,CAAC,KAAK,IAAI,CAAA;IAC9D,KAAK,CAAC,EAAE,QAAQ,GAAG,KAAK,GAAG,MAAM,CAAA;IACjC,KAAK,CAAC,EAAE,SAAS,CAAA;IACjB,QAAQ,CAAC,EAAE,OAAO,CAAA;CACnB;AAED,eAAO,MAAM,QAAQ,EAAE,iBAAiB,CAAC,aAAa,CA4DrD,CAAA;AAED,eAAe,QAAQ,CAAA"}
1
+ {"version":3,"file":"Checkbox.d.ts","sourceRoot":"","sources":["../Checkbox.tsx"],"names":[],"mappings":"AACA,OAAO,KAAK,KAAK,MAAM,OAAO,CAAA;AAC9B,OAAO,KAAK,EAAE,iBAAiB,EAAE,SAAS,EAAE,SAAS,EAAE,MAAM,OAAO,CAAA;AAGpE,MAAM,WAAW,aACf,SAAQ,IAAI,CAAC,SAAS,CAAC,cAAc,CAAC,EAAE,OAAO,GAAG,UAAU,GAAG,MAAM,CAAC;IACtE;;;OAGG;IACH,EAAE,CAAC,EAAE,MAAM,CAAA;IACX;;OAEG;IACH,IAAI,CAAC,EAAE,MAAM,CAAA;IACb;;OAEG;IACH,OAAO,CAAC,EAAE,OAAO,CAAA;IACjB;;;OAGG;IACH,KAAK,CAAC,EAAE,KAAK,GAAG,QAAQ,GAAG,MAAM,CAAA;IACjC;;OAEG;IACH,QAAQ,CAAC,EAAE,OAAO,CAAA;IAClB;;;OAGG;IACH,KAAK,CAAC,EAAE,SAAS,CAAA;IACjB,QAAQ,EAAE,CAAC,KAAK,EAAE,KAAK,CAAC,WAAW,CAAC,gBAAgB,CAAC,KAAK,IAAI,CAAA;CAC/D;AAQD,eAAO,MAAM,QAAQ,EAAE,iBAAiB,CAAC,aAAa,CA6DrD,CAAA;AAED,eAAe,QAAQ,CAAA"}
package/dist/index.es.js CHANGED
@@ -1,6 +1,7 @@
1
1
  import clsx from 'clsx';
2
2
  import * as React from 'react';
3
3
  import * as iconsRegistry from '@cypress-design/icon-registry';
4
+ import { ICON_COLOR_PROP_NAMES } from '@cypress-design/icon-registry';
4
5
 
5
6
  /******************************************************************************
6
7
  Copyright (c) Microsoft Corporation.
@@ -79,10 +80,17 @@ function __rest(s, e) {
79
80
  }
80
81
 
81
82
  var compileReactIconProperties = function (_a) {
82
- var body = _a.body, compiledClasses = _a.compiledClasses, size = _a.size; _a.strokeColor; _a.fillColor; _a.secondaryStrokeColor; _a.secondaryFillColor; var attributes = __rest(_a, ["body", "compiledClasses", "size", "strokeColor", "fillColor", "secondaryStrokeColor", "secondaryFillColor"]);
83
+ var body = _a.body, compiledClasses = _a.compiledClasses, size = _a.size, attributes = __rest(_a, ["body", "compiledClasses", "size"]);
84
+ var filteredAttributes = Object.keys(attributes).reduce(function (newAttributes, attrName) {
85
+ if (!ICON_COLOR_PROP_NAMES.includes(attrName)) {
86
+ newAttributes[attrName] =
87
+ attributes[attrName];
88
+ }
89
+ return newAttributes;
90
+ }, {});
83
91
  var componentProps = __assign({ width: size, height: size, fill: 'none', dangerouslySetInnerHTML: {
84
92
  __html: body
85
- } }, attributes);
93
+ } }, filteredAttributes);
86
94
  if (attributes.className) {
87
95
  compiledClasses.push(attributes.className);
88
96
  }
@@ -98,20 +106,24 @@ var IconCheckmarkSmall = function (props) {
98
106
  };
99
107
  var body = iconBodies[size];
100
108
  if (!body) {
101
- throw Error("Icon \"checkmark-small\" is not available in size ".concat(size));
109
+ throw Error("Icon \"checkmark-small\" is not available in size " + size);
102
110
  }
103
111
  return React.createElement('svg', compileReactIconProperties(__assign(__assign({}, props), { size: size, body: body, compiledClasses: compiledClasses })));
104
112
  };
113
+ /* <windicss-keep class=""> */
105
114
 
115
+ var uid = function () {
116
+ return String(Date.now().toString(32) + Math.random().toString(16)).replace(/\./g, '');
117
+ };
106
118
  var Checkbox = function (_a) {
107
- var _b = _a.id, id = _b === void 0 ? crypto.randomUUID() : _b, _c = _a.checked, checked = _c === void 0 ? false : _c, onChange = _a.onChange, _d = _a.color, color = _d === void 0 ? 'indigo' : _d, label = _a.label, disabled = _a.disabled, rest = __rest$1(_a, ["id", "checked", "onChange", "color", "label", "disabled"]);
119
+ var _b = _a.id, id = _b === void 0 ? uid() : _b, _c = _a.checked, checked = _c === void 0 ? false : _c, onChange = _a.onChange, _d = _a.color, color = _d === void 0 ? 'indigo' : _d, label = _a.label, disabled = _a.disabled, className = _a.className, name = _a.name, rest = __rest$1(_a, ["id", "checked", "onChange", "color", "label", "disabled", "className", "name"]);
108
120
  var _e = React.useState(checked), localChecked = _e[0], setChecked = _e[1];
109
121
  function onChangeInput(event) {
110
122
  setChecked(!localChecked);
111
123
  onChange(event);
112
124
  }
113
- return (React.createElement("label", __assign$1({}, rest, { className: clsx(rest.className, 'relative flex items-center') }),
114
- React.createElement("input", { id: id, className: "absolute inset-0 w-0 h-0 opacity-0", "aria-describedby": "".concat(id, "-description"), name: id, type: "checkbox", onChange: onChangeInput, disabled: disabled, checked: localChecked }),
125
+ return (React.createElement("div", __assign$1({ className: clsx(className, 'relative flex items-center') }, rest),
126
+ React.createElement("input", { id: id, className: "absolute inset-0 w-0 h-0 opacity-0", "aria-describedby": id + "-description", name: name || id, type: "checkbox", onChange: onChangeInput, disabled: disabled, checked: localChecked }),
115
127
  localChecked && (React.createElement(IconCheckmarkSmall, { strokeColor: "white", className: "absolute" })),
116
128
  React.createElement("span", { className: clsx([
117
129
  "block border-1 rounded h-16px w-16px flex items-center text-white",
@@ -125,11 +137,13 @@ var Checkbox = function (_a) {
125
137
  }
126
138
  : 'border-gray-200 bg-white',
127
139
  ]) }),
128
- label && (React.createElement("span", { className: clsx([
140
+ React.createElement("label", { htmlFor: id }, label && (React.createElement("span", { className: clsx([
129
141
  disabled ? 'text-gray-500' : 'text-gray-800',
130
142
  'block ml-2 text-16px leading-normal font-light select-none',
131
- ]) }, label))));
143
+ ]) }, label)))));
132
144
  };
133
145
 
134
146
  export { Checkbox as default };
147
+ /* <windicss-keep class="relative flex items-center absolute inset-0 w-0 h-0 opacity-0 block border-1 rounded h-16px w-16px text-white border-gray-200 bg-gray-100 border-indigo-500 bg-indigo-400 border-jade-500 bg-jade-400 border-red-500 bg-red-400 bg-white text-gray-500 text-gray-800 ml-2 text-16px leading-normal font-light select-none"> */
148
+
135
149
  //# sourceMappingURL=index.es.js.map