@conform-to/react 0.6.0-pre.0 → 0.6.1

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/hooks.js CHANGED
@@ -13,43 +13,40 @@ var react = require('react');
13
13
  * @see https://conform.guide/api/react#useform
14
14
  */
15
15
  function useForm() {
16
- var _config$state;
16
+ var _config$lastSubmissio, _config$ref, _ref2, _config$lastSubmissio2;
17
17
  var config = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
18
18
  var configRef = react.useRef(config);
19
- var ref = react.useRef(null);
20
- var [lastSubmission, setLastSubmission] = react.useState((_config$state = config.state) !== null && _config$state !== void 0 ? _config$state : null);
21
- var [error, setError] = react.useState(() => {
22
- if (!config.state) {
23
- return '';
19
+ var formRef = react.useRef(null);
20
+ var [lastSubmission, setLastSubmission] = react.useState((_config$lastSubmissio = config.lastSubmission) !== null && _config$lastSubmissio !== void 0 ? _config$lastSubmissio : null);
21
+ var [errors, setErrors] = react.useState(() => {
22
+ if (!config.lastSubmission) {
23
+ return [];
24
24
  }
25
- var message = config.state.error[''];
26
- return dom.getValidationMessage(message);
25
+ return [].concat(config.lastSubmission.error['']);
27
26
  });
28
- var [uncontrolledState, setUncontrolledState] = react.useState(() => {
29
- var submission = config.state;
27
+ var initialError = react.useMemo(() => {
28
+ var submission = config.lastSubmission;
30
29
  if (!submission) {
31
- return {
32
- defaultValue: config.defaultValue
33
- };
30
+ return {};
34
31
  }
35
- return {
36
- defaultValue: submission.payload,
37
- initialError: Object.entries(submission.error).reduce((result, _ref) => {
38
- var [name, message] = _ref;
39
- if (name !== '' && dom.shouldValidate(submission.intent, name)) {
40
- result[name] = message;
41
- }
42
- return result;
43
- }, {})
44
- };
45
- });
46
- var fieldsetConfig = _rollupPluginBabelHelpers.objectSpread2(_rollupPluginBabelHelpers.objectSpread2({}, uncontrolledState), {}, {
32
+ var scope = dom.getScope(submission.intent);
33
+ return Object.entries(submission.error).reduce((result, _ref) => {
34
+ var [name, message] = _ref;
35
+ if (name !== '' && (scope === null || scope === name)) {
36
+ result[name] = message;
37
+ }
38
+ return result;
39
+ }, {});
40
+ }, [config.lastSubmission]);
41
+ var ref = (_config$ref = config.ref) !== null && _config$ref !== void 0 ? _config$ref : formRef;
42
+ var fieldset = useFieldset(ref, {
43
+ defaultValue: (_ref2 = (_config$lastSubmissio2 = config.lastSubmission) === null || _config$lastSubmissio2 === void 0 ? void 0 : _config$lastSubmissio2.payload) !== null && _ref2 !== void 0 ? _ref2 : config.defaultValue,
44
+ initialError,
47
45
  constraint: config.constraint,
48
46
  form: config.id
49
47
  });
50
- var fieldset = useFieldset(ref, fieldsetConfig);
51
48
  var [noValidate, setNoValidate] = react.useState(config.noValidate || !config.fallbackNative);
52
- react.useEffect(() => {
49
+ useSafeLayoutEffect(() => {
53
50
  configRef.current = config;
54
51
  });
55
52
  react.useEffect(() => {
@@ -57,7 +54,7 @@ function useForm() {
57
54
  }, []);
58
55
  react.useEffect(() => {
59
56
  var form = ref.current;
60
- var submission = config.state;
57
+ var submission = config.lastSubmission;
61
58
  if (!form || !submission) {
62
59
  return;
63
60
  }
@@ -68,24 +65,29 @@ function useForm() {
68
65
  }));
69
66
  }
70
67
  setLastSubmission(submission);
71
- }, [config.state]);
68
+ }, [ref, config.lastSubmission]);
72
69
  react.useEffect(() => {
73
70
  var form = ref.current;
74
71
  if (!form || !lastSubmission) {
75
72
  return;
76
73
  }
77
- dom.reportSubmission(ref.current, lastSubmission);
78
- }, [lastSubmission]);
74
+ dom.reportSubmission(form, lastSubmission);
75
+ }, [ref, lastSubmission]);
79
76
  react.useEffect(() => {
80
77
  // Revalidate the form when input value is changed
81
78
  var handleInput = event => {
82
79
  var field = event.target;
83
80
  var form = ref.current;
84
81
  var formConfig = configRef.current;
82
+ var {
83
+ initialReport = 'onSubmit',
84
+ shouldValidate = initialReport === 'onChange' ? 'onInput' : initialReport,
85
+ shouldRevalidate = 'onInput'
86
+ } = formConfig;
85
87
  if (!form || !dom.isFieldElement(field) || field.form !== form) {
86
88
  return;
87
89
  }
88
- if (field.dataset.conformTouched || formConfig.initialReport === 'onChange') {
90
+ if (field.dataset.conformTouched ? shouldRevalidate === 'onInput' : shouldValidate === 'onInput') {
89
91
  dom.requestIntent(form, dom.validate(field.name));
90
92
  }
91
93
  };
@@ -93,27 +95,31 @@ function useForm() {
93
95
  var field = event.target;
94
96
  var form = ref.current;
95
97
  var formConfig = configRef.current;
98
+ var {
99
+ initialReport = 'onSubmit',
100
+ shouldValidate = initialReport === 'onChange' ? 'onInput' : initialReport,
101
+ shouldRevalidate = 'onInput'
102
+ } = formConfig;
96
103
  if (!form || !dom.isFieldElement(field) || field.form !== form) {
97
104
  return;
98
105
  }
99
- if (formConfig.initialReport === 'onBlur' && !field.dataset.conformTouched) {
106
+ if (field.dataset.conformTouched ? shouldRevalidate === 'onBlur' : shouldValidate === 'onBlur') {
100
107
  dom.requestIntent(form, dom.validate(field.name));
101
108
  }
102
109
  };
103
110
  var handleInvalid = event => {
104
- var form = dom.getFormElement(ref.current);
111
+ var form = ref.current;
105
112
  var field = event.target;
106
- if (!form || !dom.isFieldElement(field) || field.form !== form || field.name !== '__form__') {
113
+ if (!form || !dom.isFieldElement(field) || field.form !== form || field.name !== dom.FORM_ERROR_ELEMENT_NAME) {
107
114
  return;
108
115
  }
109
116
  event.preventDefault();
110
117
  if (field.dataset.conformTouched) {
111
- setError(field.validationMessage);
118
+ setErrors(dom.getErrors(field.validationMessage));
112
119
  }
113
120
  };
114
121
  var handleReset = event => {
115
122
  var form = ref.current;
116
- var formConfig = configRef.current;
117
123
  if (!form || event.target !== form) {
118
124
  return;
119
125
  }
@@ -122,22 +128,11 @@ function useForm() {
122
128
  for (var field of form.elements) {
123
129
  if (dom.isFieldElement(field)) {
124
130
  delete field.dataset.conformTouched;
125
- field.setAttribute('aria-invalid', 'false');
126
131
  field.setCustomValidity('');
127
132
  }
128
133
  }
129
- setError('');
130
- setUncontrolledState({
131
- defaultValue: formConfig.defaultValue
132
- });
134
+ setErrors([]);
133
135
  };
134
-
135
- /**
136
- * The input event handler will be triggered in capturing phase in order to
137
- * allow follow-up action in the bubble phase based on the latest validity
138
- * E.g. `useFieldset` reset the error of valid field after checking the
139
- * validity in the bubble phase.
140
- */
141
136
  document.addEventListener('input', handleInput, true);
142
137
  document.addEventListener('blur', handleBlur, true);
143
138
  document.addEventListener('invalid', handleInvalid, true);
@@ -148,14 +143,13 @@ function useForm() {
148
143
  document.removeEventListener('invalid', handleInvalid, true);
149
144
  document.removeEventListener('reset', handleReset);
150
145
  };
151
- }, []);
146
+ }, [ref]);
152
147
  var form = {
153
- id: config.id,
154
148
  ref,
155
- error,
149
+ error: errors[0],
150
+ errors,
156
151
  props: {
157
152
  ref,
158
- id: config.id,
159
153
  noValidate,
160
154
  onSubmit(event) {
161
155
  var form = event.currentTarget;
@@ -172,11 +166,11 @@ function useForm() {
172
166
  form,
173
167
  formData
174
168
  });
175
- if (!config.noValidate && !(submitter !== null && submitter !== void 0 && submitter.formNoValidate) && Object.entries(submission.error).some(_ref2 => {
176
- var [, message] = _ref2;
177
- return message !== '' && ![].concat(message).includes(dom.VALIDATION_UNDEFINED);
178
- }) || typeof config.onValidate !== 'undefined' && (submission.intent.startsWith('validate') || submission.intent.startsWith('list')) && Object.entries(submission.error).every(_ref3 => {
169
+ if (!config.noValidate && !(submitter !== null && submitter !== void 0 && submitter.formNoValidate) && Object.entries(submission.error).some(_ref3 => {
179
170
  var [, message] = _ref3;
171
+ return message !== '' && ![].concat(message).includes(dom.VALIDATION_UNDEFINED);
172
+ }) || typeof config.onValidate !== 'undefined' && (submission.intent.startsWith('validate') || submission.intent.startsWith('list')) && Object.entries(submission.error).every(_ref4 => {
173
+ var [, message] = _ref4;
180
174
  return ![].concat(message).includes(dom.VALIDATION_UNDEFINED);
181
175
  })) {
182
176
  var listCommand = dom.parseListCommand(submission.intent);
@@ -198,46 +192,41 @@ function useForm() {
198
192
  console.warn(e);
199
193
  }
200
194
  }
201
- },
202
- config: fieldsetConfig
195
+ }
203
196
  };
197
+ if (config.id) {
198
+ form.id = config.id;
199
+ form.errorId = "".concat(config.id, "-error");
200
+ form.props.id = form.id;
201
+ }
202
+ if (form.errorId && form.errors.length > 0) {
203
+ form.props['aria-invalid'] = 'true';
204
+ form.props['aria-describedby'] = form.errorId;
205
+ }
204
206
  return [form, fieldset];
205
207
  }
206
208
 
207
209
  /**
208
- * All the information of the field, including state and config.
210
+ * A set of field configuration
209
211
  */
210
212
 
211
213
  function useFieldset(ref, config) {
212
214
  var configRef = react.useRef(config);
213
- var [uncontrolledState, setUncontrolledState] = react.useState(
214
- // @ts-expect-error
215
- () => {
216
- var _config$defaultValue;
217
- var initialError = {};
218
- for (var [name, message] of Object.entries((_config$initialError = config === null || config === void 0 ? void 0 : config.initialError) !== null && _config$initialError !== void 0 ? _config$initialError : {})) {
219
- var _config$initialError;
220
- var [key, ...paths] = dom.getPaths(name);
221
- if (typeof key === 'string') {
222
- var scopedName = dom.getName(paths);
223
- initialError[key] = _rollupPluginBabelHelpers.objectSpread2(_rollupPluginBabelHelpers.objectSpread2({}, initialError[key]), {}, {
224
- [scopedName]: message
225
- });
226
- }
227
- }
228
- return {
229
- defaultValue: (_config$defaultValue = config === null || config === void 0 ? void 0 : config.defaultValue) !== null && _config$defaultValue !== void 0 ? _config$defaultValue : {},
230
- initialError
231
- };
232
- });
233
215
  var [error, setError] = react.useState(() => {
216
+ var initialError = config === null || config === void 0 ? void 0 : config.initialError;
217
+ if (!initialError) {
218
+ return {};
219
+ }
234
220
  var result = {};
235
- for (var [key, _error] of Object.entries(uncontrolledState.initialError)) {
236
- result[key] = dom.getErrors(dom.getValidationMessage(_error === null || _error === void 0 ? void 0 : _error['']));
221
+ for (var [name, message] of Object.entries(initialError)) {
222
+ var [key, ...paths] = dom.getPaths(name);
223
+ if (typeof key === 'string' && paths.length === 0) {
224
+ result[key] = [].concat(message !== null && message !== void 0 ? message : []);
225
+ }
237
226
  }
238
227
  return result;
239
228
  });
240
- react.useEffect(() => {
229
+ useSafeLayoutEffect(() => {
241
230
  configRef.current = config;
242
231
  });
243
232
  react.useEffect(() => {
@@ -254,10 +243,6 @@ function useFieldset(ref, config) {
254
243
  // Update the error only if the field belongs to the fieldset
255
244
  if (typeof key === 'string' && paths.length === 0) {
256
245
  if (field.dataset.conformTouched) {
257
- // Update the aria attribute only if it is set
258
- if (field.getAttribute('aria-invalid')) {
259
- field.setAttribute('aria-invalid', field.validationMessage !== '' ? 'true' : 'false');
260
- }
261
246
  setError(prev => {
262
247
  var prevMessage = dom.getValidationMessage(prev === null || prev === void 0 ? void 0 : prev[key]);
263
248
  if (prevMessage === field.validationMessage) {
@@ -272,17 +257,10 @@ function useFieldset(ref, config) {
272
257
  }
273
258
  };
274
259
  var resetHandler = event => {
275
- var _fieldsetConfig$defau;
276
260
  var form = dom.getFormElement(ref.current);
277
261
  if (!form || event.target !== form) {
278
262
  return;
279
263
  }
280
- var fieldsetConfig = configRef.current;
281
- setUncontrolledState({
282
- // @ts-expect-error
283
- defaultValue: (_fieldsetConfig$defau = fieldsetConfig === null || fieldsetConfig === void 0 ? void 0 : fieldsetConfig.defaultValue) !== null && _fieldsetConfig$defau !== void 0 ? _fieldsetConfig$defau : {},
284
- initialError: {}
285
- });
286
264
  setError({});
287
265
  };
288
266
 
@@ -302,26 +280,33 @@ function useFieldset(ref, config) {
302
280
  */
303
281
  return new Proxy({}, {
304
282
  get(_target, key) {
305
- var _fieldsetConfig$const;
283
+ var _fieldsetConfig$const, _fieldsetConfig$initi, _fieldsetConfig$defau;
306
284
  if (typeof key !== 'string') {
307
285
  return;
308
286
  }
309
- var fieldsetConfig = config !== null && config !== void 0 ? config : {};
287
+ var fieldsetConfig = config;
310
288
  var constraint = (_fieldsetConfig$const = fieldsetConfig.constraint) === null || _fieldsetConfig$const === void 0 ? void 0 : _fieldsetConfig$const[key];
311
289
  var errors = error === null || error === void 0 ? void 0 : error[key];
312
- var field = {
313
- config: _rollupPluginBabelHelpers.objectSpread2({
314
- name: fieldsetConfig.name ? "".concat(fieldsetConfig.name, ".").concat(key) : key,
315
- defaultValue: uncontrolledState.defaultValue[key],
316
- initialError: uncontrolledState.initialError[key]
317
- }, constraint),
290
+ var initialError = Object.entries((_fieldsetConfig$initi = fieldsetConfig.initialError) !== null && _fieldsetConfig$initi !== void 0 ? _fieldsetConfig$initi : {}).reduce((result, _ref5) => {
291
+ var [name, message] = _ref5;
292
+ var [field, ...paths] = dom.getPaths(name);
293
+ if (field === key) {
294
+ result[dom.getName(paths)] = message;
295
+ }
296
+ return result;
297
+ }, {});
298
+ var field = _rollupPluginBabelHelpers.objectSpread2(_rollupPluginBabelHelpers.objectSpread2({}, constraint), {}, {
299
+ name: fieldsetConfig.name ? "".concat(fieldsetConfig.name, ".").concat(key) : key,
300
+ defaultValue: (_fieldsetConfig$defau = fieldsetConfig.defaultValue) === null || _fieldsetConfig$defau === void 0 ? void 0 : _fieldsetConfig$defau[key],
301
+ initialError,
318
302
  error: errors === null || errors === void 0 ? void 0 : errors[0],
319
303
  errors
320
- };
304
+ });
321
305
  if (fieldsetConfig.form) {
322
- field.config.form = fieldsetConfig.form;
323
- field.config.id = "".concat(fieldsetConfig.form, "-").concat(field.config.name);
324
- field.config.errorId = "".concat(field.config.id, "-error");
306
+ field.form = fieldsetConfig.form;
307
+ field.id = "".concat(fieldsetConfig.form, "-").concat(field.name);
308
+ field.errorId = "".concat(field.id, "-error");
309
+ field.descriptionId = "".concat(field.id, "-description");
325
310
  }
326
311
  return field;
327
312
  }
@@ -336,30 +321,22 @@ function useFieldset(ref, config) {
336
321
  */
337
322
  function useFieldList(ref, config) {
338
323
  var configRef = react.useRef(config);
339
- var [uncontrolledState, setUncontrolledState] = react.useState(() => {
340
- var _config$defaultValue2;
324
+ var [error, setError] = react.useState(() => {
341
325
  var initialError = [];
342
- for (var [name, message] of Object.entries((_config$initialError2 = config === null || config === void 0 ? void 0 : config.initialError) !== null && _config$initialError2 !== void 0 ? _config$initialError2 : {})) {
343
- var _config$initialError2;
326
+ for (var [name, message] of Object.entries((_config$initialError = config === null || config === void 0 ? void 0 : config.initialError) !== null && _config$initialError !== void 0 ? _config$initialError : {})) {
327
+ var _config$initialError;
344
328
  var [index, ...paths] = dom.getPaths(name);
345
- if (typeof index === 'number') {
346
- var scopedName = dom.getName(paths);
347
- initialError[index] = _rollupPluginBabelHelpers.objectSpread2(_rollupPluginBabelHelpers.objectSpread2({}, initialError[index]), {}, {
348
- [scopedName]: message
349
- });
329
+ if (typeof index === 'number' && paths.length === 0) {
330
+ initialError[index] = [].concat(message !== null && message !== void 0 ? message : []);
350
331
  }
351
332
  }
352
- return {
353
- defaultValue: (_config$defaultValue2 = config.defaultValue) !== null && _config$defaultValue2 !== void 0 ? _config$defaultValue2 : [],
354
- initialError
355
- };
333
+ return initialError;
356
334
  });
357
- var [error, setError] = react.useState(() => uncontrolledState.initialError.map(error => dom.getErrors(dom.getValidationMessage(error === null || error === void 0 ? void 0 : error['']))));
358
335
  var [entries, setEntries] = react.useState(() => {
359
- var _config$defaultValue3;
360
- return Object.entries((_config$defaultValue3 = config.defaultValue) !== null && _config$defaultValue3 !== void 0 ? _config$defaultValue3 : [undefined]);
336
+ var _config$defaultValue;
337
+ return Object.entries((_config$defaultValue = config.defaultValue) !== null && _config$defaultValue !== void 0 ? _config$defaultValue : [undefined]);
361
338
  });
362
- react.useEffect(() => {
339
+ useSafeLayoutEffect(() => {
363
340
  configRef.current = config;
364
341
  });
365
342
  react.useEffect(() => {
@@ -433,17 +410,12 @@ function useFieldList(ref, config) {
433
410
  });
434
411
  };
435
412
  var resetHandler = event => {
436
- var _fieldConfig$defaultV, _fieldConfig$defaultV2;
413
+ var _configRef$current$de;
437
414
  var form = dom.getFormElement(ref.current);
438
415
  if (!form || event.target !== form) {
439
416
  return;
440
417
  }
441
- var fieldConfig = configRef.current;
442
- setUncontrolledState({
443
- defaultValue: (_fieldConfig$defaultV = fieldConfig.defaultValue) !== null && _fieldConfig$defaultV !== void 0 ? _fieldConfig$defaultV : [],
444
- initialError: []
445
- });
446
- setEntries(Object.entries((_fieldConfig$defaultV2 = fieldConfig.defaultValue) !== null && _fieldConfig$defaultV2 !== void 0 ? _fieldConfig$defaultV2 : [undefined]));
418
+ setEntries(Object.entries((_configRef$current$de = configRef.current.defaultValue) !== null && _configRef$current$de !== void 0 ? _configRef$current$de : [undefined]));
447
419
  setError([]);
448
420
  };
449
421
 
@@ -458,25 +430,34 @@ function useFieldList(ref, config) {
458
430
  document.removeEventListener('reset', resetHandler);
459
431
  };
460
432
  }, [ref]);
461
- return entries.map((_ref4, index) => {
462
- var [key, defaultValue] = _ref4;
433
+ return entries.map((_ref6, index) => {
434
+ var _config$initialError2, _config$defaultValue2;
435
+ var [key, defaultValue] = _ref6;
463
436
  var errors = error[index];
437
+ var initialError = Object.entries((_config$initialError2 = config.initialError) !== null && _config$initialError2 !== void 0 ? _config$initialError2 : {}).reduce((result, _ref7) => {
438
+ var [name, message] = _ref7;
439
+ var [field, ...paths] = dom.getPaths(name);
440
+ if (field === index) {
441
+ result[dom.getName(paths)] = message;
442
+ }
443
+ return result;
444
+ }, {});
464
445
  var fieldConfig = {
465
446
  name: "".concat(config.name, "[").concat(index, "]"),
466
- defaultValue: defaultValue !== null && defaultValue !== void 0 ? defaultValue : uncontrolledState.defaultValue[index],
467
- initialError: uncontrolledState.initialError[index]
447
+ defaultValue: defaultValue !== null && defaultValue !== void 0 ? defaultValue : (_config$defaultValue2 = config.defaultValue) === null || _config$defaultValue2 === void 0 ? void 0 : _config$defaultValue2[index],
448
+ initialError,
449
+ error: errors === null || errors === void 0 ? void 0 : errors[0],
450
+ errors
468
451
  };
469
452
  if (config.form) {
470
453
  fieldConfig.form = config.form;
471
454
  fieldConfig.id = "".concat(config.form, "-").concat(config.name);
472
455
  fieldConfig.errorId = "".concat(fieldConfig.id, "-error");
456
+ fieldConfig.descriptionId = "".concat(fieldConfig.id, "-description");
473
457
  }
474
- return {
475
- key,
476
- error: errors === null || errors === void 0 ? void 0 : errors[0],
477
- errors,
478
- config: fieldConfig
479
- };
458
+ return _rollupPluginBabelHelpers.objectSpread2({
459
+ key
460
+ }, fieldConfig);
480
461
  });
481
462
  }
482
463
 
package/index.d.ts CHANGED
@@ -1,3 +1,3 @@
1
- export { type FieldConfig, type FieldsetConstraint, type Submission, getFormElements, list, validate, requestIntent, requestSubmit, parse, validateConstraint, } from '@conform-to/dom';
2
- export * from './hooks';
1
+ export { type FieldConfig, type FieldsetConstraint, type Submission, parse, validateConstraint, list, validate, requestIntent, isFieldElement, } from '@conform-to/dom';
2
+ export { type Fieldset, type FieldsetConfig, type FormConfig, useForm, useFieldset, useFieldList, useInputEvent, } from './hooks';
3
3
  export * as conform from './helpers';
package/index.js CHANGED
@@ -8,9 +8,9 @@ var helpers = require('./helpers.js');
8
8
 
9
9
 
10
10
 
11
- Object.defineProperty(exports, 'getFormElements', {
11
+ Object.defineProperty(exports, 'isFieldElement', {
12
12
  enumerable: true,
13
- get: function () { return dom.getFormElements; }
13
+ get: function () { return dom.isFieldElement; }
14
14
  });
15
15
  Object.defineProperty(exports, 'list', {
16
16
  enumerable: true,
@@ -24,10 +24,6 @@ Object.defineProperty(exports, 'requestIntent', {
24
24
  enumerable: true,
25
25
  get: function () { return dom.requestIntent; }
26
26
  });
27
- Object.defineProperty(exports, 'requestSubmit', {
28
- enumerable: true,
29
- get: function () { return dom.requestSubmit; }
30
- });
31
27
  Object.defineProperty(exports, 'validate', {
32
28
  enumerable: true,
33
29
  get: function () { return dom.validate; }
package/module/helpers.js CHANGED
@@ -1,4 +1,5 @@
1
- export { VALIDATION_SKIPPED, VALIDATION_UNDEFINED } from '@conform-to/dom';
1
+ import { objectSpread2 as _objectSpread2 } from './_virtual/_rollupPluginBabelHelpers.js';
2
+ export { INTENT, VALIDATION_SKIPPED, VALIDATION_UNDEFINED } from '@conform-to/dom';
2
3
 
3
4
  /**
4
5
  * Style to make the input element visually hidden
@@ -15,88 +16,69 @@ var hiddenStyle = {
15
16
  whiteSpace: 'nowrap',
16
17
  border: 0
17
18
  };
18
- function input(config) {
19
- var _config$initialError;
20
- var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
21
- var attributes = {
19
+ function getFormControlProps(config, options) {
20
+ var _config$error;
21
+ var props = {
22
22
  id: config.id,
23
- type: options.type,
24
23
  name: config.name,
25
24
  form: config.form,
26
- required: config.required,
25
+ required: config.required
26
+ };
27
+ if (config.id) {
28
+ props.id = config.id;
29
+ }
30
+ if (config.descriptionId && options !== null && options !== void 0 && options.description) {
31
+ props['aria-describedby'] = config.descriptionId;
32
+ }
33
+ if (config.errorId && (_config$error = config.error) !== null && _config$error !== void 0 && _config$error.length) {
34
+ props['aria-invalid'] = true;
35
+ props['aria-describedby'] = config.descriptionId && options !== null && options !== void 0 && options.description ? "".concat(config.errorId, " ").concat(config.descriptionId) : config.errorId;
36
+ }
37
+ if (config.initialError && Object.entries(config.initialError).length > 0) {
38
+ props.autoFocus = true;
39
+ }
40
+ if (options !== null && options !== void 0 && options.hidden) {
41
+ props.style = hiddenStyle;
42
+ props.tabIndex = -1;
43
+ props['aria-hidden'] = true;
44
+ }
45
+ return props;
46
+ }
47
+ function input(config) {
48
+ var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
49
+ var props = _objectSpread2(_objectSpread2({}, getFormControlProps(config, options)), {}, {
50
+ type: options.type,
27
51
  minLength: config.minLength,
28
52
  maxLength: config.maxLength,
29
53
  min: config.min,
30
54
  max: config.max,
31
55
  step: config.step,
32
56
  pattern: config.pattern,
33
- multiple: config.multiple,
34
- 'aria-invalid': Boolean((_config$initialError = config.initialError) === null || _config$initialError === void 0 ? void 0 : _config$initialError.length),
35
- 'aria-describedby': config.errorId
36
- };
37
- if (options !== null && options !== void 0 && options.hidden) {
38
- attributes.style = hiddenStyle;
39
- attributes.tabIndex = -1;
40
- attributes['aria-hidden'] = true;
41
- }
42
- if (config.initialError && Object.entries(config.initialError).length > 0) {
43
- attributes.autoFocus = true;
44
- }
57
+ multiple: config.multiple
58
+ });
45
59
  if (options.type === 'checkbox' || options.type === 'radio') {
46
60
  var _options$value;
47
- attributes.value = (_options$value = options.value) !== null && _options$value !== void 0 ? _options$value : 'on';
48
- attributes.defaultChecked = config.defaultValue === attributes.value;
61
+ props.value = (_options$value = options.value) !== null && _options$value !== void 0 ? _options$value : 'on';
62
+ props.defaultChecked = config.defaultValue === props.value;
49
63
  } else if (options.type !== 'file') {
50
- attributes.defaultValue = config.defaultValue;
64
+ props.defaultValue = config.defaultValue;
51
65
  }
52
- return attributes;
66
+ return props;
53
67
  }
54
68
  function select(config, options) {
55
- var _config$defaultValue, _config$initialError2;
56
- var attributes = {
57
- id: config.id,
58
- name: config.name,
59
- form: config.form,
60
- defaultValue: config.multiple ? Array.isArray(config.defaultValue) ? config.defaultValue : [] : "".concat((_config$defaultValue = config.defaultValue) !== null && _config$defaultValue !== void 0 ? _config$defaultValue : ''),
61
- required: config.required,
62
- multiple: config.multiple,
63
- 'aria-invalid': Boolean((_config$initialError2 = config.initialError) === null || _config$initialError2 === void 0 ? void 0 : _config$initialError2.length),
64
- 'aria-describedby': config.errorId
65
- };
66
- if (options !== null && options !== void 0 && options.hidden) {
67
- attributes.style = hiddenStyle;
68
- attributes.tabIndex = -1;
69
- attributes['aria-hidden'] = true;
70
- }
71
- if (config.initialError && Object.entries(config.initialError).length > 0) {
72
- attributes.autoFocus = true;
73
- }
74
- return attributes;
69
+ var props = _objectSpread2(_objectSpread2({}, getFormControlProps(config, options)), {}, {
70
+ defaultValue: config.defaultValue,
71
+ multiple: config.multiple
72
+ });
73
+ return props;
75
74
  }
76
75
  function textarea(config, options) {
77
- var _config$defaultValue2, _config$initialError3;
78
- var attributes = {
79
- id: config.id,
80
- name: config.name,
81
- form: config.form,
82
- defaultValue: "".concat((_config$defaultValue2 = config.defaultValue) !== null && _config$defaultValue2 !== void 0 ? _config$defaultValue2 : ''),
83
- required: config.required,
76
+ var props = _objectSpread2(_objectSpread2({}, getFormControlProps(config, options)), {}, {
77
+ defaultValue: config.defaultValue,
84
78
  minLength: config.minLength,
85
- maxLength: config.maxLength,
86
- autoFocus: Boolean(config.initialError),
87
- 'aria-invalid': Boolean((_config$initialError3 = config.initialError) === null || _config$initialError3 === void 0 ? void 0 : _config$initialError3.length),
88
- 'aria-describedby': config.errorId
89
- };
90
- if (options !== null && options !== void 0 && options.hidden) {
91
- attributes.style = hiddenStyle;
92
- attributes.tabIndex = -1;
93
- attributes['aria-hidden'] = true;
94
- }
95
- if (config.initialError && Object.entries(config.initialError).length > 0) {
96
- attributes.autoFocus = true;
97
- }
98
- return attributes;
79
+ maxLength: config.maxLength
80
+ });
81
+ return props;
99
82
  }
100
- var intent = '__intent__';
101
83
 
102
- export { input, intent, select, textarea };
84
+ export { input, select, textarea };