@contentful/field-editor-checkbox 1.2.0 → 1.3.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.
@@ -0,0 +1,176 @@
1
+ import * as React from 'react';
2
+ import { createFakeFieldAPI, createFakeLocalesAPI } from '@contentful/field-editor-test-utils';
3
+ import '@testing-library/jest-dom/extend-expect';
4
+ import { cleanup, configure, fireEvent, render } from '@testing-library/react';
5
+ import { CheckboxEditor } from './CheckboxEditor';
6
+ configure({
7
+ testIdAttribute: 'data-test-id'
8
+ });
9
+ describe('CheckboxEditor', ()=>{
10
+ afterEach(cleanup);
11
+ it('renders a warning if no options are present', ()=>{
12
+ const [field] = createFakeFieldAPI((mock)=>{
13
+ return {
14
+ ...mock,
15
+ items: {
16
+ type: '',
17
+ validations: []
18
+ }
19
+ };
20
+ });
21
+ const { getByTestId , queryByTestId } = render(React.createElement(CheckboxEditor, {
22
+ field: field,
23
+ locales: createFakeLocalesAPI(),
24
+ isInitiallyDisabled: false
25
+ }));
26
+ expect(getByTestId('predefined-values-warning')).toBeInTheDocument();
27
+ expect(queryByTestId('dropdown-editor')).not.toBeInTheDocument();
28
+ });
29
+ it('renders checkboxes for predefined values', ()=>{
30
+ const predefined = [
31
+ 'banana',
32
+ 'orange',
33
+ 'strawberry'
34
+ ];
35
+ const [field] = createFakeFieldAPI((mock)=>{
36
+ return {
37
+ ...mock,
38
+ items: {
39
+ type: '',
40
+ validations: [
41
+ {
42
+ in: predefined
43
+ }
44
+ ]
45
+ }
46
+ };
47
+ });
48
+ const { container } = render(React.createElement(CheckboxEditor, {
49
+ field: field,
50
+ locales: createFakeLocalesAPI(),
51
+ isInitiallyDisabled: false
52
+ }));
53
+ const $inputs = container.querySelectorAll('input[type="checkbox"]');
54
+ expect($inputs).toHaveLength(3);
55
+ predefined.forEach((item, index)=>{
56
+ expect($inputs[index].value).toEqual(item);
57
+ });
58
+ });
59
+ it('calls setValue for every check event and removeValue if all items are unclicked', ()=>{
60
+ const predefined = [
61
+ 'banana',
62
+ 'orange',
63
+ 'strawberry'
64
+ ];
65
+ const [field] = createFakeFieldAPI((mock)=>{
66
+ jest.spyOn(mock, 'setValue');
67
+ jest.spyOn(mock, 'removeValue');
68
+ return {
69
+ ...mock,
70
+ items: {
71
+ type: '',
72
+ validations: [
73
+ {
74
+ in: predefined
75
+ }
76
+ ]
77
+ }
78
+ };
79
+ });
80
+ const { container } = render(React.createElement(CheckboxEditor, {
81
+ field: field,
82
+ locales: createFakeLocalesAPI(),
83
+ isInitiallyDisabled: false
84
+ }));
85
+ const $inputs = container.querySelectorAll('input[type="checkbox"]');
86
+ fireEvent.click($inputs[0]);
87
+ expect(field.setValue).toHaveBeenCalledWith([
88
+ predefined[0]
89
+ ]);
90
+ expect(field.setValue).toHaveBeenCalledTimes(1);
91
+ fireEvent.click($inputs[2]);
92
+ expect(field.setValue).toHaveBeenCalledWith([
93
+ predefined[0],
94
+ predefined[2]
95
+ ]);
96
+ expect(field.setValue).toHaveBeenCalledTimes(2);
97
+ fireEvent.click($inputs[1]);
98
+ expect(field.setValue).toHaveBeenCalledWith([
99
+ predefined[0],
100
+ predefined[2],
101
+ predefined[1]
102
+ ]);
103
+ expect(field.setValue).toHaveBeenCalledTimes(3);
104
+ $inputs.forEach(($input)=>{
105
+ fireEvent.click($input);
106
+ });
107
+ expect(field.removeValue).toHaveBeenCalledTimes(1);
108
+ });
109
+ it('renders invalid text and remove link when value set is not in predefined values', ()=>{
110
+ const predefined = [
111
+ 'banana',
112
+ 'orange',
113
+ 'strawberry'
114
+ ];
115
+ const [field] = createFakeFieldAPI((mock)=>{
116
+ jest.spyOn(mock, 'setValue');
117
+ jest.spyOn(mock, 'removeValue');
118
+ return {
119
+ ...mock,
120
+ items: {
121
+ type: '',
122
+ validations: [
123
+ {
124
+ in: predefined
125
+ }
126
+ ]
127
+ }
128
+ };
129
+ });
130
+ field.setValue([
131
+ 'mango'
132
+ ]);
133
+ const { getByTestId } = render(React.createElement(CheckboxEditor, {
134
+ field: field,
135
+ locales: createFakeLocalesAPI(),
136
+ isInitiallyDisabled: false
137
+ }));
138
+ expect(getByTestId('invalid-text')).toBeInTheDocument();
139
+ expect(getByTestId('cf-ui-text-link')).toBeInTheDocument();
140
+ fireEvent.click(getByTestId('cf-ui-text-link'));
141
+ expect(field.removeValue).toHaveBeenCalledTimes(1);
142
+ });
143
+ it('renders checkboxes with unique ids', async ()=>{
144
+ const predefined = [
145
+ 'banana',
146
+ 'orange',
147
+ 'strawberry'
148
+ ];
149
+ const [field] = createFakeFieldAPI((mock)=>{
150
+ return {
151
+ ...mock,
152
+ items: {
153
+ type: '',
154
+ validations: [
155
+ {
156
+ in: predefined
157
+ }
158
+ ]
159
+ }
160
+ };
161
+ });
162
+ const locales = createFakeLocalesAPI();
163
+ const { findAllByTestId } = render(React.createElement("div", null, React.createElement(CheckboxEditor, {
164
+ field: field,
165
+ locales: locales,
166
+ isInitiallyDisabled: false
167
+ }), React.createElement(CheckboxEditor, {
168
+ field: field,
169
+ locales: locales,
170
+ isInitiallyDisabled: false
171
+ })));
172
+ const $labels = await findAllByTestId('cf-ui-checkbox');
173
+ const uniqueIds = Array.from(new Set($labels.map((label)=>label.getAttribute('for'))));
174
+ expect(uniqueIds).toHaveLength(6);
175
+ });
176
+ });
@@ -0,0 +1 @@
1
+ export { CheckboxEditor } from './CheckboxEditor';
@@ -0,0 +1,15 @@
1
+ import tokens from '@contentful/f36-tokens';
2
+ import { css } from 'emotion';
3
+ export const form = css({
4
+ marginTop: tokens.spacingS
5
+ });
6
+ export const rightToLeft = css({
7
+ direction: 'rtl'
8
+ });
9
+ export const invalidText = css({
10
+ color: tokens.red500,
11
+ marginLeft: tokens.spacing2Xs
12
+ });
13
+ export const removeBtn = css({
14
+ marginLeft: tokens.spacingL
15
+ });
@@ -1,22 +1,22 @@
1
- /// <reference types="react" />
2
- import { FieldAPI, LocalesAPI } from '@contentful/field-editor-shared';
3
- export interface CheckboxEditorProps {
4
- /**
5
- * is the field disabled initially
6
- */
7
- isInitiallyDisabled: boolean;
8
- /**
9
- * sdk.field
10
- */
11
- field: FieldAPI;
12
- /**
13
- * sdk.locales
14
- */
15
- locales: LocalesAPI;
16
- }
17
- export declare function CheckboxEditor(props: CheckboxEditorProps): JSX.Element;
18
- export declare namespace CheckboxEditor {
19
- var defaultProps: {
20
- isInitiallyDisabled: boolean;
21
- };
22
- }
1
+ import * as React from 'react';
2
+ import { FieldAPI, LocalesAPI } from '@contentful/field-editor-shared';
3
+ export interface CheckboxEditorProps {
4
+ /**
5
+ * is the field disabled initially
6
+ */
7
+ isInitiallyDisabled: boolean;
8
+ /**
9
+ * sdk.field
10
+ */
11
+ field: FieldAPI;
12
+ /**
13
+ * sdk.locales
14
+ */
15
+ locales: LocalesAPI;
16
+ }
17
+ export declare function CheckboxEditor(props: CheckboxEditorProps): React.JSX.Element;
18
+ export declare namespace CheckboxEditor {
19
+ var defaultProps: {
20
+ isInitiallyDisabled: boolean;
21
+ };
22
+ }
@@ -0,0 +1 @@
1
+ import '@testing-library/jest-dom/extend-expect';
@@ -1 +1 @@
1
- export { CheckboxEditor } from './CheckboxEditor';
1
+ export { CheckboxEditor } from './CheckboxEditor';
@@ -1,4 +1,4 @@
1
- export declare const form: string;
2
- export declare const rightToLeft: string;
3
- export declare const invalidText: string;
4
- export declare const removeBtn: string;
1
+ export declare const form: string;
2
+ export declare const rightToLeft: string;
3
+ export declare const invalidText: string;
4
+ export declare const removeBtn: string;
package/package.json CHANGED
@@ -1,9 +1,17 @@
1
1
  {
2
2
  "name": "@contentful/field-editor-checkbox",
3
- "version": "1.2.0",
4
- "main": "dist/index.js",
5
- "module": "dist/field-editor-checkbox.esm.js",
6
- "typings": "dist/index.d.ts",
3
+ "version": "1.3.0",
4
+ "main": "dist/cjs/index.js",
5
+ "module": "dist/esm/index.js",
6
+ "types": "dist/types/index.d.ts",
7
+ "exports": {
8
+ ".": {
9
+ "types": "./dist/types/index.d.ts",
10
+ "require": "./dist/cjs/index.js",
11
+ "default": "./dist/cjs/index.js"
12
+ },
13
+ "./package.json": "./package.json"
14
+ },
7
15
  "files": [
8
16
  "dist"
9
17
  ],
@@ -14,25 +22,31 @@
14
22
  "url": "https://github.com/contentful/field-editors"
15
23
  },
