@ukhomeoffice/cop-react-form-renderer 4.43.0 → 4.44.0-alpha

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.
@@ -73,4 +73,61 @@ describe('utils.Component.get', function () {
73
73
  value: 'Some text'
74
74
  });
75
75
  });
76
+ it('should return an appropriately rendered textarea component with a character count', function () {
77
+ var ID = 'test-id';
78
+ var FIELD_ID = 'field-id';
79
+ var LABEL = 'label';
80
+ var ROWS = 13;
81
+ var ON_CHANGE_CALLS = [];
82
+
83
+ var ON_CHANGE = function ON_CHANGE(e) {
84
+ ON_CHANGE_CALLS.push(e.target);
85
+ };
86
+
87
+ var COMPONENT = {
88
+ type: _models.ComponentTypes.TEXT_AREA,
89
+ id: ID,
90
+ fieldId: FIELD_ID,
91
+ label: LABEL,
92
+ rows: ROWS,
93
+ onChange: ON_CHANGE,
94
+ 'data-testid': ID,
95
+ showCharacterCount: true
96
+ };
97
+
98
+ var _render2 = (0, _react.render)((0, _getComponent.default)(COMPONENT)),
99
+ container = _render2.container;
100
+
101
+ var _getAllByTestId3 = (0, _react.getAllByTestId)(container, ID),
102
+ _getAllByTestId4 = _slicedToArray(_getAllByTestId3, 2),
103
+ formGroup = _getAllByTestId4[0],
104
+ textarea = _getAllByTestId4[1];
105
+
106
+ expect(formGroup.tagName).toEqual('DIV');
107
+ expect(formGroup.classList).toContain('govuk-form-group');
108
+ var label = formGroup.childNodes[0].childNodes[0].childNodes[0];
109
+ expect(label.innerHTML).toContain(LABEL);
110
+ expect(label.getAttribute('for')).toEqual(ID);
111
+ expect(textarea.tagName).toEqual('TEXTAREA');
112
+ expect(textarea.classList).toContain('govuk-textarea');
113
+ expect(textarea.getAttribute('rows')).toEqual("".concat(ROWS));
114
+ expect(textarea.id).toEqual(ID);
115
+
116
+ _react.fireEvent.change(textarea, {
117
+ target: {
118
+ name: FIELD_ID,
119
+ value: 'Some text'
120
+ }
121
+ });
122
+
123
+ expect(ON_CHANGE_CALLS.length).toEqual(1);
124
+ expect(ON_CHANGE_CALLS[0]).toMatchObject({
125
+ name: FIELD_ID,
126
+ value: 'Some text'
127
+ });
128
+ var characterCount = formGroup.childNodes[0].childNodes[2];
129
+ expect(characterCount.tagName).toEqual('DIV');
130
+ expect(characterCount.classList).toContain('govuk-hint');
131
+ expect(characterCount.textContent).toEqual('You have 1000 characters remaining');
132
+ });
76
133
  });
@@ -25,6 +25,8 @@ var _validateRegex = _interopRequireDefault(require("./validateRegex"));
25
25
 
26
26
  var _validateRequired = _interopRequireDefault(require("./validateRequired"));
27
27
 
28
+ var _validateTextarea = _interopRequireDefault(require("./validateTextarea"));
29
+
28
30
  var _validateTime = _interopRequireDefault(require("./validateTime"));
29
31
 
30
32
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
@@ -102,6 +104,10 @@ var validateComponent = function validateComponent(component, outerData, formDat
102
104
  error = (0, _validateMultifile.default)(value, component.custom_errors);
103
105
  break;
104
106
 
107
+ case _models.ComponentTypes.TEXT_AREA:
108
+ error = (0, _validateTextarea.default)(component.label, value, component.showCharacterCount, component.custom_errors, component.maxLength);
109
+ break;
110
+
105
111
  default:
106
112
  break;
107
113
  }
