@contentful/field-editor-single-line 1.4.16 → 1.5.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.
@@ -57,17 +57,19 @@ function isSupportedFieldTypes(val) {
57
57
  return val === 'Symbol' || val === 'Text';
58
58
  }
59
59
  function SingleLineEditor(props) {
60
- const { field, locales } = props;
60
+ const { field, isDisabled, isInitiallyDisabled = true, locales, onBlur, onFocus, withCharInformation = true, withCharValidation = true } = props;
61
+ console.log(`isDisabled => `, isDisabled);
61
62
  if (!isSupportedFieldTypes(field.type)) {
62
63
  throw new Error(`"${field.type}" field type is not supported by SingleLineEditor`);
63
64
  }
64
65
  const constraints = _fieldeditorshared.ConstraintsUtils.fromFieldValidations(field.validations, field.type);
65
66
  const checkConstraint = _fieldeditorshared.ConstraintsUtils.makeChecker(constraints);
66
67
  const direction = locales.direction[field.locale] || 'ltr';
68
+ const handleCheckConstraint = withCharValidation ? checkConstraint : ()=>true;
67
69
  return _react.createElement(_fieldeditorshared.FieldConnector, {
68
70
  field: field,
69
- isInitiallyDisabled: props.isInitiallyDisabled,
70
- isDisabled: props.isDisabled
71
+ isInitiallyDisabled: isInitiallyDisabled,
72
+ isDisabled: isDisabled
71
73
  }, ({ value, errors, disabled, setValue })=>{
72
74
  return _react.createElement("div", {
73
75
  "data-test-id": "single-line-editor"
@@ -77,27 +79,18 @@ function SingleLineEditor(props) {
77
79
  isInvalid: errors.length > 0,
78
80
  isDisabled: disabled,
79
81
  value: value || '',
80
- onFocus: props.onFocus,
81
- onBlur: props.onBlur,
82
+ onFocus: onFocus,
83
+ onBlur: onBlur,
82
84
  onChange: (e)=>{
83
85
  setValue(e.target.value);
84
86
  }
85
- }), props.withCharValidation && _react.createElement("div", {
87
+ }), withCharInformation && _react.createElement("div", {
86
88
  className: _styles.validationRow
87
89
  }, _react.createElement(_fieldeditorshared.CharCounter, {
88
90
  value: value || '',
89
- checkConstraint: checkConstraint
90
- }), _react.createElement(_fieldeditorshared.CharValidation, {
91
+ checkConstraint: handleCheckConstraint
92
+ }), withCharValidation && _react.createElement(_fieldeditorshared.CharValidation, {
91
93
  constraints: constraints
92
- })), props.withCharValidation === false && _react.createElement("div", {
93
- className: _styles.validationRow
94
- }, _react.createElement(_fieldeditorshared.CharCounter, {
95
- value: value || '',
96
- checkConstraint: ()=>true
97
94
  })));
98
95
  });
99
96
  }
100
- SingleLineEditor.defaultProps = {
101
- isInitiallyDisabled: true,
102
- withCharValidation: true
103
- };
@@ -229,4 +229,30 @@ describe('SingleLineEditor', ()=>{
229
229
  expect(getByText('0 characters')).toBeInTheDocument();
230
230
  expect(queryByText('Requires between 100 and 1000 characters')).not.toBeInTheDocument();
231
231
  });
232
+ it('renders no validation message or counter if withCharInformation is false', ()=>{
233
+ const [field] = (0, _fieldeditortestutils.createFakeFieldAPI)((field)=>{
234
+ return {
235
+ ...field,
236
+ type: 'Symbol',
237
+ validations: [
238
+ {
239
+ size: {
240
+ min: 100,
241
+ max: 1000
242
+ }
243
+ }
244
+ ],
245
+ id: 'field-id'
246
+ };
247
+ });
248
+ const { queryByText } = (0, _react1.render)(_react.createElement(_SingleLineEditor.SingleLineEditor, {
249
+ field: field,
250
+ withCharValidation: false,
251
+ isInitiallyDisabled: false,
252
+ withCharInformation: false,
253
+ locales: (0, _fieldeditortestutils.createFakeLocalesAPI)()
254
+ }));
255
+ expect(queryByText('0 characters')).not.toBeInTheDocument();
256
+ expect(queryByText('Requires between 100 and 1000 characters')).not.toBeInTheDocument();
257
+ });
232
258
  });
@@ -6,17 +6,19 @@ function isSupportedFieldTypes(val) {
6
6
  return val === 'Symbol' || val === 'Text';
7
7
  }