16
24
  "scripts": {
17
- "watch": "tsdx watch",
18
- "build": "tsdx build",
19
- "test": "tsdx test --env=jsdom --watch",
20
- "test:ci": "tsdx test --env=jsdom --ci",
25
+ "watch": "yarn concurrently \"yarn:watch:*\"",
26
+ "watch:cjs": "yarn build:cjs -w",
27
+ "watch:esm": "yarn build:esm -w",
28
+ "watch:types": "yarn build:types --watch",
29
+ "build": "yarn build:types && yarn build:cjs && yarn build:esm",
30
+ "build:types": "tsc --outDir dist/types --emitDeclarationOnly",
31
+ "build:cjs": "swc src --config-file ../../.swcrc -d dist/cjs -C module.type=commonjs",
32
+ "build:esm": "swc src --config-file ../../.swcrc -d dist/esm",
33
+ "test": "jest --watch",
34
+ "test:ci": "jest --ci",
21
35
  "tsc": "tsc -p ./ --noEmit"
22
36
  },
23
37
  "dependencies": {
24
38
  "@contentful/f36-components": "^4.3.23",
25
39
  "@contentful/f36-tokens": "^4.0.0",
26
- "@contentful/field-editor-shared": "^1.2.0",
40
+ "@contentful/field-editor-shared": "^1.3.0",
27
41
  "emotion": "^10.0.17",
28
42
  "lodash": "^4.17.15",
29
43
  "lodash-es": "^4.17.15"
30
44
  },
