@conform-to/react 0.4.0-pre.3 → 0.4.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/module/base.js ADDED
@@ -0,0 +1,107 @@
1
+ import { objectWithoutProperties as _objectWithoutProperties, objectSpread2 as _objectSpread2 } from './_virtual/_rollupPluginBabelHelpers.js';
2
+ import { forwardRef, createElement } from 'react';
3
+
4
+ var _excluded = ["hidden", "className", "style"];
5
+ /**
6
+ * Triggering react custom change event
7
+ * Solution based on dom-testing-library
8
+ * @see https://github.com/facebook/react/issues/10135#issuecomment-401496776
9
+ * @see https://github.com/testing-library/dom-testing-library/blob/main/src/events.js#L104-L123
10
+ */
11
+ function setNativeValue(element, value) {
12
+ var {
13
+ set: valueSetter
14
+ } = Object.getOwnPropertyDescriptor(element, 'value') || {};
15
+ var prototype = Object.getPrototypeOf(element);
16
+ var {
17
+ set: prototypeValueSetter
18
+ } = Object.getOwnPropertyDescriptor(prototype, 'value') || {};
19
+ if (prototypeValueSetter && valueSetter !== prototypeValueSetter) {
20
+ prototypeValueSetter.call(element, value);
21
+ } else {
22
+ if (valueSetter) {
23
+ valueSetter.call(element, value);
24
+ } else {
25
+ throw new Error('The given element does not have a value setter');
26
+ }
27
+ }
28
+ }
29
+ function useInputControl(ref) {
30
+ function getInputElement() {
31
+ var $input = ref.current;
32
+ if (!$input) {
33
+ console.warn('input ref is not available; Maybe you forget to setup the ref?');
34
+ }
35
+ return $input;
36
+ }
37
+ return {
38
+ onChange(eventLikeOrString) {
39
+ var $input = getInputElement();
40
+ var value = typeof eventLikeOrString === 'string' ? eventLikeOrString : eventLikeOrString.target.value;
41
+ if ($input && $input.value !== value) {
42
+ $input.dispatchEvent(new InputEvent('beforeinput', {
43
+ bubbles: true,
44
+ cancelable: true
45
+ }));
46
+ setNativeValue($input, value);
47
+ $input.dispatchEvent(new InputEvent('input', {
48
+ bubbles: true,
49
+ cancelable: true
50
+ }));
51
+ }
52
+ },
53
+ onInput(eventLikeOrString) {
54
+ this.onChange(eventLikeOrString);
55
+ },
56
+ onFocus() {
57
+ var $input = getInputElement();
58
+ if ($input) {
59
+ $input.dispatchEvent(new FocusEvent('focusin', {
60
+ bubbles: true,
61
+ cancelable: true
62
+ }));
63
+ $input.dispatchEvent(new FocusEvent('focus', {
64
+ cancelable: true
65
+ }));
66
+ }
67
+ },
68
+ onBlur() {
69
+ var $input = getInputElement();
70
+ if ($input) {
71
+ $input.dispatchEvent(new FocusEvent('focusout', {
72
+ bubbles: true,
73
+ cancelable: true
74
+ }));
75
+ $input.dispatchEvent(new FocusEvent('blur', {
76
+ cancelable: true
77
+ }));
78
+ }
79
+ }
80
+ };
81
+ }
82
+ function BaseInputImpl(_ref, ref) {
83
+ var {
84
+ hidden = true,
85
+ className,
86
+ style
87
+ } = _ref,
88
+ props = _objectWithoutProperties(_ref, _excluded);
89
+ return /*#__PURE__*/createElement('input', _objectSpread2({
90
+ ref,
91
+ className: hidden ? '' : className,
92
+ style: hidden ? {
93
+ position: 'absolute',
94
+ width: '1px',
95
+ height: '1px',
96
+ padding: 0,
97
+ margin: '-1px',
98
+ overflow: 'hidden',
99
+ clip: 'rect(0,0,0,0)',
100
+ whiteSpace: 'nowrap',
101
+ borderWidth: 0
102
+ } : style
103
+ }, props));
104
+ }
105
+ var BaseInput = /*#__PURE__*/forwardRef(BaseInputImpl);
106
+
107
+ export { BaseInput, useInputControl };
package/module/helpers.js CHANGED
@@ -17,23 +17,19 @@ function input(config) {
17
17
  pattern: config.pattern,
18
18
  multiple: config.multiple
19
19
  };
20
-
21
20
  if (config.initialError && config.initialError.length > 0) {
22
21
  attributes.autoFocus = true;
23
22
  }
24
-
25
23
  if (isCheckboxOrRadio) {
26
24
  attributes.value = value !== null && value !== void 0 ? value : 'on';
27
25
  attributes.defaultChecked = config.defaultValue === attributes.value;
28
26
  } else {
29
27
  attributes.defaultValue = config.defaultValue;
30
28
  }
31
-
32
29
  return attributes;
33
30
  }
34
31
  function select(config) {
35
32
  var _config$defaultValue;
36
-
37
33
  var attributes = {
38
34
  name: config.name,
39
35
  form: config.form,
@@ -41,16 +37,13 @@ function select(config) {
41
37
  required: config.required,
42
38
  multiple: config.multiple
43
39
  };
44
-
45
40
  if (config.initialError && config.initialError.length > 0) {
46
41
  attributes.autoFocus = true;
47
42
  }
48
-
49
43
  return attributes;
50
44
  }
51
45
  function textarea(config) {
52
46
  var _config$defaultValue2;
53
-
54
47
  var attributes = {
55
48
  name: config.name,
56
49
  form: config.form,
@@ -60,11 +53,9 @@ function textarea(config) {
60
53
  maxLength: config.maxLength,
61
54
  autoFocus: Boolean(config.initialError)
62
55
  };
63
-
64
56
  if (config.initialError && config.initialError.length > 0) {
65
57
  attributes.autoFocus = true;
66
58
  }
67
-
68
59
  return attributes;
69
60
  }
70
61