@cypress-design/react-checkbox 0.1.0 → 0.2.2

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,31 @@
1
1
  # @cypress-design/react-checkbox
2
2
 
3
+ ## 0.2.2
4
+
5
+ ### Patch Changes
6
+
7
+ - [`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
8
+
9
+ ## 0.2.1
10
+
11
+ ### Patch Changes
12
+
13
+ - [#39](https://github.com/cypress-io/cypress-design/pull/39) [`1b383f3`](https://github.com/cypress-io/cypress-design/commit/1b383f3d149948bf2cc062d3baa17d5ce032f07e) Thanks [@elevatebart](https://github.com/elevatebart)! - fix file path of sourcemaps
14
+
15
+ - Updated dependencies [[`4259026`](https://github.com/cypress-io/cypress-design/commit/4259026314464260e89bcd88690c8a60ad2f0459), [`1b383f3`](https://github.com/cypress-io/cypress-design/commit/1b383f3d149948bf2cc062d3baa17d5ce032f07e)]:
16
+ - @cypress-design/react-icon@0.3.0
17
+
18
+ ## 0.2.0
19
+
20
+ ### Minor Changes
21
+
22
+ - [#33](https://github.com/cypress-io/cypress-design/pull/33) [`5f9cf10`](https://github.com/cypress-io/cypress-design/commit/5f9cf10ff4709fcd7d322c2dc5dbc676473b433e) Thanks [@elevatebart](https://github.com/elevatebart)! - fix design and api of checkboxes
23
+
24
+ ### Patch Changes
25
+
26
+ - Updated dependencies []:
27
+ - @cypress-design/react-icon@0.2.1
28
+
3
29
  ## 0.1.0
4
30
 
5
31
  ### Minor Changes
@@ -1,23 +1,67 @@
1
- import { ArgsTable, Canvas, Meta, Story } from '@storybook/addon-docs'
1
+ import { ArgsTable, Canvas, Meta, Story, Description } from '@storybook/addon-docs'
2
+ import { version } from "./package.json"
3
+ import changelog from "./CHANGELOG.md"
2
4
 
3
5
  import { Checkbox } from './Checkbox'
4
6
 
5
- # Checkbox
7
+ <h1>Checkbox<span className="text-lg font-normal"> - v{version}</span></h1>
6
8
 
7
- <Meta title="Checkbox" component={Checkbox} />
9
+ <Meta title="Checkbox" component={Checkbox}
10
+ argTypes={{
11
+ color: {
12
+ control: {
13
+ type: 'select',
14
+ },
15
+ },
16
+ }}
17
+ parameters={{
18
+ design: {
19
+ type: "figma",
20
+ url: 'https://www.figma.com/file/1WJ3GVQyMV5e7xVxPg3yID/Design-System?node-id=1027%3A9825',
21
+ }
22
+ }} />
8
23
 
9
- <Canvas>
24
+ <Canvas withSource="none">
10
25
  <Story name="Checkbox">
11
- <>
12
- <Checkbox modelValue={true} label="Checked" />
26
+ {args =>
27
+ (<>
28
+ <Checkbox {...args} checked label="Checked" />
13
29
  <br />
14
- <Checkbox modelValue={false} label="Unchecked" />
30
+ <Checkbox {...args} label="Unchecked" />
15
31
  <br />
16
- <Checkbox modelValue={true} disabled label="Disabled & checked" />
32
+ <Checkbox {...args} checked disabled label="Disabled & checked" />
17
33
  <br />
18
- <Checkbox modelValue={false} disabled label="Disabled & unchecked" />
19
- </>
34
+ <Checkbox {...args} disabled label="Disabled & unchecked" />
35
+ </>)}
20
36
  </Story>
21
37
  </Canvas>
22
38
 
23
39
  <ArgsTable />
40
+
41
+ ## Usage
42
+
43
+ Install the component
44
+
45
+ ```
46
+ yarn add @cypress-design/react-checkbox
47
+ ```
48
+
49
+ In the script, import the component
50
+
51
+ ```js
52
+ import Checkbox from '@cypress-design/react-checkbox'
53
+ ```
54
+
55
+ then in your render function, use it
56
+
57
+ ```jsx
58
+ export const MyComp = () => {
59
+ return (<div>
60
+ <Checkbox checked={checked} label="Checked" color="indigo" onChange={setChecked}/>
61
+ </div>)
62
+ }
63
+ ```
64
+
65
+ <Description>
66
+ {changelog.replace(/^# .+/, '## Changelog')}
67
+ </Description>
package/Checkbox.tsx CHANGED
@@ -1,60 +1,79 @@
1
- import * as React from 'react';
2
- import type { FunctionComponent, HTMLProps, ReactNode } from 'react';
1
+ import clsx from 'clsx'
2
+ import * as React from 'react'
3
+ import type { FunctionComponent, HTMLProps, ReactNode } from 'react'
4
+ import { IconCheckmarkSmall } from '@cypress-design/react-icon'
3
5
 
4
6
  export interface CheckboxProps
5
- extends Omit<HTMLProps<HTMLDivElement>, 'label'> {
6
- id?: string;
7
- modelValue: boolean;
8
- onChange: () => void;
9
- state?: 'success' | 'danger' | 'default';
10
- label?: ReactNode;
7
+ extends Omit<HTMLProps<HTMLLabelElement>, 'label' | 'onChange'> {
8
+ id?: string
9
+ checked: boolean
10
+ onChange: (event: React.ChangeEvent<HTMLInputElement>) => void
11
+ color?: 'indigo' | 'red' | 'jade'
12
+ label?: ReactNode
13
+ disabled?: boolean
11
14
  }
12
15
 
13
- // TODO: how do you disable it? does it work?
14
16
  export const Checkbox: FunctionComponent<CheckboxProps> = ({
17
+ // @ts-ignore
15
18
  id = crypto.randomUUID(),
16
- modelValue,
19
+ checked = false,
17
20
  onChange,
18
- state = 'default',
21
+ color = 'indigo',
19
22
  label,
23
+ disabled,
20
24
  ...rest
21
25
  }) => {
26
+ const [localChecked, setChecked] = React.useState(checked)
27
+
28
+ function onChangeInput(event: React.ChangeEvent<HTMLInputElement>) {
29
+ setChecked(!localChecked)
30
+ onChange(event)
31
+ }
32
+
22
33
  return (
23
- <div {...rest} className="relative flex items-center">
24
- <div className="flex items-center h-5">
25
- <input
26
- id={id}
27
- defaultChecked={modelValue}
28
- aria-describedby={`${id}-description`}
29
- name={id}
30
- type="checkbox"
31
- className={`border-1 rounded border-gray-200 bg-white h-4 w-4 text-indigo-500 disabled:bg-gray-100 checked:bg-indigo-500 ${
32
- state === 'default'
33
- ? 'text-indigo-500 checked:border-indigo-300 checked:bg-indigo-600 checked:text-indigo-600'
34
- : ''
35
- } ${
36
- state === 'success'
37
- ? 'checked:border-jade-300 checked:bg-jade-600 checked:text-jade-600'
38
- : ''
39
- } ${
40
- state === 'danger'
41
- ? 'checked:border-red-300 checked:bg-red-600 checked:text-red-600'
42
- : ''
43
- }`}
44
- onChange={onChange}
45
- />
46
- </div>
47
- <div className="ml-2 text-16px leading-normal">
48
- <label
49
- v-if="label"
50
- htmlFor={id}
51
- className="disabled:text-gray-500 text-gray-800 font-light select-none"
34
+ <label
35
+ {...rest}
36
+ className={clsx(rest.className, 'relative flex items-center')}
37
+ >
38
+ <input
39
+ id={id}
40
+ className="absolute inset-0 w-0 h-0 opacity-0"
41
+ aria-describedby={`${id}-description`}
42
+ name={id}
43
+ type="checkbox"
44
+ onChange={onChangeInput}
45
+ disabled={disabled}
46
+ checked={localChecked}
47
+ />
48
+ {localChecked && (
49
+ <IconCheckmarkSmall strokeColor="white" className="absolute" />
50
+ )}
51
+ <span
52
+ className={clsx([
53
+ `block border-1 rounded h-16px w-16px flex items-center text-white`,
54
+ disabled
55
+ ? 'border-gray-200 bg-gray-100'
56
+ : localChecked
57
+ ? {
58
+ 'border-indigo-500 bg-indigo-400': color === 'indigo',
59
+ 'border-jade-500 bg-jade-400': color === 'jade',
60
+ 'border-red-500 bg-red-400': color === 'red',
61
+ }
62
+ : 'border-gray-200 bg-white',
63
+ ])}
64
+ />
65
+ {label && (
66
+ <span
67
+ className={clsx([
68
+ disabled ? 'text-gray-500' : 'text-gray-800',
69
+ 'block ml-2 text-16px leading-normal font-light select-none',
70
+ ])}
52
71
  >
53
72
  {label}
54
- </label>
55
- </div>
56
- </div>
57
- );
58
- };
73
+ </span>
74
+ )}
75
+ </label>
76
+ )
77
+ }
59
78
 
60
- export default Checkbox;
79
+ export default Checkbox
@@ -0,0 +1,23 @@
1
+ import * as React from 'react'
2
+ import { mount } from 'cypress/react'
3
+ import { Checkbox } from './Checkbox'
4
+
5
+ describe('Checkbox', () => {
6
+ it('renders', () => {
7
+ let isChecked = true
8
+
9
+ mount(
10
+ <Checkbox
11
+ label="Welcome guide settings"
12
+ id="welcome-opt-out"
13
+ checked={isChecked}
14
+ onChange={() => (isChecked = !isChecked)}
15
+ className="m-2 px-2 py-1 border-1 border-gray-300 rounded"
16
+ />
17
+ )
18
+
19
+ cy.get('input[type="checkbox"]').should('be.checked')
20
+ cy.contains('Welcome guide settings').click()
21
+ cy.get('input[type="checkbox"]').should('not.be.checked')
22
+ })
23
+ })
@@ -1,10 +1,12 @@
1
+ import * as React from 'react';
1
2
  import type { FunctionComponent, HTMLProps, ReactNode } from 'react';
2
- export interface CheckboxProps extends Omit<HTMLProps<HTMLDivElement>, 'label'> {
3
+ export interface CheckboxProps extends Omit<HTMLProps<HTMLLabelElement>, 'label' | 'onChange'> {
3
4
  id?: string;
4
- modelValue: boolean;
5
- onChange: () => void;
6
- state?: 'success' | 'danger' | 'default';
5
+ checked: boolean;
6
+ onChange: (event: React.ChangeEvent<HTMLInputElement>) => void;
7
+ color?: 'indigo' | 'red' | 'jade';
7
8
  label?: ReactNode;
9
+ disabled?: boolean;
8
10
  }
9
11
  export declare const Checkbox: FunctionComponent<CheckboxProps>;
10
12
  export default Checkbox;
@@ -1 +1 @@
1
- {"version":3,"file":"Checkbox.d.ts","sourceRoot":"","sources":["../Checkbox.tsx"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,iBAAiB,EAAE,SAAS,EAAE,SAAS,EAAE,MAAM,OAAO,CAAC;AAErE,MAAM,WAAW,aACf,SAAQ,IAAI,CAAC,SAAS,CAAC,cAAc,CAAC,EAAE,OAAO,CAAC;IAChD,EAAE,CAAC,EAAE,MAAM,CAAC;IACZ,UAAU,EAAE,OAAO,CAAC;IACpB,QAAQ,EAAE,MAAM,IAAI,CAAC;IACrB,KAAK,CAAC,EAAE,SAAS,GAAG,QAAQ,GAAG,SAAS,CAAC;IACzC,KAAK,CAAC,EAAE,SAAS,CAAC;CACnB;AAGD,eAAO,MAAM,QAAQ,EAAE,iBAAiB,CAAC,aAAa,CA4CrD,CAAC;AAEF,eAAe,QAAQ,CAAC"}
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,CA6DrD,CAAA;AAED,eAAe,QAAQ,CAAA"}
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,YAAY,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,YAAY,CAAA"}
package/dist/index.es.js CHANGED
@@ -1,4 +1,44 @@
1
+ import clsx from 'clsx';
1
2
  import * as React from 'react';
3
+ import * as iconsRegistry from '@cypress-design/icon-registry';
4
+
5
+ /******************************************************************************
6
+ Copyright (c) Microsoft Corporation.
7
+
8
+ Permission to use, copy, modify, and/or distribute this software for any
9
+ purpose with or without fee is hereby granted.
10
+
11
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
12
+ REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
13
+ AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
14
+ INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15
+ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
16
+ OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17
+ PERFORMANCE OF THIS SOFTWARE.
18
+ ***************************************************************************** */
19
+
20
+ var __assign$1 = function() {
21
+ __assign$1 = Object.assign || function __assign(t) {
22
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
23
+ s = arguments[i];
24
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
25
+ }
26
+ return t;
27
+ };
28
+ return __assign$1.apply(this, arguments);
29
+ };
30
+
31
+ function __rest$1(s, e) {
32
+ var t = {};
33
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
34
+ t[p] = s[p];
35
+ if (s != null && typeof Object.getOwnPropertySymbols === "function")
36
+ for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
37
+ if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
38
+ t[p[i]] = s[p[i]];
39
+ }
40
+ return t;
41
+ }
2
42
 
3
43
  /******************************************************************************
4
44
  Copyright (c) Microsoft Corporation.
@@ -38,20 +78,61 @@ function __rest(s, e) {
38
78
  return t;
39
79
  }
40
80
 
41
- // TODO: how do you disable it? does it work?
81
+ 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 componentProps = __assign({ width: size, height: size, fill: 'none', dangerouslySetInnerHTML: {
84
+ __html: body
85
+ } }, attributes);
86
+ if (attributes.className) {
87
+ compiledClasses.push(attributes.className);
88
+ }
89
+ if (compiledClasses.length) {
90
+ componentProps.className = compiledClasses.join(' ');
91
+ }
92
+ return componentProps;
93
+ };
94
+ var IconCheckmarkSmall = function (props) {
95
+ var _a = iconsRegistry.getComponentAttributes(__assign(__assign({}, props), { availableSizes: ["16"] })), size = _a.sizeWithDefault, compiledClasses = _a.compiledClasses;
96
+ var iconBodies = {
97
+ "16": "<path d=\"M5 8.5 7.5 11 11 5\" stroke=\"currentColor\" class=\"icon-dark\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\"/>"
98
+ };
99
+ var body = iconBodies[size];
100
+ if (!body) {
101
+ throw Error("Icon \"checkmark-small\" is not available in size " + size);
102
+ }
103
+ return React.createElement('svg', compileReactIconProperties(__assign(__assign({}, props), { size: size, body: body, compiledClasses: compiledClasses })));
104
+ };
105
+
42
106
  var Checkbox = function (_a) {
43
- var _b = _a.id, id = _b === void 0 ? crypto.randomUUID() : _b, modelValue = _a.modelValue, onChange = _a.onChange, _c = _a.state, state = _c === void 0 ? 'default' : _c, label = _a.label, rest = __rest(_a, ["id", "modelValue", "onChange", "state", "label"]);
44
- return (React.createElement("div", __assign({}, rest, { className: "relative flex items-center" }),
45
- React.createElement("div", { className: "flex items-center h-5" },
46
- React.createElement("input", { id: id, defaultChecked: modelValue, "aria-describedby": "".concat(id, "-description"), name: id, type: "checkbox", className: "border-1 rounded border-gray-200 bg-white h-4 w-4 text-indigo-500 disabled:bg-gray-100 checked:bg-indigo-500 ".concat(state === 'default'
47
- ? 'text-indigo-500 checked:border-indigo-300 checked:bg-indigo-600 checked:text-indigo-600'
48
- : '', " ").concat(state === 'success'
49
- ? 'checked:border-jade-300 checked:bg-jade-600 checked:text-jade-600'
50
- : '', " ").concat(state === 'danger'
51
- ? 'checked:border-red-300 checked:bg-red-600 checked:text-red-600'
52
- : ''), onChange: onChange })),
53
- React.createElement("div", { className: "ml-2 text-16px leading-normal" },
54
- React.createElement("label", { "v-if": "label", htmlFor: id, className: "disabled:text-gray-500 text-gray-800 font-light select-none" }, label))));
107
+ var
108
+ // @ts-ignore
109
+ _b = _a.id,
110
+ // @ts-ignore
111
+ 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"]);
112
+ var _e = React.useState(checked), localChecked = _e[0], setChecked = _e[1];
113
+ function onChangeInput(event) {
114
+ setChecked(!localChecked);
115
+ onChange(event);
116
+ }
117
+ return (React.createElement("label", __assign$1({}, rest, { className: clsx(rest.className, 'relative flex items-center') }),
118
+ React.createElement("input", { id: id, className: "absolute inset-0 w-0 h-0 opacity-0", "aria-describedby": id + "-description", name: id, type: "checkbox", onChange: onChangeInput, disabled: disabled, checked: localChecked }),
119
+ localChecked && (React.createElement(IconCheckmarkSmall, { strokeColor: "white", className: "absolute" })),
120
+ React.createElement("span", { className: clsx([
121
+ "block border-1 rounded h-16px w-16px flex items-center text-white",
122
+ disabled
123
+ ? 'border-gray-200 bg-gray-100'
124
+ : localChecked
125
+ ? {
126
+ 'border-indigo-500 bg-indigo-400': color === 'indigo',
127
+ 'border-jade-500 bg-jade-400': color === 'jade',
128
+ 'border-red-500 bg-red-400': color === 'red'
129
+ }
130
+ : 'border-gray-200 bg-white',
131
+ ]) }),
132
+ label && (React.createElement("span", { className: clsx([
133
+ disabled ? 'text-gray-500' : 'text-gray-800',
134
+ 'block ml-2 text-16px leading-normal font-light select-none',
135
+ ]) }, label))));
55
136
  };
56
137
 
57
138
  export { Checkbox as default };