@conform-to/react 0.5.1 → 0.6.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/module/helpers.js CHANGED
@@ -1,3 +1,6 @@
1
+ import { objectSpread2 as _objectSpread2 } from './_virtual/_rollupPluginBabelHelpers.js';
2
+ export { INTENT, VALIDATION_SKIPPED, VALIDATION_UNDEFINED } from '@conform-to/dom';
3
+
1
4
  /**
2
5
  * Style to make the input element visually hidden
3
6
  * Based on the `sr-only` class from tailwindcss
@@ -13,87 +16,66 @@ var hiddenStyle = {
13
16
  whiteSpace: 'nowrap',
14
17
  border: 0
15
18
  };
16
- function input(config) {
17
- var _config$initialError;
18
- var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
19
- var attributes = {
19
+ function getFormControlProps(config, options) {
20
+ var _config$error;
21
+ var props = {
20
22
  id: config.id,
21
- type: options.type,
22
23
  name: config.name,
23
24
  form: config.form,
24
- required: config.required,
25
+ required: config.required
26
+ };
27
+ if (config.id) {
28
+ props.id = config.id;
29
+ props['aria-describedby'] = config.errorId;
30
+ }
31
+ if (config.errorId && (_config$error = config.error) !== null && _config$error !== void 0 && _config$error.length) {
32
+ props['aria-invalid'] = true;
33
+ }
34
+ if (config.initialError && Object.entries(config.initialError).length > 0) {
35
+ props.autoFocus = true;
36
+ }
37
+ if (options !== null && options !== void 0 && options.hidden) {
38
+ props.style = hiddenStyle;
39
+ props.tabIndex = -1;
40
+ props['aria-hidden'] = true;
41
+ }
42
+ return props;
43
+ }
44
+ function input(config) {
45
+ var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
46
+ var props = _objectSpread2(_objectSpread2({}, getFormControlProps(config, options)), {}, {
47
+ type: options.type,
25
48
  minLength: config.minLength,
26
49
  maxLength: config.maxLength,
27
50
  min: config.min,
28
51
  max: config.max,
29
52
  step: config.step,
30
53
  pattern: config.pattern,
31
- multiple: config.multiple,
32
- 'aria-invalid': Boolean((_config$initialError = config.initialError) === null || _config$initialError === void 0 ? void 0 : _config$initialError.length),
33
- 'aria-describedby': config.errorId
34
- };
35
- if (options !== null && options !== void 0 && options.hidden) {
36
- attributes.style = hiddenStyle;
37
- attributes.tabIndex = -1;
38
- attributes['aria-hidden'] = true;
39
- }
40
- if (config.initialError && config.initialError.length > 0) {
41
- attributes.autoFocus = true;
42
- }
54
+ multiple: config.multiple
55
+ });
43
56
  if (options.type === 'checkbox' || options.type === 'radio') {
44
57
  var _options$value;
45
- attributes.value = (_options$value = options.value) !== null && _options$value !== void 0 ? _options$value : 'on';
46
- attributes.defaultChecked = config.defaultValue === attributes.value;
58
+ props.value = (_options$value = options.value) !== null && _options$value !== void 0 ? _options$value : 'on';
59
+ props.defaultChecked = config.defaultValue === props.value;
47
60
  } else if (options.type !== 'file') {
48
- attributes.defaultValue = config.defaultValue;
61
+ props.defaultValue = config.defaultValue;
49
62
  }
50
- return attributes;
63
+ return props;
51
64
  }
52
65
  function select(config, options) {
53
- var _config$defaultValue, _config$initialError2;
54
- var attributes = {
55
- id: config.id,
56
- name: config.name,
57
- form: config.form,
58
- defaultValue: config.multiple ? Array.isArray(config.defaultValue) ? config.defaultValue : [] : "".concat((_config$defaultValue = config.defaultValue) !== null && _config$defaultValue !== void 0 ? _config$defaultValue : ''),
59
- required: config.required,
60
- multiple: config.multiple,
61
- 'aria-invalid': Boolean((_config$initialError2 = config.initialError) === null || _config$initialError2 === void 0 ? void 0 : _config$initialError2.length),
62
- 'aria-describedby': config.errorId
63
- };
64
- if (options !== null && options !== void 0 && options.hidden) {
65
- attributes.style = hiddenStyle;
66
- attributes.tabIndex = -1;
67
- attributes['aria-hidden'] = true;
68
- }
69
- if (config.initialError && config.initialError.length > 0) {
70
- attributes.autoFocus = true;
71
- }
72
- return attributes;
66
+ var props = _objectSpread2(_objectSpread2({}, getFormControlProps(config, options)), {}, {
67
+ defaultValue: config.defaultValue,
68
+ multiple: config.multiple
69
+ });
70
+ return props;
73
71
  }
74
72
  function textarea(config, options) {
75
- var _config$defaultValue2, _config$initialError3;
76
- var attributes = {
77
- id: config.id,
78
- name: config.name,
79
- form: config.form,
80
- defaultValue: "".concat((_config$defaultValue2 = config.defaultValue) !== null && _config$defaultValue2 !== void 0 ? _config$defaultValue2 : ''),
81
- required: config.required,
73
+ var props = _objectSpread2(_objectSpread2({}, getFormControlProps(config, options)), {}, {
74
+ defaultValue: config.defaultValue,
82
75
  minLength: config.minLength,
83
- maxLength: config.maxLength,
84
- autoFocus: Boolean(config.initialError),
85
- 'aria-invalid': Boolean((_config$initialError3 = config.initialError) === null || _config$initialError3 === void 0 ? void 0 : _config$initialError3.length),
86
- 'aria-describedby': config.errorId
87
- };
88
- if (options !== null && options !== void 0 && options.hidden) {
89
- attributes.style = hiddenStyle;
90
- attributes.tabIndex = -1;
91
- attributes['aria-hidden'] = true;
92
- }
93
- if (config.initialError && config.initialError.length > 0) {
94
- attributes.autoFocus = true;
95
- }
96
- return attributes;
76
+ maxLength: config.maxLength
77
+ });
78
+ return props;
97
79
  }
98
80
 
99
81
  export { input, select, textarea };