@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.
@@ -4,17 +4,14 @@ Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
5
  function ownKeys(object, enumerableOnly) {
6
6
  var keys = Object.keys(object);
7
-
8
7
  if (Object.getOwnPropertySymbols) {
9
8
  var symbols = Object.getOwnPropertySymbols(object);
10
9
  enumerableOnly && (symbols = symbols.filter(function (sym) {
11
10
  return Object.getOwnPropertyDescriptor(object, sym).enumerable;
12
11
  })), keys.push.apply(keys, symbols);
13
12
  }
14
-
15
13
  return keys;
16
14
  }
17
-
18
15
  function _objectSpread2(target) {
19
16
  for (var i = 1; i < arguments.length; i++) {
20
17
  var source = null != arguments[i] ? arguments[i] : {};
@@ -24,10 +21,8 @@ function _objectSpread2(target) {
24
21
  Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
25
22
  });
26
23
  }
27
-
28
24
  return target;
29
25
  }
30
-
31
26
  function _defineProperty(obj, key, value) {
32
27
  if (key in obj) {
33
28
  Object.defineProperty(obj, key, {
@@ -39,7 +34,6 @@ function _defineProperty(obj, key, value) {
39
34
  } else {
40
35
  obj[key] = value;
41
36
  }
42
-
43
37
  return obj;
44
38
  }
45
39
 
package/base.d.ts ADDED
@@ -0,0 +1,17 @@
1
+ import { type InputHTMLAttributes, type RefObject } from 'react';
2
+ declare type EventLikeOrString = {
3
+ target: {
4
+ value: string;
5
+ };
6
+ } | string;
7
+ declare type InputControl = {
8
+ onChange: (eventLikeOrString: EventLikeOrString) => void;
9
+ onInput: (eventLikeOrString: EventLikeOrString) => void;
10
+ onFocus: () => void;
11
+ onBlur: () => void;
12
+ };
13
+ export declare function useInputControl(ref: RefObject<HTMLInputElement | HTMLSelectElement | HTMLTextAreaElement>): InputControl;
14
+ export declare const BaseInput: import("react").ForwardRefExoticComponent<{
15
+ name: string;
16
+ } & Omit<InputHTMLAttributes<HTMLInputElement>, "name" | "type" | "value" | "defaultValue"> & import("react").RefAttributes<HTMLInputElement>>;
17
+ export {};
package/base.js ADDED
@@ -0,0 +1,112 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ var _rollupPluginBabelHelpers = require('./_virtual/_rollupPluginBabelHelpers.js');
6
+ var react = require('react');
7
+
8
+ var _excluded = ["hidden", "className", "style"];
9
+ /**
10
+ * Triggering react custom change event
11
+ * Solution based on dom-testing-library
12
+ * @see https://github.com/facebook/react/issues/10135#issuecomment-401496776
13
+ * @see https://github.com/testing-library/dom-testing-library/blob/main/src/events.js#L104-L123
14
+ */
15
+ function setNativeValue(element, value) {
16
+ var {
17
+ set: valueSetter
18
+ } = Object.getOwnPropertyDescriptor(element, 'value') || {};
19
+ var prototype = Object.getPrototypeOf(element);
20
+ var {
21
+ set: prototypeValueSetter
22
+ } = Object.getOwnPropertyDescriptor(prototype, 'value') || {};
23
+ if (prototypeValueSetter && valueSetter !== prototypeValueSetter) {
24
+ prototypeValueSetter.call(element, value);
25
+ } else {
26
+ if (valueSetter) {
27
+ valueSetter.call(element, value);
28
+ } else {
29
+ throw new Error('The given element does not have a value setter');
30
+ }
31
+ }
32
+ }
33
+ function useInputControl(ref) {
34
+ function getInputElement() {
35
+ var $input = ref.current;
36
+ if (!$input) {
37
+ console.warn('input ref is not available; Maybe you forget to setup the ref?');
38
+ }
39
+ return $input;
40
+ }
41
+ return {
42
+ onChange(eventLikeOrString) {
43
+ var $input = getInputElement();
44
+ var value = typeof eventLikeOrString === 'string' ? eventLikeOrString : eventLikeOrString.target.value;
45
+ if ($input && $input.value !== value) {
46
+ $input.dispatchEvent(new InputEvent('beforeinput', {
47
+ bubbles: true,
48
+ cancelable: true
49
+ }));
50
+ setNativeValue($input, value);
51
+ $input.dispatchEvent(new InputEvent('input', {
52
+ bubbles: true,
53
+ cancelable: true
54
+ }));
55
+ }
56
+ },
57
+ onInput(eventLikeOrString) {
58
+ this.onChange(eventLikeOrString);
59
+ },
60
+ onFocus() {
61
+ var $input = getInputElement();
62
+ if ($input) {
63
+ $input.dispatchEvent(new FocusEvent('focusin', {
64
+ bubbles: true,
65
+ cancelable: true
66
+ }));
67
+ $input.dispatchEvent(new FocusEvent('focus', {
68
+ cancelable: true
69
+ }));
70
+ }
71
+ },
72
+ onBlur() {
73
+ var $input = getInputElement();
74
+ if ($input) {
75
+ $input.dispatchEvent(new FocusEvent('focusout', {
76
+ bubbles: true,
77
+ cancelable: true
78
+ }));
79
+ $input.dispatchEvent(new FocusEvent('blur', {
80
+ cancelable: true
81
+ }));
82
+ }
83
+ }
84
+ };
85
+ }
86
+ function BaseInputImpl(_ref, ref) {
87
+ var {
88
+ hidden = true,
89
+ className,
90
+ style
91
+ } = _ref,
92
+ props = _rollupPluginBabelHelpers.objectWithoutProperties(_ref, _excluded);
93
+ return /*#__PURE__*/react.createElement('input', _rollupPluginBabelHelpers.objectSpread2({
94
+ ref,
95
+ className: hidden ? '' : className,
96
+ style: hidden ? {
97
+ position: 'absolute',
98
+ width: '1px',
99
+ height: '1px',
100
+ padding: 0,
101
+ margin: '-1px',
102
+ overflow: 'hidden',
103
+ clip: 'rect(0,0,0,0)',
104
+ whiteSpace: 'nowrap',
105
+ borderWidth: 0
106
+ } : style
107
+ }, props));
108
+ }
109
+ var BaseInput = /*#__PURE__*/react.forwardRef(BaseInputImpl);
110
+
111
+ exports.BaseInput = BaseInput;
112
+ exports.useInputControl = useInputControl;
package/helpers.d.ts CHANGED
@@ -1,8 +1,37 @@
1
1
  import { type FieldConfig, type Primitive } from '@conform-to/dom';
2
- import { type InputHTMLAttributes, type SelectHTMLAttributes, type TextareaHTMLAttributes } from 'react';
2
+ import { type HTMLInputTypeAttribute } from 'react';
3
+ interface FieldProps {
4
+ name: string;
5
+ form?: string;
6
+ required?: boolean;
7
+ autoFocus?: boolean;
8
+ }
9
+ interface InputProps<Schema> extends FieldProps {
10
+ type?: HTMLInputTypeAttribute;
11
+ minLength?: number;
12
+ maxLength?: number;
13
+ min?: Schema extends number ? number : string | number;
14
+ max?: Schema extends number ? number : string | number;
15
+ step?: Schema extends number ? number : string | number;
16
+ pattern?: string;
17
+ multiple?: boolean;
18
+ value?: string;
19
+ defaultChecked?: boolean;
20
+ defaultValue?: string;
21
+ }
22
+ interface SelectProps extends FieldProps {
23
+ defaultValue?: string | number | readonly string[] | undefined;
24
+ multiple?: boolean;
25
+ }
26
+ interface TextareaProps extends FieldProps {
27
+ minLength?: number;
28
+ maxLength?: number;
29
+ defaultValue?: string;
30
+ }
3
31
  export declare function input<Schema extends Primitive>(config: FieldConfig<Schema>, { type, value }?: {
4
- type?: string;
32
+ type?: HTMLInputTypeAttribute;
5
33
  value?: string;
6
- }): InputHTMLAttributes<HTMLInputElement>;
7
- export declare function select<Schema extends Primitive | Array<Primitive>>(config: FieldConfig<Schema>): SelectHTMLAttributes<HTMLSelectElement>;
8
- export declare function textarea<Schema extends Primitive>(config: FieldConfig<Schema>): TextareaHTMLAttributes<HTMLTextAreaElement>;
34
+ }): InputProps<Schema>;
35
+ export declare function select<Schema extends Primitive | Array<Primitive>>(config: FieldConfig<Schema>): SelectProps;
36
+ export declare function textarea<Schema extends Primitive>(config: FieldConfig<Schema>): TextareaProps;
37
+ export {};
package/helpers.js CHANGED
@@ -21,23 +21,19 @@ function input(config) {
21
21
  pattern: config.pattern,
22
22
  multiple: config.multiple
23
23
  };
24
-
25
24
  if (config.initialError && config.initialError.length > 0) {
26
25
  attributes.autoFocus = true;
27
26
  }
28
-
29
27
  if (isCheckboxOrRadio) {
30
28
  attributes.value = value !== null && value !== void 0 ? value : 'on';
31
29
  attributes.defaultChecked = config.defaultValue === attributes.value;
32
30
  } else {
33
31
  attributes.defaultValue = config.defaultValue;
34
32
  }
35
-
36
33
  return attributes;
37
34
  }