31
45
  "devDependencies": {
32
- "@contentful/field-editor-test-utils": "^1.3.0"
46
+ "@contentful/field-editor-test-utils": "^1.4.0"
33
47
  },
34
48
  "peerDependencies": {
35
49
  "react": ">=16.8.0"
36
50
  },
37
- "gitHead": "de7e74e3485dd69c240cfe9c545e6e50e41fb295"
51
+ "gitHead": "543e02672a8dd4edc810f9f3568d6b69c454e1f9"
38
52
  }
package/CHANGELOG.md DELETED
@@ -1,204 +0,0 @@
1
- # Change Log
2
-
3
- All notable changes to this project will be documented in this file.
4
- See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5
-
6
- # [1.2.0](https://github.com/contentful/field-editors/compare/@contentful/field-editor-checkbox@1.1.12...@contentful/field-editor-checkbox@1.2.0) (2023-04-19)
7
-
8
- ### Features
9
-
10
- - upgrade cypress [TOL-1036] ([#1391](https://github.com/contentful/field-editors/issues/1391)) ([9c1aec9](https://github.com/contentful/field-editors/commit/9c1aec98aabbe464cdc3f1236c3bb1cc29b8208d))
11
-
12
- ## [1.1.12](https://github.com/contentful/field-editors/compare/@contentful/field-editor-checkbox@1.1.11...@contentful/field-editor-checkbox@1.1.12) (2023-03-14)
13
-
14
- **Note:** Version bump only for package @contentful/field-editor-checkbox
15
-
16
- ## [1.1.11](https://github.com/contentful/field-editors/compare/@contentful/field-editor-checkbox@1.1.10...@contentful/field-editor-checkbox@1.1.11) (2023-03-10)
17
-
18
- **Note:** Version bump only for package @contentful/field-editor-checkbox
19
-
20
- ## [1.1.10](https://github.com/contentful/field-editors/compare/@contentful/field-editor-checkbox@1.1.9...@contentful/field-editor-checkbox@1.1.10) (2023-02-21)
21
-
22
- **Note:** Version bump only for package @contentful/field-editor-checkbox
23
-
24
- ## [1.1.9](https://github.com/contentful/field-editors/compare/@contentful/field-editor-checkbox@1.1.8...@contentful/field-editor-checkbox@1.1.9) (2023-02-07)
25
-
26
- **Note:** Version bump only for package @contentful/field-editor-checkbox
27
-
28
- ## [1.1.8](https://github.com/contentful/field-editors/compare/@contentful/field-editor-checkbox@1.1.7...@contentful/field-editor-checkbox@1.1.8) (2022-12-08)
29
-
30
- **Note:** Version bump only for package @contentful/field-editor-checkbox
31
-
32
- ## [1.1.7](https://github.com/contentful/field-editors/compare/@contentful/field-editor-checkbox@1.1.6...@contentful/field-editor-checkbox@1.1.7) (2022-12-06)
33
-
34
- ### Bug Fixes
35
-
36
- - clickable area of checkbox was too big ([#1301](https://github.com/contentful/field-editors/issues/1301)) ([696713b](https://github.com/contentful/field-editors/commit/696713b55670f77ea4afa6f7cfc7cb8217d74552))
37
-
38
- ## [1.1.6](https://github.com/contentful/field-editors/compare/@contentful/field-editor-checkbox@1.1.5...@contentful/field-editor-checkbox@1.1.6) (2022-07-29)
39
-
40
- **Note:** Version bump only for package @contentful/field-editor-checkbox
41
-
42
- ## [1.1.5](https://github.com/contentful/field-editors/compare/@contentful/field-editor-checkbox@1.1.4...@contentful/field-editor-checkbox@1.1.5) (2022-07-29)
43
-
44
- **Note:** Version bump only for package @contentful/field-editor-checkbox
45
-
46
- ## [1.1.4](https://github.com/contentful/field-editors/compare/@contentful/field-editor-checkbox@1.1.3...@contentful/field-editor-checkbox@1.1.4) (2022-07-11)
47
-
48
- **Note:** Version bump only for package @contentful/field-editor-checkbox
49
-
50
- ## [1.1.3](https://github.com/contentful/field-editors/compare/@contentful/field-editor-checkbox@1.1.2...@contentful/field-editor-checkbox@1.1.3) (2022-06-22)
51
-
52
- **Note:** Version bump only for package @contentful/field-editor-checkbox
53
-
54
- ## [1.1.2](https://github.com/contentful/field-editors/compare/@contentful/field-editor-checkbox@1.1.1...@contentful/field-editor-checkbox@1.1.2) (2022-02-15)
55
-
56
- ### Bug Fixes
57
-
58
- - bump f36 packages ([#1025](https://github.com/contentful/field-editors/issues/1025)) ([ec37a40](https://github.com/contentful/field-editors/commit/ec37a4000db7cd75c66dd9621136b2272c9feeea))
59
-
60
- ## [1.1.1](https://github.com/contentful/field-editors/compare/@contentful/field-editor-checkbox@1.1.0...@contentful/field-editor-checkbox@1.1.1) (2022-02-14)
61
-
62
- **Note:** Version bump only for package @contentful/field-editor-checkbox
63
-
64
- # [1.1.0](https://github.com/contentful/field-editors/compare/@contentful/field-editor-checkbox@1.0.3...@contentful/field-editor-checkbox@1.1.0) (2022-01-11)
65
-
66
- ### Features
67
-
68
- - bump f36 packages to stable v4 [BAU-521] ([#988](https://github.com/contentful/field-editors/issues/988)) ([419cf56](https://github.com/contentful/field-editors/commit/419cf56692179b074fcfa2743469d5265ed98429))
69
-
70
- ## [1.0.3](https://github.com/contentful/field-editors/compare/@contentful/field-editor-checkbox@1.0.2...@contentful/field-editor-checkbox@1.0.3) (2021-12-23)
71
-
72
- ### Bug Fixes
73
-
74
- - markdown buttons ([#968](https://github.com/contentful/field-editors/issues/968)) ([9803b98](https://github.com/contentful/field-editors/commit/9803b98c25d92df6148686ffe2749a77f7efdbb9))
75
-
76
- ## [1.0.2](https://github.com/contentful/field-editors/compare/@contentful/field-editor-checkbox@1.0.1...@contentful/field-editor-checkbox@1.0.2) (2021-12-20)
77
-
78
- **Note:** Version bump only for package @contentful/field-editor-checkbox
79
-
80
- ## [1.0.1](https://github.com/contentful/field-editors/compare/@contentful/field-editor-checkbox@1.0.0...@contentful/field-editor-checkbox@1.0.1) (2021-11-17)
81
-
82
- ### Bug Fixes
83
-
84
- - **card-actions:** update forma 36 to fix card actions click issue ([#927](https://github.com/contentful/field-editors/issues/927)) ([3dfdef2](https://github.com/contentful/field-editors/commit/3dfdef2c2b0045f12ea94ddafca89a8e9f25e7d0))
85
-
86
- # [1.0.0](https://github.com/contentful/field-editors/compare/@contentful/field-editor-checkbox@0.16.0...@contentful/field-editor-checkbox@1.0.0) (2021-11-04)
87
-
88
- **Note:** Version bump only for package @contentful/field-editor-checkbox
89
-
90
- # [0.16.0](https://github.com/contentful/field-editors/compare/@contentful/field-editor-checkbox@0.15.6...@contentful/field-editor-checkbox@0.16.0) (2021-11-04)
91
-
92
- ### Features
93
-
94
- - Forma v4 components adoption ([#805](https://github.com/contentful/field-editors/issues/805)) ([526bde6](https://github.com/contentful/field-editors/commit/526bde6e10e0ee3789705ec10fb31489af7ca59e))
95
-
96
- ### BREAKING CHANGES
97
-
98
- - adopts a new Forma v4 beta
99
-
100
- ## [0.15.6](https://github.com/contentful/field-editors/compare/@contentful/field-editor-checkbox@0.15.5...@contentful/field-editor-checkbox@0.15.6) (2021-10-14)
101
-
102
- **Note:** Version bump only for package @contentful/field-editor-checkbox
103
-
104
- ## [0.15.5](https://github.com/contentful/field-editors/compare/@contentful/field-editor-checkbox@0.15.4...@contentful/field-editor-checkbox@0.15.5) (2021-10-06)
105
-
106
- **Note:** Version bump only for package @contentful/field-editor-checkbox
107
-
108
- ## [0.15.4](https://github.com/contentful/field-editors/compare/@contentful/field-editor-checkbox@0.15.3...@contentful/field-editor-checkbox@0.15.4) (2021-09-17)
109
-
110
- **Note:** Version bump only for package @contentful/field-editor-checkbox
111
-
112
- ## [0.15.3](https://github.com/contentful/field-editors/compare/@contentful/field-editor-checkbox@0.15.2...@contentful/field-editor-checkbox@0.15.3) (2021-09-16)
113
-
114
- **Note:** Version bump only for package @contentful/field-editor-checkbox
115
-
116
- ## [0.15.2](https://github.com/contentful/field-editors/compare/@contentful/field-editor-checkbox@0.15.1...@contentful/field-editor-checkbox@0.15.2) (2021-08-19)
117
-
118
- **Note:** Version bump only for package @contentful/field-editor-checkbox
119
-
120
- ## [0.15.1](https://github.com/contentful/field-editors/compare/@contentful/field-editor-checkbox@0.15.0...@contentful/field-editor-checkbox@0.15.1) (2021-07-29)
121
-
122
- **Note:** Version bump only for package @contentful/field-editor-checkbox
123
-
124
- # [0.15.0](https://github.com/contentful/field-editors/compare/@contentful/field-editor-checkbox@0.14.0...@contentful/field-editor-checkbox@0.15.0) (2021-07-23)
125
-
126
- ### Features
127
-
128
- - 💡 new color tokens ([#778](https://github.com/contentful/field-editors/issues/778)) ([fba548d](https://github.com/contentful/field-editors/commit/fba548de32305016df7f2685634eefb14294828f))
129
-
130
- # [0.14.0](https://github.com/contentful/field-editors/compare/@contentful/field-editor-checkbox@0.13.6...@contentful/field-editor-checkbox@0.14.0) (2021-07-12)
131
-
132
- ### Features
133
-
134
- - fix non-unique checkbox labels for multiple editors ([#775](https://github.com/contentful/field-editors/issues/775)) ([a5e09b3](https://github.com/contentful/field-editors/commit/a5e09b3359fb48a0b6d27629905df5fccc1a1b37))
135
-
136
- ## [0.13.6](https://github.com/contentful/field-editors/compare/@contentful/field-editor-checkbox@0.13.3...@contentful/field-editor-checkbox@0.13.6) (2021-07-06)
137
-
138
- **Note:** Version bump only for package @contentful/field-editor-checkbox
139
-
140
- ## [0.13.5](https://github.com/contentful/field-editors/compare/@contentful/field-editor-checkbox@0.13.3...@contentful/field-editor-checkbox@0.13.5) (2021-07-06)
141
-
142
- **Note:** Version bump only for package @contentful/field-editor-checkbox
143
-
144
- ## [0.13.4](https://github.com/contentful/field-editors/compare/@contentful/field-editor-checkbox@0.13.3...@contentful/field-editor-checkbox@0.13.4) (2021-06-23)
145
-
146
- **Note:** Version bump only for package @contentful/field-editor-checkbox
147
-
148
- ## [0.13.3](https://github.com/contentful/field-editors/compare/@contentful/field-editor-checkbox@0.13.2...@contentful/field-editor-checkbox@0.13.3) (2021-06-23)
149
-
150
- **Note:** Version bump only for package @contentful/field-editor-checkbox
151
-
152
- ## [0.13.2](https://github.com/contentful/field-editors/compare/@contentful/field-editor-checkbox@0.13.1...@contentful/field-editor-checkbox@0.13.2) (2021-06-22)
153
-
154
- **Note:** Version bump only for package @contentful/field-editor-checkbox
155
-
156
- ## [0.13.1](https://github.com/contentful/field-editors/compare/@contentful/field-editor-checkbox@0.13.0...@contentful/field-editor-checkbox@0.13.1) (2021-03-05)
157
-
158
- **Note:** Version bump only for package @contentful/field-editor-checkbox
159
-
160
- # [0.13.0](https://github.com/contentful/field-editors/compare/@contentful/field-editor-checkbox@0.12.2...@contentful/field-editor-checkbox@0.13.0) (2021-02-19)
161
-
162
- ### Features
163
-
164
- - bump min version of forma-36 ([#606](https://github.com/contentful/field-editors/issues/606)) ([fd57c7a](https://github.com/contentful/field-editors/commit/fd57c7a4312766af38c01507f17706ab22992617))
165
-
166
- ## [0.12.2](https://github.com/contentful/field-editors/compare/@contentful/field-editor-checkbox@0.12.1...@contentful/field-editor-checkbox@0.12.2) (2021-02-09)
167
-
168
- **Note:** Version bump only for package @contentful/field-editor-checkbox
169
-
170
- ## [0.12.1](https://github.com/contentful/field-editors/compare/@contentful/field-editor-checkbox@0.12.0...@contentful/field-editor-checkbox@0.12.1) (2021-02-01)
171
-
172
- **Note:** Version bump only for package @contentful/field-editor-checkbox
173
-
174
- # [0.12.0](https://github.com/contentful/field-editors/compare/@contentful/field-editor-checkbox@0.11.0...@contentful/field-editor-checkbox@0.12.0) (2021-01-20)
175
-
176
- ### Features
177
-
178
- - update minimal forma-36 versions to use updated design ([#565](https://github.com/contentful/field-editors/issues/565)) ([332c734](https://github.com/contentful/field-editors/commit/332c734bfaf54f0e9773fcbb460d743b1f5459ec))
179
-
180
- # [0.11.0](https://github.com/contentful/field-editors/compare/@contentful/field-editor-checkbox@0.10.6...@contentful/field-editor-checkbox@0.11.0) (2021-01-12)
181
-
182
- ### Features
183
-
184
- - update minimal required Forma version to the 3.73.12 ([#552](https://github.com/contentful/field-editors/issues/552)) ([2816fd9](https://github.com/contentful/field-editors/commit/2816fd960c28815faebf49a9ef8f4c4c0d91fc36))
185
-
186
- ## [0.10.6](https://github.com/contentful/field-editors/compare/@contentful/field-editor-checkbox@0.10.5...@contentful/field-editor-checkbox@0.10.6) (2020-12-16)
187
-
188
- **Note:** Version bump only for package @contentful/field-editor-checkbox
189
-
190
- ## [0.10.5](https://github.com/contentful/field-editors/compare/@contentful/field-editor-checkbox@0.10.4...@contentful/field-editor-checkbox@0.10.5) (2020-11-06)
191
-
192
- **Note:** Version bump only for package @contentful/field-editor-checkbox
193
-
194
- ## [0.10.4](https://github.com/contentful/field-editors/compare/@contentful/field-editor-checkbox@0.10.3...@contentful/field-editor-checkbox@0.10.4) (2020-11-06)
195
-
196
- **Note:** Version bump only for package @contentful/field-editor-checkbox
197
-
198
- ## [0.10.3](https://github.com/contentful/field-editors/compare/@contentful/field-editor-checkbox@0.10.2...@contentful/field-editor-checkbox@0.10.3) (2020-10-28)
199
-
200
- **Note:** Version bump only for package @contentful/field-editor-checkbox
201
-
202
- ## [0.10.2](https://github.com/contentful/field-editors/compare/@contentful/field-editor-checkbox@0.10.1...@contentful/field-editor-checkbox@0.10.2) (2020-08-24)
203
-
204
- **Note:** Version bump only for package @contentful/field-editor-checkbox
@@ -1,130 +0,0 @@
1
- 'use strict';
2
-
3
- Object.defineProperty(exports, '__esModule', { value: true });
4
-
5
- function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
6
-
7
- var React = require('react');
8
- var f36Components = require('@contentful/f36-components');
9
- var fieldEditorShared = require('@contentful/field-editor-shared');
10
- var emotion = require('emotion');
11
- var get = _interopDefault(require('lodash/get'));
12
- var nanoid = require('nanoid');
13
- var tokens = _interopDefault(require('@contentful/f36-tokens'));
14
-
15
- const form = /*#__PURE__*/emotion.css({
16
- marginTop: tokens.spacingS
17
- });
18
- const rightToLeft = /*#__PURE__*/emotion.css({
19
- direction: 'rtl'
20
- });
21
- const invalidText = /*#__PURE__*/emotion.css({
22
- color: tokens.red500,
23
- marginLeft: tokens.spacing2Xs
24
- });
25
- const removeBtn = /*#__PURE__*/emotion.css({
26
- marginLeft: tokens.spacingL
27
- });
28
-
29
- function isEmptyListValue(value) {
30
- return value === null || value.length === 0;
31
- }
32
-
33
- function getOptions(field, id) {
34
- // Get first object that has a 'in' property
35
- const validations = get(field, ['items', 'validations'], []);
36
- const predefinedValues = validations.filter(validation => validation.in).map(validation => validation.in);
37
- const firstPredefinedValues = predefinedValues.length > 0 ? predefinedValues[0] : [];
38
- return firstPredefinedValues.map((value, index) => ({
39
- // Append a random id to distinguish between checkboxes opened in two editors (e.g. slide-in)
40
- id: ['entity', field.id, field.locale, index, id].join('.'),
41
- value,
42
- label: value
43
- }));
44
- }
45
-
46
- const getInvalidValues = (field, values, options) => {
47
- const getValueFromOptions = options.map(item => item.value);
48
- const invalidValues = values.filter(value => !getValueFromOptions.includes(value)).map((value, index) => ({
49
- id: ['entity', field.id, field.locale, index, 'invalid'].join('.'),
50
- label: value,
51
- invalid: true,
52
- value
53
- }));
54
- return invalidValues;
55
- };
56
-
57
- function CheckboxEditor(props) {
58
- const [id] = React.useState(() => nanoid.nanoid(6));
59
- const {
60
- field,
61
- locales
62
- } = props;
63
- const options = getOptions(field, id);
64
- const misconfigured = options.length === 0;
65
-
66
- if (misconfigured) {
67
- return React.createElement(fieldEditorShared.PredefinedValuesError, null);
68
- }
69
-
70
- const direction = locales.direction[field.locale] || 'ltr';
71
- return React.createElement(fieldEditorShared.FieldConnector, {
72
- throttle: 0,
73
- isEmptyValue: isEmptyListValue,
74
- field: field,
75
- isInitiallyDisabled: props.isInitiallyDisabled
76
- }, ({
77
- disabled,
78
- value,
79
- setValue
80
- }) => {
81
- const values = value || [];
82
-
83
- const addValue = value => {
84
- const newValues = [...values.filter(item => item !== value), value];
85
- setValue(newValues);
86
- };
87
-
88
- const removeValue = value => {
89
- const newValues = values.filter(item => item !== value);
90
- setValue(newValues);
91
- };
92
-
93
- const invalidValues = getInvalidValues(field, values, options);
94
- const mergedOptions = [...options, ...invalidValues];
95
- return React.createElement(f36Components.Form, {
96
- testId: "checkbox-editor",
97
- className: emotion.cx(form, direction === 'rtl' ? rightToLeft : '')
98
- }, mergedOptions.map(item => React.createElement(f36Components.Box, {
99
- key: item.id,
100
- marginBottom: "spacingS"
101
- }, React.createElement(f36Components.Checkbox, {
102
- key: item.id,
103
- id: item.id,
104
- isChecked: values.includes(item.value),
105
- isDisabled: disabled,
106
- value: item.value,
107
- name: `${field.id}.${field.locale}`,
108
- onChange: e => {
109
- if (e.target.checked) {
110
- addValue(item.value);
111
- } else {
112
- removeValue(item.value);
113
- }
114
- }
115
- }, item.label), item.invalid && React.createElement(React.Fragment, null, React.createElement("span", {
116
- "data-test-id": "invalid-text",
117
- className: invalidText
118
- }, "(invalid)"), React.createElement(f36Components.TextLink, {
119
- as: "button",
120
- className: removeBtn,
121
- onClick: () => removeValue(item.value)
122
- }, "Remove")))));
123
- });
124
- }
125
- CheckboxEditor.defaultProps = {
126
- isInitiallyDisabled: true
127
- };
128
-
129
- exports.CheckboxEditor = CheckboxEditor;
130
- //# sourceMappingURL=field-editor-checkbox.cjs.development.js.map