@contentful/field-editor-dropdown 1.1.10 → 1.2.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 +10 -0
- package/dist/field-editor-dropdown.cjs.development.js +40 -47
- package/dist/field-editor-dropdown.cjs.development.js.map +1 -1
- package/dist/field-editor-dropdown.cjs.production.min.js +1 -1
- package/dist/field-editor-dropdown.cjs.production.min.js.map +1 -1
- package/dist/field-editor-dropdown.esm.js +40 -47
- package/dist/field-editor-dropdown.esm.js.map +1 -1
- package/package.json +4 -14
package/CHANGELOG.md
CHANGED
|
@@ -3,6 +3,16 @@
|
|
|
3
3
|
All notable changes to this project will be documented in this file.
|
|
4
4
|
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
|
5
5
|
|
|
6
|
+
# [1.2.0](https://github.com/contentful/field-editors/compare/@contentful/field-editor-dropdown@1.1.11...@contentful/field-editor-dropdown@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.11](https://github.com/contentful/field-editors/compare/@contentful/field-editor-dropdown@1.1.10...@contentful/field-editor-dropdown@1.1.11) (2023-03-14)
|
|
13
|
+
|
|
14
|
+
**Note:** Version bump only for package @contentful/field-editor-dropdown
|
|
15
|
+
|
|
6
16
|
## [1.1.10](https://github.com/contentful/field-editors/compare/@contentful/field-editor-dropdown@1.1.9...@contentful/field-editor-dropdown@1.1.10) (2023-03-10)
|
|
7
17
|
|
|
8
18
|
**Note:** Version bump only for package @contentful/field-editor-dropdown
|
|
@@ -3,10 +3,10 @@
|
|
|
3
3
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
4
|
|
|
5
5
|
var React = require('react');
|
|
6
|
+
var f36Components = require('@contentful/f36-components');
|
|
6
7
|
var fieldEditorShared = require('@contentful/field-editor-shared');
|
|
7
8
|
var nanoid = require('nanoid');
|
|
8
9
|
var emotion = require('emotion');
|
|
9
|
-
var f36Components = require('@contentful/f36-components');
|
|
10
10
|
|
|
11
11
|
function parseValue(value, fieldType) {
|
|
12
12
|
if (value === '') {
|
|
@@ -14,7 +14,7 @@ function parseValue(value, fieldType) {
|
|
|
14
14
|
}
|
|
15
15
|
|
|
16
16
|
if (fieldType === 'Integer' || fieldType === 'Number') {
|
|
17
|
-
|
|
17
|
+
const asNumber = Number(value);
|
|
18
18
|
return isNaN(asNumber) ? undefined : asNumber;
|
|
19
19
|
}
|
|
20
20
|
|
|
@@ -22,68 +22,61 @@ function parseValue(value, fieldType) {
|
|
|
22
22
|
}
|
|
23
23
|
function getOptions(field) {
|
|
24
24
|
// Get first object that has a 'in' property
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
return {
|
|
34
|
-
id: nanoid.nanoid(6),
|
|
35
|
-
value: parseValue(value, field.type),
|
|
36
|
-
label: String(value)
|
|
37
|
-
};
|
|
38
|
-
}).filter(function (item) {
|
|
25
|
+
const validations = field.validations || [];
|
|
26
|
+
const predefinedValues = validations.filter(validation => validation.in).map(validation => validation.in);
|
|
27
|
+
const firstPredefinedValues = predefinedValues.length > 0 ? predefinedValues[0] : [];
|
|
28
|
+
return firstPredefinedValues.map(value => ({
|
|
29
|
+
id: nanoid.nanoid(6),
|
|
30
|
+
value: parseValue(value, field.type),
|
|
31
|
+
label: String(value)
|
|
32
|
+
})).filter(item => {
|
|
39
33
|
return item.value !== undefined;
|
|
40
34
|
});
|
|
41
35
|
}
|
|
42
36
|
|
|
43
|
-
|
|
37
|
+
const rightToLeft = /*#__PURE__*/emotion.css({
|
|
44
38
|
direction: 'rtl'
|
|
45
39
|
});
|
|
46
40
|
|
|
47
41
|
function DropdownEditor(props) {
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
42
|
+
const {
|
|
43
|
+
field,
|
|
44
|
+
locales
|
|
45
|
+
} = props;
|
|
46
|
+
const options = getOptions(field);
|
|
47
|
+
const misconfigured = options.length === 0;
|
|
52
48
|
|
|
53
49
|
if (misconfigured) {
|
|
54
50
|
return React.createElement(fieldEditorShared.PredefinedValuesError, null);
|
|
55
51
|
}
|
|
56
52
|
|
|
57
|
-
|
|
53
|
+
const direction = locales.direction[field.locale] || 'ltr';
|
|
58
54
|
return React.createElement(fieldEditorShared.FieldConnector, {
|
|
59
55
|
throttle: 0,
|
|
60
56
|
field: field,
|
|
61
57
|
isInitiallyDisabled: props.isInitiallyDisabled
|
|
62
|
-
},
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
}, option.label);
|
|
85
|
-
}));
|
|
86
|
-
});
|
|
58
|
+
}, ({
|
|
59
|
+
value,
|
|
60
|
+
errors,
|
|
61
|
+
disabled,
|
|
62
|
+
setValue
|
|
63
|
+
}) => React.createElement(f36Components.Select, {
|
|
64
|
+
testId: "dropdown-editor",
|
|
65
|
+
isInvalid: errors.length > 0,
|
|
66
|
+
isDisabled: disabled,
|
|
67
|
+
className: direction === 'rtl' ? rightToLeft : '',
|
|
68
|
+
isRequired: field.required,
|
|
69
|
+
value: value === undefined ? '' : String(value),
|
|
70
|
+
onChange: e => {
|
|
71
|
+
const value = e.target.value;
|
|
72
|
+
setValue(parseValue(value, field.type));
|
|
73
|
+
}
|
|
74
|
+
}, React.createElement(f36Components.Select.Option, {
|
|
75
|
+
value: ""
|
|
76
|
+
}, "Choose a value"), options.map(option => React.createElement(f36Components.Select.Option, {
|
|
77
|
+
key: option.value,
|
|
78
|
+
value: String(option.value)
|
|
79
|
+
}, option.label))));
|
|
87
80
|
}
|
|
88
81
|
DropdownEditor.defaultProps = {
|
|
89
82
|
isInitiallyDisabled: true
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"field-editor-dropdown.cjs.development.js","sources":["../src/dropdownUtils.ts","../src/styles.ts","../src/DropdownEditor.tsx"],"sourcesContent":["import {
|
|
1
|
+
{"version":3,"file":"field-editor-dropdown.cjs.development.js","sources":["../src/dropdownUtils.ts","../src/styles.ts","../src/DropdownEditor.tsx"],"sourcesContent":["import { FieldAPI } from '@contentful/field-editor-shared';\nimport { nanoid } from 'nanoid';\n\ntype DropdownOption = {\n id: string;\n value: string | number | undefined;\n label: string;\n};\n\nexport function parseValue(value: string, fieldType: string): string | number | undefined {\n if (value === '') {\n return undefined;\n }\n if (fieldType === 'Integer' || fieldType === 'Number') {\n const asNumber = Number(value);\n return isNaN(asNumber) ? undefined : asNumber;\n }\n return value;\n}\n\nexport function getOptions(field: FieldAPI): DropdownOption[] {\n // Get first object that has a 'in' property\n const validations = field.validations || [];\n const predefinedValues = validations\n .filter((validation) => (validation as any).in)\n .map((validation) => (validation as any).in);\n\n const firstPredefinedValues = predefinedValues.length > 0 ? predefinedValues[0] : [];\n\n return firstPredefinedValues\n .map((value: string) => ({\n id: nanoid(6),\n value: parseValue(value, field.type),\n label: String(value),\n }))\n .filter((item: { value: string | number | undefined; label: string }) => {\n return item.value !== undefined;\n });\n}\n","import { css } from 'emotion';\n\nexport const rightToLeft = css({\n direction: 'rtl',\n});\n","import * as React from 'react';\n\nimport { Select } from '@contentful/f36-components';\nimport {\n FieldAPI,\n FieldConnector,\n PredefinedValuesError,\n LocalesAPI,\n} from '@contentful/field-editor-shared';\n\nimport { getOptions, parseValue } from './dropdownUtils';\nimport * as styles from './styles';\n\n\nexport interface DropdownEditorProps {\n /**\n * is the field disabled initially\n */\n isInitiallyDisabled: boolean;\n /**\n * sdk.field\n */\n field: FieldAPI;\n\n /**\n * sdk.locales\n */\n locales: LocalesAPI;\n}\n\nexport function DropdownEditor(props: DropdownEditorProps) {\n const { field, locales } = props;\n\n const options = getOptions(field);\n const misconfigured = options.length === 0;\n\n if (misconfigured) {\n return <PredefinedValuesError />;\n }\n\n const direction = locales.direction[field.locale] || 'ltr';\n\n return (\n <FieldConnector<string | number>\n throttle={0}\n field={field}\n isInitiallyDisabled={props.isInitiallyDisabled}>\n {({ value, errors, disabled, setValue }) => (\n <Select\n testId=\"dropdown-editor\"\n isInvalid={errors.length > 0}\n isDisabled={disabled}\n className={direction === 'rtl' ? styles.rightToLeft : ''}\n isRequired={field.required}\n value={value === undefined ? '' : String(value)}\n onChange={(e: React.ChangeEvent<HTMLSelectElement>) => {\n const value = e.target.value;\n setValue(parseValue(value, field.type));\n }}>\n <Select.Option value=\"\">Choose a value</Select.Option>\n {options.map((option) => (\n <Select.Option key={option.value} value={String(option.value)}>\n {option.label}\n </Select.Option>\n ))}\n </Select>\n )}\n </FieldConnector>\n );\n}\n\nDropdownEditor.defaultProps = {\n isInitiallyDisabled: true,\n};\n"],"names":["parseValue","value","fieldType","undefined","asNumber","Number","isNaN","getOptions","field","validations","predefinedValues","filter","validation","in","map","firstPredefinedValues","length","id","nanoid","type","label","String","item","rightToLeft","css","direction","DropdownEditor","props","locales","options","misconfigured","React","PredefinedValuesError","locale","FieldConnector","throttle","isInitiallyDisabled","errors","disabled","setValue","Select","testId","isInvalid","isDisabled","className","styles","isRequired","required","onChange","e","target","Option","option","key","defaultProps"],"mappings":";;;;;;;;;;SASgBA,WAAWC,OAAeC;AACxC,MAAID,KAAK,KAAK,EAAd,EAAkB;AAChB,WAAOE,SAAP;AACD;;AACD,MAAID,SAAS,KAAK,SAAd,IAA2BA,SAAS,KAAK,QAA7C,EAAuD;AACrD,UAAME,QAAQ,GAAGC,M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,2 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("react"),
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("react"),t=require("@contentful/f36-components"),r=require("@contentful/field-editor-shared"),i=require("nanoid");function l(e,t){if(""!==e){if("Integer"===t||"Number"===t){const t=Number(e);return isNaN(t)?void 0:t}return e}}function n(e){const t=(e.validations||[]).filter(e=>e.in).map(e=>e.in);return(t.length>0?t[0]:[]).map(t=>({id:i.nanoid(6),value:l(t,e.type),label:String(t)})).filter(e=>void 0!==e.value)}const a=require("emotion").css({direction:"rtl"});function o(i){const{field:o,locales:s}=i,u=n(o);if(0===u.length)return e.createElement(r.PredefinedValuesError,null);const d=s.direction[o.locale]||"ltr";return e.createElement(r.FieldConnector,{throttle:0,field:o,isInitiallyDisabled:i.isInitiallyDisabled},({value:r,errors:i,disabled:n,setValue:s})=>e.createElement(t.Select,{testId:"dropdown-editor",isInvalid:i.length>0,isDisabled:n,className:"rtl"===d?a:"",isRequired:o.required,value:void 0===r?"":String(r),onChange:e=>{s(l(e.target.value,o.type))}},e.createElement(t.Select.Option,{value:""},"Choose a value"),u.map(r=>e.createElement(t.Select.Option,{key:r.value,value:String(r.value)},r.label))))}o.defaultProps={isInitiallyDisabled:!0},exports.DropdownEditor=o,exports.getOptions=n,exports.parseValue=l;
|
|
2
2
|
//# sourceMappingURL=field-editor-dropdown.cjs.production.min.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"field-editor-dropdown.cjs.production.min.js","sources":["../src/dropdownUtils.ts","../src/styles.ts","../src/DropdownEditor.tsx"],"sourcesContent":["import {
|
|
1
|
+
{"version":3,"file":"field-editor-dropdown.cjs.production.min.js","sources":["../src/dropdownUtils.ts","../src/styles.ts","../src/DropdownEditor.tsx"],"sourcesContent":["import { FieldAPI } from '@contentful/field-editor-shared';\nimport { nanoid } from 'nanoid';\n\ntype DropdownOption = {\n id: string;\n value: string | number | undefined;\n label: string;\n};\n\nexport function parseValue(value: string, fieldType: string): string | number | undefined {\n if (value === '') {\n return undefined;\n }\n if (fieldType === 'Integer' || fieldType === 'Number') {\n const asNumber = Number(value);\n return isNaN(asNumber) ? undefined : asNumber;\n }\n return value;\n}\n\nexport function getOptions(field: FieldAPI): DropdownOption[] {\n // Get first object that has a 'in' property\n const validations = field.validations || [];\n const predefinedValues = validations\n .filter((validation) => (validation as any).in)\n .map((validation) => (validation as any).in);\n\n const firstPredefinedValues = predefinedValues.length > 0 ? predefinedValues[0] : [];\n\n return firstPredefinedValues\n .map((value: string) => ({\n id: nanoid(6),\n value: parseValue(value, field.type),\n label: String(value),\n }))\n .filter((item: { value: string | number | undefined; label: string }) => {\n return item.value !== undefined;\n });\n}\n","import { css } from 'emotion';\n\nexport const rightToLeft = css({\n direction: 'rtl',\n});\n","import * as React from 'react';\n\nimport { Select } from '@contentful/f36-components';\nimport {\n FieldAPI,\n FieldConnector,\n PredefinedValuesError,\n LocalesAPI,\n} from '@contentful/field-editor-shared';\n\nimport { getOptions, parseValue } from './dropdownUtils';\nimport * as styles from './styles';\n\n\nexport interface DropdownEditorProps {\n /**\n * is the field disabled initially\n */\n isInitiallyDisabled: boolean;\n /**\n * sdk.field\n */\n field: FieldAPI;\n\n /**\n * sdk.locales\n */\n locales: LocalesAPI;\n}\n\nexport function DropdownEditor(props: DropdownEditorProps) {\n const { field, locales } = props;\n\n const options = getOptions(field);\n const misconfigured = options.length === 0;\n\n if (misconfigured) {\n return <PredefinedValuesError />;\n }\n\n const direction = locales.direction[field.locale] || 'ltr';\n\n return (\n <FieldConnector<string | number>\n throttle={0}\n field={field}\n isInitiallyDisabled={props.isInitiallyDisabled}>\n {({ value, errors, disabled, setValue }) => (\n <Select\n testId=\"dropdown-editor\"\n isInvalid={errors.length > 0}\n isDisabled={disabled}\n className={direction === 'rtl' ? styles.rightToLeft : ''}\n isRequired={field.required}\n value={value === undefined ? '' : String(value)}\n onChange={(e: React.ChangeEvent<HTMLSelectElement>) => {\n const value = e.target.value;\n setValue(parseValue(value, field.type));\n }}>\n <Select.Option value=\"\">Choose a value</Select.Option>\n {options.map((option) => (\n <Select.Option key={option.value} value={String(option.value)}>\n {option.label}\n </Select.Option>\n ))}\n </Select>\n )}\n </FieldConnector>\n );\n}\n\nDropdownEditor.defaultProps = {\n isInitiallyDisabled: true,\n};\n"],"names":["parseValue","value","fieldType","asNumber","Number","isNaN","undefined","getOptions","field","predefinedValues","validations","filter","validation","in","map","length","id","nanoid","type","label","String","item","rightToLeft","direction","DropdownEditor","props","locales","options","React","PredefinedValuesError","locale","FieldConnector","throttle","isInitiallyDisabled","errors","disabled","setValue","Select","testId","isInvalid","isDisabled","className","styles","isRequired","required","onChange","e","target","Option","option","key","defaultProps"],"mappings":"6MASgBA,EAAWC,EAAeC,MAC1B,KAAVD,MAGc,YAAdC,GAAyC,WAAdA,EAAwB,OAC/CC,EAAWC,OAAOH,UACjBI,MAAMF,QAAYG,EAAYH,SAEhCF,YAGOM,EAAWC,SAGnBC,GADcD,EAAME,aAAe,IAEtCC,OAAQC,GAAgBA,EAAmBC,IAC3CC,IAAKF,GAAgBA,EAAmBC,WAEbJ,EAAiBM,OAAS,EAAIN,EAAiB,GAAK,IAG/EK,IAAKb,KACJe,GAAIC,SAAO,GACXhB,MAAOD,EAAWC,EAAOO,EAAMU,MAC/BC,MAAOC,OAAOnB,MAEfU,OAAQU,QACef,IAAfe,EAAKpB,OClCX,MAAMqB,yBAAkB,CAC7BC,UAAW,iBC2BGC,EAAeC,SACvBjB,MAAEA,EAAFkB,QAASA,GAAYD,EAErBE,EAAUpB,EAAWC,MACc,IAAnBmB,EAAQZ,cAGrBa,gBAACC,oCAGJN,EAAYG,EAAQH,UAAUf,EAAMsB,SAAW,aAGnDF,gBAACG,kBACCC,SAAU,EACVxB,MAAOA,EACPyB,oBAAqBR,EAAMQ,qBAC1B,EAAGhC,MAAAA,EAAOiC,OAAAA,EAAQC,SAAAA,EAAUC,SAAAA,KAC3BR,gBAACS,UACCC,OAAO,kBACPC,UAAWL,EAAOnB,OAAS,EAC3ByB,WAAYL,EACZM,UAAyB,QAAdlB,EAAsBmB,EAAqB,GACtDC,WAAYnC,EAAMoC,SAClB3C,WAAiBK,IAAVL,EAAsB,GAAKmB,OAAOnB,GACzC4C,SAAWC,IAETV,EAASpC,EADK8C,EAAEC,OAAO9C,MACIO,EAAMU,SAEnCU,gBAACS,SAAOW,QAAO/C,MAAM,sBACpB0B,EAAQb,IAAKmC,GACZrB,gBAACS,SAAOW,QAAOE,IAAKD,EAAOhD,MAAOA,MAAOmB,OAAO6B,EAAOhD,QACpDgD,EAAO9B,UAStBK,EAAe2B,aAAe,CAC5BlB,qBAAqB"}
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { createElement } from 'react';
|
|
2
|
+
import { Select } from '@contentful/f36-components';
|
|
2
3
|
import { PredefinedValuesError, FieldConnector } from '@contentful/field-editor-shared';
|
|
3
4
|
import { nanoid } from 'nanoid';
|
|
4
5
|
import { css } from 'emotion';
|
|
5
|
-
import { Select } from '@contentful/f36-components';
|
|
6
6
|
|
|
7
7
|
function parseValue(value, fieldType) {
|
|
8
8
|
if (value === '') {
|
|
@@ -10,7 +10,7 @@ function parseValue(value, fieldType) {
|
|
|
10
10
|
}
|
|
11
11
|
|
|
12
12
|
if (fieldType === 'Integer' || fieldType === 'Number') {
|
|
13
|
-
|
|
13
|
+
const asNumber = Number(value);
|
|
14
14
|
return isNaN(asNumber) ? undefined : asNumber;
|
|
15
15
|
}
|
|
16
16
|
|
|
@@ -18,68 +18,61 @@ function parseValue(value, fieldType) {
|
|
|
18
18
|
}
|
|
19
19
|
function getOptions(field) {
|
|
20
20
|
// Get first object that has a 'in' property
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
return {
|
|
30
|
-
id: nanoid(6),
|
|
31
|
-
value: parseValue(value, field.type),
|
|
32
|
-
label: String(value)
|
|
33
|
-
};
|
|
34
|
-
}).filter(function (item) {
|
|
21
|
+
const validations = field.validations || [];
|
|
22
|
+
const predefinedValues = validations.filter(validation => validation.in).map(validation => validation.in);
|
|
23
|
+
const firstPredefinedValues = predefinedValues.length > 0 ? predefinedValues[0] : [];
|
|
24
|
+
return firstPredefinedValues.map(value => ({
|
|
25
|
+
id: nanoid(6),
|
|
26
|
+
value: parseValue(value, field.type),
|
|
27
|
+
label: String(value)
|
|
28
|
+
})).filter(item => {
|
|
35
29
|
return item.value !== undefined;
|
|
36
30
|
});
|
|
37
31
|
}
|
|
38
32
|
|
|
39
|
-
|
|
33
|
+
const rightToLeft = /*#__PURE__*/css({
|
|
40
34
|
direction: 'rtl'
|
|
41
35
|
});
|
|
42
36
|
|
|
43
37
|
function DropdownEditor(props) {
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
38
|
+
const {
|
|
39
|
+
field,
|
|
40
|
+
locales
|
|
41
|
+
} = props;
|
|
42
|
+
const options = getOptions(field);
|
|
43
|
+
const misconfigured = options.length === 0;
|
|
48
44
|
|
|
49
45
|
if (misconfigured) {
|
|
50
46
|
return createElement(PredefinedValuesError, null);
|
|
51
47
|
}
|
|
52
48
|
|
|
53
|
-
|
|
49
|
+
const direction = locales.direction[field.locale] || 'ltr';
|
|
54
50
|
return createElement(FieldConnector, {
|
|
55
51
|
throttle: 0,
|
|
56
52
|
field: field,
|
|
57
53
|
isInitiallyDisabled: props.isInitiallyDisabled
|
|
58
|
-
},
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
}, option.label);
|
|
81
|
-
}));
|
|
82
|
-
});
|
|
54
|
+
}, ({
|
|
55
|
+
value,
|
|
56
|
+
errors,
|
|
57
|
+
disabled,
|
|
58
|
+
setValue
|
|
59
|
+
}) => createElement(Select, {
|
|
60
|
+
testId: "dropdown-editor",
|
|
61
|
+
isInvalid: errors.length > 0,
|
|
62
|
+
isDisabled: disabled,
|
|
63
|
+
className: direction === 'rtl' ? rightToLeft : '',
|
|
64
|
+
isRequired: field.required,
|
|
65
|
+
value: value === undefined ? '' : String(value),
|
|
66
|
+
onChange: e => {
|
|
67
|
+
const value = e.target.value;
|
|
68
|
+
setValue(parseValue(value, field.type));
|
|
69
|
+
}
|
|
70
|
+
}, createElement(Select.Option, {
|
|
71
|
+
value: ""
|
|
72
|
+
}, "Choose a value"), options.map(option => createElement(Select.Option, {
|
|
73
|
+
key: option.value,
|
|
74
|
+
value: String(option.value)
|
|
75
|
+
}, option.label))));
|
|
83
76
|
}
|
|
84
77
|
DropdownEditor.defaultProps = {
|
|
85
78
|
isInitiallyDisabled: true
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"field-editor-dropdown.esm.js","sources":["../src/dropdownUtils.ts","../src/styles.ts","../src/DropdownEditor.tsx"],"sourcesContent":["import {
|
|
1
|
+
{"version":3,"file":"field-editor-dropdown.esm.js","sources":["../src/dropdownUtils.ts","../src/styles.ts","../src/DropdownEditor.tsx"],"sourcesContent":["import { FieldAPI } from '@contentful/field-editor-shared';\nimport { nanoid } from 'nanoid';\n\ntype DropdownOption = {\n id: string;\n value: string | number | undefined;\n label: string;\n};\n\nexport function parseValue(value: string, fieldType: string): string | number | undefined {\n if (value === '') {\n return undefined;\n }\n if (fieldType === 'Integer' || fieldType === 'Number') {\n const asNumber = Number(value);\n return isNaN(asNumber) ? undefined : asNumber;\n }\n return value;\n}\n\nexport function getOptions(field: FieldAPI): DropdownOption[] {\n // Get first object that has a 'in' property\n const validations = field.validations || [];\n const predefinedValues = validations\n .filter((validation) => (validation as any).in)\n .map((validation) => (validation as any).in);\n\n const firstPredefinedValues = predefinedValues.length > 0 ? predefinedValues[0] : [];\n\n return firstPredefinedValues\n .map((value: string) => ({\n id: nanoid(6),\n value: parseValue(value, field.type),\n label: String(value),\n }))\n .filter((item: { value: string | number | undefined; label: string }) => {\n return item.value !== undefined;\n });\n}\n","import { css } from 'emotion';\n\nexport const rightToLeft = css({\n direction: 'rtl',\n});\n","import * as React from 'react';\n\nimport { Select } from '@contentful/f36-components';\nimport {\n FieldAPI,\n FieldConnector,\n PredefinedValuesError,\n LocalesAPI,\n} from '@contentful/field-editor-shared';\n\nimport { getOptions, parseValue } from './dropdownUtils';\nimport * as styles from './styles';\n\n\nexport interface DropdownEditorProps {\n /**\n * is the field disabled initially\n */\n isInitiallyDisabled: boolean;\n /**\n * sdk.field\n */\n field: FieldAPI;\n\n /**\n * sdk.locales\n */\n locales: LocalesAPI;\n}\n\nexport function DropdownEditor(props: DropdownEditorProps) {\n const { field, locales } = props;\n\n const options = getOptions(field);\n const misconfigured = options.length === 0;\n\n if (misconfigured) {\n return <PredefinedValuesError />;\n }\n\n const direction = locales.direction[field.locale] || 'ltr';\n\n return (\n <FieldConnector<string | number>\n throttle={0}\n field={field}\n isInitiallyDisabled={props.isInitiallyDisabled}>\n {({ value, errors, disabled, setValue }) => (\n <Select\n testId=\"dropdown-editor\"\n isInvalid={errors.length > 0}\n isDisabled={disabled}\n className={direction === 'rtl' ? styles.rightToLeft : ''}\n isRequired={field.required}\n value={value === undefined ? '' : String(value)}\n onChange={(e: React.ChangeEvent<HTMLSelectElement>) => {\n const value = e.target.value;\n setValue(parseValue(value, field.type));\n }}>\n <Select.Option value=\"\">Choose a value</Select.Option>\n {options.map((option) => (\n <Select.Option key={option.value} value={String(option.value)}>\n {option.label}\n </Select.Option>\n ))}\n </Select>\n )}\n </FieldConnector>\n );\n}\n\nDropdownEditor.defaultProps = {\n isInitiallyDisabled: true,\n};\n"],"names":["parseValue","value","fieldType","undefined","asNumber","Number","isNaN","getOptions","field","validations","predefinedValues","filter","validation","in","map","firstPredefinedValues","length","id","nanoid","type","label","String","item","rightToLeft","css","direction","DropdownEditor","props","locales","options","misconfigured","React","PredefinedValuesError","locale","FieldConnector","throttle","isInitiallyDisabled","errors","disabled","setValue","Select","testId","isInvalid","isDisabled","className","styles","isRequired","required","onChange","e","target","Option","option","key","defaultProps"],"mappings":";;;;;;SASgBA,WAAWC,OAAeC;AACxC,MAAID,KAAK,KAAK,EAAd,EAAkB;AAChB,WAAOE,SAAP;AACD;;AACD,MAAID,SAAS,KAAK,SAAd,IAA2BA,SAAS,KAAK,QAA7C,EAAuD;AACrD,UAAME,QAAQ,GAAGC,MAAM,CAACJ,KAAD,CAAvB;AACA,WAAOK,KAAK,CAACF,QAAD,CAAL,GAAkBD,SAAlB,GAA8BC,QAArC;AACD;;AACD,SAAOH,KAAP;AACD;SAEeM,WAAWC;AACzB;AACA,QAAMC,WAAW,GAAGD,KAAK,CAACC,WAAN,IAAqB,EAAzC;AACA,QAAMC,gBAAgB,GAAGD,WAAW,CACjCE,MADsB,CACdC,UAAD,IAAiBA,UAAkB,CAACC,EADrB,EAEtBC,GAFsB,CAEjBF,UAAD,IAAiBA,UAAkB,CAACC,EAFlB,CAAzB;AAIA,QAAME,qBAAqB,GAAGL,gBAAgB,CAACM,MAAjB,GAA0B,CAA1B,GAA8BN,gBAAgB,CAAC,CAAD,CAA9C,GAAoD,EAAlF;AAEA,SAAOK,qBAAqB,CACzBD,GADI,CACCb,KAAD,KAAoB;AACvBgB,IAAAA,EAAE,EAAEC,MAAM,CAAC,CAAD,CADa;AAEvBjB,IAAAA,KAAK,EAAED,UAAU,CAACC,KAAD,EAAQO,KAAK,CAACW,IAAd,CAFM;AAGvBC,IAAAA,KAAK,EAAEC,MAAM,CAACpB,KAAD;AAHU,GAApB,CADA,EAMJU,MANI,CAMIW,IAAD;AACN,WAAOA,IAAI,CAACrB,KAAL,KAAeE,SAAtB;AACD,GARI,CAAP;AASD;;ACpC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,MAAM,CAACW,MAAR;AAAelD,IAAAA,KAAK,EAAC;GAArB,kBAAA,CAXF,EAYG4B,OAAO,CAACf,GAAR,CAAasC,MAAD,IACXrB,aAAA,CAACS,MAAM,CAACW,MAAR;AAAeE,IAAAA,GAAG,EAAED,MAAM,CAACnD;AAAOA,IAAAA,KAAK,EAAEoB,MAAM,CAAC+B,MAAM,CAACnD,KAAR;GAA/C,EACGmD,MAAM,CAAChC,KADV,CADD,CAZH,CALJ,CADF;AA2BD;AAEDM,cAAc,CAAC4B,YAAf,GAA8B;AAC5BlB,EAAAA,mBAAmB,EAAE;AADO,CAA9B;;;;"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@contentful/field-editor-dropdown",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.2.0",
|
|
4
4
|
"main": "dist/index.js",
|
|
5
5
|
"module": "dist/field-editor-dropdown.esm.js",
|
|
6
6
|
"typings": "dist/index.d.ts",
|
|
@@ -23,28 +23,18 @@
|
|
|
23
23
|
"dependencies": {
|
|
24
24
|
"@contentful/f36-components": "^4.0.27",
|
|
25
25
|
"@contentful/f36-tokens": "^4.0.0",
|
|
26
|
-
"@contentful/field-editor-shared": "^1.
|
|
26
|
+
"@contentful/field-editor-shared": "^1.2.0",
|
|
27
27
|
"emotion": "^10.0.0",
|
|
28
28
|
"lodash": "^4.17.15",
|
|
29
29
|
"lodash-es": "^4.17.15",
|
|
30
30
|
"nanoid": "^3.1.3"
|
|
31
31
|
},
|
|
32
32
|
"devDependencies": {
|
|
33
|
-
"@contentful/field-editor-test-utils": "^1.
|
|
33
|
+
"@contentful/field-editor-test-utils": "^1.3.0",
|
|
34
34
|
"contentful-management": "^10.0.0"
|
|
35
35
|
},
|
|
36
36
|
"peerDependencies": {
|
|
37
37
|
"react": ">=16.8.0"
|
|
38
38
|
},
|
|
39
|
-
"
|
|
40
|
-
"testMatch": [
|
|
41
|
-
"**/?(*.)+(spec|test).[jt]s?(x)"
|
|
42
|
-
],
|
|
43
|
-
"globals": {
|
|
44
|
-
"ts-jest": {
|
|
45
|
-
"diagnostics": false
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
},
|
|
49
|
-
"gitHead": "1e4a7d92c48f0e1949f2b48b3c9974da1602fd5c"
|
|
39
|
+
"gitHead": "de7e74e3485dd69c240cfe9c545e6e50e41fb295"
|
|
50
40
|
}
|