38
35
  function select(config) {
39
36
  var _config$defaultValue;
40
-
41
37
  var attributes = {
42
38
  name: config.name,
43
39
  form: config.form,
@@ -45,16 +41,13 @@ function select(config) {
45
41
  required: config.required,
46
42
  multiple: config.multiple
47
43
  };
48
-
49
44
  if (config.initialError && config.initialError.length > 0) {
50
45
  attributes.autoFocus = true;
51
46
  }
52
-
53
47
  return attributes;
54
48
  }
55
49
  function textarea(config) {
56
50
  var _config$defaultValue2;
57
-
58
51
  var attributes = {
59
52
  name: config.name,
60
53
  form: config.form,
@@ -64,11 +57,9 @@ function textarea(config) {
64
57
  maxLength: config.maxLength,
65
58
  autoFocus: Boolean(config.initialError)
66
59
  };
67
-
68
60
  if (config.initialError && config.initialError.length > 0) {
69
61
  attributes.autoFocus = true;
70
62
  }
71
-
72
63
  return attributes;
73
64
  }
74
65
 
package/hooks.d.ts CHANGED
@@ -66,7 +66,7 @@ interface Form<Schema extends Record<string, any>> {
66
66
  * Returns properties required to hook into form events.
67
67
  * Applied custom validation and define when error should be reported.
68
68
  *
69
- * @see https://github.com/edmundhung/conform/tree/v0.4.0-pre.3/packages/conform-react/README.md#useform
69
+ * @see https://github.com/edmundhung/conform/tree/v0.4.1/packages/conform-react/README.md#useform
70
70
  */
71
71
  export declare function useForm<Schema extends Record<string, any>>(config?: FormConfig<Schema>): Form<Schema>;
72
72
  /**
@@ -107,7 +107,7 @@ export interface FieldsetConfig<Schema extends Record<string, any>> {
107
107
  /**
108
108
  * Returns all the information about the fieldset.
109
109
  *
110
- * @see https://github.com/edmundhung/conform/tree/v0.4.0-pre.3/packages/conform-react/README.md#usefieldset
110
+ * @see https://github.com/edmundhung/conform/tree/v0.4.1/packages/conform-react/README.md#usefieldset
111
111
  */
112
112
  export declare function useFieldset<Schema extends Record<string, any>>(ref: RefObject<HTMLFormElement | HTMLFieldSetElement>, config: FieldsetConfig<Schema>): Fieldset<Schema>;
113
113
  export declare function useFieldset<Schema extends Record<string, any>>(ref: RefObject<HTMLFormElement | HTMLFieldSetElement>, config: FieldConfig<Schema>): Fieldset<Schema>;
@@ -124,7 +124,7 @@ declare type ListCommandPayload<Schema, Type extends ListCommand<FieldValue<Sche
124
124
  * Returns a list of key and config, with a group of helpers
125
125
  * configuring buttons for list manipulation
126
126
  *
127
- * @see https://github.com/edmundhung/conform/tree/v0.4.0-pre.3/packages/conform-react/README.md#usefieldlist
127
+ * @see https://github.com/edmundhung/conform/tree/v0.4.1/packages/conform-react/README.md#usefieldlist
128
128
  */
129
129
  export declare function useFieldList<Payload = any>(ref: RefObject<HTMLFormElement | HTMLFieldSetElement>, config: FieldConfig<Array<Payload>>): [
130
130
  Array<{
@@ -160,7 +160,7 @@ interface InputControl<Element extends {
160
160
  * This is particular useful when integrating dropdown and datepicker whichs
161
161
  * introduces custom input mode.
162
162
  *
163
- * @see https://github.com/edmundhung/conform/tree/v0.4.0-pre.3/packages/conform-react/README.md#usecontrolledinput
163
+ * @see https://github.com/edmundhung/conform/tree/v0.4.1/packages/conform-react/README.md#usecontrolledinput
164
164
  */
165
165
  export declare function useControlledInput<Element extends {
166
166
  focus: () => void;