@@ -0,0 +1,45 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = void 0;
7
+
8
+ /**
9
+ * Validates a text area when showCharacterCount property has been set.
10
+ *
11
+ * @param {string} label The label to use in any error message.
12
+ * @param {*} value The value to validate.
13
+ * @param {string} showCharacterCount The flag to enable character count validation.
14
+ * @param {array} customErrors An array of custom errors for the component.
15
+ * @param {number} maxLength The maximum allowable number of characters (by default, this is 1000 characters).
16
+ * @returns An error if the value exceeds the maximum allowable number of characters.
17
+ */
18
+ var validateTextarea = function validateTextarea(label, value, showCharacterCount, customErrors) {
19
+ var maxLength = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : 1000;
20
+
21
+ if (!showCharacterCount) {
22
+ return undefined;
23
+ }
24
+
25
+ var hasError = value.length > maxLength;
26
+
27
+ if (hasError) {
28
+ if (Array.isArray(customErrors)) {
29
+ var result = customErrors.filter(function (error) {
30
+ return error.type === 'length';
31
+ });
32
+
33
+ if (result && result.length > 0 && result[0].message) {
34
+ return result[0].message;
35
+ }
36
+ }
37
+
38
+ return "".concat(label, " must be ").concat(maxLength, " characters or less");
39
+ }
40
+
41
+ return undefined;
42
+ };
43
+
44
+ var _default = validateTextarea;
45
+ exports.default = _default;
@@ -0,0 +1,44 @@
1
+ "use strict";
2
+
3
+ var _validateTextarea = _interopRequireDefault(require("./validateTextarea"));
4
+
5
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
6
+
7
+ describe('utils', function () {
8
+ describe('Validate', function () {
9
+ describe('textarea', function () {
10
+ var MAX_LENGTH = 10;
11
+ var LABEL = 'This is a custom label';
12
+ var CUSTOM_ERROR = "The value must be ".concat(MAX_LENGTH, " characters or less");
13
+ var ERROR_MSG = "".concat(LABEL, " must be ").concat(MAX_LENGTH, " characters or less");
14
+ var CUSTOM_ERRORS = [{
15
+ type: 'length',
16
+ message: CUSTOM_ERROR
17
+ }];
18
+ it('should return no error when the character count flag is disabled', function () {
19
+ var VALUE = 'ALPHA BRAVO';
20
+ expect((0, _validateTextarea.default)(LABEL, VALUE, false, CUSTOM_ERRORS, MAX_LENGTH)).toBeUndefined();
21
+ });
22
+ it('should return no error when value length has not exceeded the maximum allowable character length', function () {
23
+ var VALUE = 'ALPHA BRA';
24
+ expect((0, _validateTextarea.default)(LABEL, VALUE, true, CUSTOM_ERRORS, MAX_LENGTH)).toBeUndefined();
25
+ });
26
+ it('should return no error when value length has not exceeded the default maximum allowable character length', function () {
27
+ var VALUE = 'ALPHA BRAVO';
28
+ expect((0, _validateTextarea.default)(LABEL, VALUE, true, undefined)).toBeUndefined();
29
+ });
30
+ it('should return an error when value length has exceeded the maximum allowable character length', function () {
31
+ var VALUE = 'ALPHA BRAVO';
32
+ var error = (0, _validateTextarea.default)(LABEL, VALUE, true, undefined, MAX_LENGTH);
33
+ expect(error).toBeDefined();
34
+ expect(error).toEqual(ERROR_MSG);
35
+ });
36
+ it('should return a custom error when one is provided', function () {
37
+ var VALUE = 'ALPHA BRAVO CHARLIE';
38
+ var error = (0, _validateTextarea.default)(LABEL, VALUE, true, CUSTOM_ERRORS, MAX_LENGTH);
39
+ expect(error).toBeDefined();
40
+ expect(error).toEqual(CUSTOM_ERROR);
41
+ });
42
+ });
43
+ });
44
+ });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ukhomeoffice/cop-react-form-renderer",
3
- "version": "4.43.0",
3
+ "version": "4.44.0-alpha",
4
4
  "private": false,
5
5
  "scripts": {
6
6
  "clean": "rimraf dist",
@@ -16,7 +16,7 @@
16
16
  "post-compile": "rimraf dist/*.test.* dist/**/*.test.* dist/**/*.stories.* dist/docs dist/assets"
17
17
  },
18
18
  "dependencies": {
19
- "@ukhomeoffice/cop-react-components": "2.9.1",
19
+ "@ukhomeoffice/cop-react-components": "2.10.0",
20
20
  "axios": "^0.23.0",
21
21
  "dayjs": "^1.11.0",
22
22
  "govuk-frontend": "^4.3.1",