8
8
  export function SingleLineEditor(props) {
9
- const { field, locales } = props;
9
+ const { field, isDisabled, isInitiallyDisabled = true, locales, onBlur, onFocus, withCharInformation = true, withCharValidation = true } = props;
10
+ console.log(`isDisabled => `, isDisabled);
10
11
  if (!isSupportedFieldTypes(field.type)) {
11
12
  throw new Error(`"${field.type}" field type is not supported by SingleLineEditor`);
12
13
  }
13
14
  const constraints = ConstraintsUtils.fromFieldValidations(field.validations, field.type);
14
15
  const checkConstraint = ConstraintsUtils.makeChecker(constraints);
15
16
  const direction = locales.direction[field.locale] || 'ltr';
17
+ const handleCheckConstraint = withCharValidation ? checkConstraint : ()=>true;
16
18
  return React.createElement(FieldConnector, {
17
19
  field: field,
18
- isInitiallyDisabled: props.isInitiallyDisabled,
19
- isDisabled: props.isDisabled
20
+ isInitiallyDisabled: isInitiallyDisabled,
21
+ isDisabled: isDisabled
20
22
  }, ({ value, errors, disabled, setValue })=>{
21
23
  return React.createElement("div", {
22
24
  "data-test-id": "single-line-editor"
@@ -26,27 +28,18 @@ export function SingleLineEditor(props) {
26
28
  isInvalid: errors.length > 0,
27
29
  isDisabled: disabled,
28
30
  value: value || '',
29
- onFocus: props.onFocus,
30
- onBlur: props.onBlur,
31
+ onFocus: onFocus,
32
+ onBlur: onBlur,
31
33
  onChange: (e)=>{
32
34
  setValue(e.target.value);
33
35
  }
34
- }), props.withCharValidation && React.createElement("div", {
36
+ }), withCharInformation && React.createElement("div", {
35
37
  className: styles.validationRow
36
38
  }, React.createElement(CharCounter, {
37
39
  value: value || '',
38
- checkConstraint: checkConstraint
39
- }), React.createElement(CharValidation, {
40
+ checkConstraint: handleCheckConstraint
41
+ }), withCharValidation && React.createElement(CharValidation, {
40
42
  constraints: constraints
41
- })), props.withCharValidation === false && React.createElement("div", {
42
- className: styles.validationRow
43
- }, React.createElement(CharCounter, {
44
- value: value || '',
45
- checkConstraint: ()=>true
46
43
  })));
47
44
  });
48
45
  }
49
- SingleLineEditor.defaultProps = {
50
- isInitiallyDisabled: true,
51
- withCharValidation: true
52
- };
@@ -184,4 +184,30 @@ describe('SingleLineEditor', ()=>{
184
184
  expect(getByText('0 characters')).toBeInTheDocument();
185
185
  expect(queryByText('Requires between 100 and 1000 characters')).not.toBeInTheDocument();
186
186
  });
187
+ it('renders no validation message or counter if withCharInformation is false', ()=>{
188
+ const [field] = createFakeFieldAPI((field)=>{
189
+ return {
190
+ ...field,
191
+ type: 'Symbol',
192
+ validations: [
193
+ {
194
+ size: {
195
+ min: 100,
196
+ max: 1000
197
+ }
198
+ }
199
+ ],
200
+ id: 'field-id'
201
+ };
202
+ });
203
+ const { queryByText } = render(React.createElement(SingleLineEditor, {
204
+ field: field,
205
+ withCharValidation: false,
206
+ isInitiallyDisabled: false,
207
+ withCharInformation: false,
208
+ locales: createFakeLocalesAPI()
209
+ }));
210
+ expect(queryByText('0 characters')).not.toBeInTheDocument();
211
+ expect(queryByText('Requires between 100 and 1000 characters')).not.toBeInTheDocument();
212
+ });
187
213
  });
@@ -4,15 +4,19 @@ export interface SingleLineEditorProps {
4
4
  /**
5
5
  * is the field disabled initially
6
6
  */
7
- isInitiallyDisabled: boolean;
7
+ isInitiallyDisabled?: boolean;
8
8
  /**
9
9
  * is the field manually disabled
10
10
  */
11
11
  isDisabled?: boolean;
12
+ /**
13
+ * whether validations should be rendered or not.
14
+ */
15
+ withCharInformation?: boolean;
12
16
  /**
13
17
  * whether char validation should be shown or not
14
18
  */
15
- withCharValidation: boolean;
19
+ withCharValidation?: boolean;
16
20
  /**
17
21
  * sdk.field
18
22
  */
@@ -31,9 +35,3 @@ export interface SingleLineEditorProps {
31
35
  onFocus?: (event: React.FocusEvent<HTMLInputElement>) => void;
32
36
  }
33
37
  export declare function SingleLineEditor(props: SingleLineEditorProps): React.JSX.Element;
34
- export declare namespace SingleLineEditor {
35
- var defaultProps: {
36
- isInitiallyDisabled: boolean;
37
- withCharValidation: boolean;
38
- };
39
- }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@contentful/field-editor-single-line",
3
- "version": "1.4.16",
3
+ "version": "1.5.0",
4
4
  "main": "dist/cjs/index.js",
5
5
  "module": "dist/esm/index.js",
6
6
  "types": "dist/types/index.d.ts",
@@ -50,5 +50,5 @@
50
50
  "publishConfig": {
51
51
  "registry": "https://npm.pkg.github.com/"
52
52
  },
53
- "gitHead": "5744bed7b566437cb831312e84af7444b9047b69"
53
+ "gitHead": "aada768201894d98a6a71baa06696f6add49b692"
54
54
  }