@conform-to/react 1.0.0-pre.0 → 1.0.0-pre.2
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/context.d.ts +32 -23
- package/context.js +91 -43
- package/context.mjs +88 -42
- package/helpers.d.ts +187 -43
- package/helpers.js +192 -98
- package/helpers.mjs +184 -92
- package/hooks.d.ts +21 -33
- package/hooks.js +53 -109
- package/hooks.mjs +54 -109
- package/index.d.ts +6 -6
- package/index.js +18 -6
- package/index.mjs +5 -7
- package/integrations.d.ts +31 -20
- package/integrations.js +116 -90
- package/integrations.mjs +115 -91
- package/package.json +2 -2
- package/validitystate.d.ts +12 -0
- package/validitystate.js +38 -0
- package/validitystate.mjs +34 -0
- package/intent.d.ts +0 -38
- package/intent.js +0 -37
- package/intent.mjs +0 -32
package/hooks.mjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { createForm } from '@conform-to/dom';
|
|
2
2
|
import { useState, useRef, useEffect, useLayoutEffect, useId } from 'react';
|
|
3
|
-
import { useSubjectRef,
|
|
3
|
+
import { useSubjectRef, useFormState, getFormMetadata, useRegistry, getFieldMetadata } from './context.mjs';
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
6
|
* useLayoutEffect is client-only.
|
|
@@ -11,26 +11,39 @@ function useFormId(preferredId) {
|
|
|
11
11
|
var id = useId();
|
|
12
12
|
return preferredId !== null && preferredId !== void 0 ? preferredId : id;
|
|
13
13
|
}
|
|
14
|
+
function useNoValidate() {
|
|
15
|
+
var defaultNoValidate = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : true;
|
|
16
|
+
var [noValidate, setNoValidate] = useState(defaultNoValidate);
|
|
17
|
+
useSafeLayoutEffect(() => {
|
|
18
|
+
// This is necessary to fix an issue in strict mode with related to our proxy setup
|
|
19
|
+
// It avoids the component from being rerendered without re-rendering the child
|
|
20
|
+
// Which reset the proxy but failed to capture its usage within child component
|
|
21
|
+
if (!noValidate) {
|
|
22
|
+
setNoValidate(true);
|
|
23
|
+
}
|
|
24
|
+
}, [noValidate]);
|
|
25
|
+
return noValidate;
|
|
26
|
+
}
|
|
14
27
|
function useForm(options) {
|
|
15
28
|
var formId = useFormId(options.id);
|
|
16
|
-
var
|
|
17
|
-
var [form, setForm] = useState(
|
|
29
|
+
var initializeContext = () => createForm(formId, options);
|
|
30
|
+
var [form, setForm] = useState(initializeContext);
|
|
18
31
|
|
|
19
32
|
// If id changes, reinitialize the form immediately
|
|
20
33
|
if (formId !== form.id) {
|
|
21
|
-
setForm(
|
|
34
|
+
setForm(initializeContext);
|
|
22
35
|
}
|
|
23
36
|
var optionsRef = useRef(options);
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
37
|
+
useSafeLayoutEffect(() => {
|
|
38
|
+
document.addEventListener('input', form.input);
|
|
39
|
+
document.addEventListener('focusout', form.blur);
|
|
40
|
+
document.addEventListener('reset', form.reset);
|
|
41
|
+
return () => {
|
|
42
|
+
document.removeEventListener('input', form.input);
|
|
43
|
+
document.removeEventListener('focusout', form.blur);
|
|
44
|
+
document.removeEventListener('reset', form.reset);
|
|
45
|
+
};
|
|
46
|
+
}, [form]);
|
|
34
47
|
useSafeLayoutEffect(() => {
|
|
35
48
|
if (options.lastResult === optionsRef.current.lastResult) {
|
|
36
49
|
// If there is no change, do nothing
|
|
@@ -47,108 +60,40 @@ function useForm(options) {
|
|
|
47
60
|
optionsRef.current = options;
|
|
48
61
|
form.update(options);
|
|
49
62
|
});
|
|
63
|
+
var subjectRef = useSubjectRef();
|
|
64
|
+
var state = useFormState(form, subjectRef);
|
|
65
|
+
var noValidate = useNoValidate(options.defaultNoValidate);
|
|
66
|
+
var meta = getFormMetadata(formId, state, subjectRef, form, noValidate);
|
|
50
67
|
return {
|
|
51
|
-
|
|
52
|
-
fields
|
|
53
|
-
form: metadata
|
|
68
|
+
meta,
|
|
69
|
+
fields: meta.getFieldset()
|
|
54
70
|
};
|
|
55
71
|
}
|
|
56
72
|
function useFormMetadata(options) {
|
|
57
|
-
var _options$defaultNoVal;
|
|
58
|
-
var subjectRef = useSubjectRef();
|
|
59
|
-
var form = useFormStore(options.formId, options.context);
|
|
60
|
-
var context = useFormContext(form, subjectRef);
|
|
61
|
-
var metadata = getBaseMetadata(options.formId, context, {
|
|
62
|
-
subjectRef
|
|
63
|
-
});
|
|
64
|
-
var [noValidate, setNoValidate] = useState((_options$defaultNoVal = options.defaultNoValidate) !== null && _options$defaultNoVal !== void 0 ? _options$defaultNoVal : true);
|
|
65
|
-
useSafeLayoutEffect(() => {
|
|
66
|
-
// This is necessary to fix an issue in strict mode with related to our proxy setup
|
|
67
|
-
// It avoids the component from being rerendered without re-rendering the child
|
|
68
|
-
// Which reset the proxy but failed to capture its usage within child component
|
|
69
|
-
if (!noValidate) {
|
|
70
|
-
setNoValidate(true);
|
|
71
|
-
}
|
|
72
|
-
}, [noValidate]);
|
|
73
|
-
return new Proxy(metadata, {
|
|
74
|
-
get(target, key, receiver) {
|
|
75
|
-
switch (key) {
|
|
76
|
-
case 'onSubmit':
|
|
77
|
-
return event => {
|
|
78
|
-
var submitEvent = event.nativeEvent;
|
|
79
|
-
var result = form.submit(submitEvent);
|
|
80
|
-
if (submitEvent.defaultPrevented) {
|
|
81
|
-
event.preventDefault();
|
|
82
|
-
}
|
|
83
|
-
return result;
|
|
84
|
-
};
|
|
85
|
-
case 'onReset':
|
|
86
|
-
return event => form.reset(event.nativeEvent);
|
|
87
|
-
case 'noValidate':
|
|
88
|
-
return noValidate;
|
|
89
|
-
}
|
|
90
|
-
return Reflect.get(target, key, receiver);
|
|
91
|
-
}
|
|
92
|
-
});
|
|
93
|
-
}
|
|
94
|
-
function useFieldset(options) {
|
|
95
73
|
var subjectRef = useSubjectRef();
|
|
96
|
-
var form =
|
|
97
|
-
var
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
var getMetadata = key => getFieldMetadata(options.formId, context, {
|
|
101
|
-
name: options.name,
|
|
102
|
-
key: key,
|
|
103
|
-
subjectRef
|
|
104
|
-
});
|
|
105
|
-
|
|
106
|
-
// To support array destructuring
|
|
107
|
-
if (prop === Symbol.iterator) {
|
|
108
|
-
var _index = 0;
|
|
109
|
-
return () => ({
|
|
110
|
-
next: () => ({
|
|
111
|
-
value: getMetadata(_index++),
|
|
112
|
-
done: false
|
|
113
|
-
})
|
|
114
|
-
});
|
|
115
|
-
}
|
|
116
|
-
var index = Number(prop);
|
|
117
|
-
if (typeof prop === 'string') {
|
|
118
|
-
return getMetadata(Number.isNaN(index) ? prop : index);
|
|
119
|
-
}
|
|
120
|
-
return Reflect.get(target, prop, receiver);
|
|
121
|
-
}
|
|
122
|
-
});
|
|
123
|
-
}
|
|
124
|
-
function useFieldList(options) {
|
|
125
|
-
var _context$initialValue;
|
|
126
|
-
var subjectRef = useSubjectRef({
|
|
127
|
-
initialValue: {
|
|
128
|
-
name: [options.name]
|
|
129
|
-
}
|
|
130
|
-
});
|
|
131
|
-
var form = useFormStore(options.formId, options.context);
|
|
132
|
-
var context = useFormContext(form, subjectRef);
|
|
133
|
-
var initialValue = (_context$initialValue = context.initialValue[options.name]) !== null && _context$initialValue !== void 0 ? _context$initialValue : [];
|
|
134
|
-
if (!Array.isArray(initialValue)) {
|
|
135
|
-
throw new Error('The initial value at the given name is not a list');
|
|
136
|
-
}
|
|
137
|
-
return Array(initialValue.length).fill(0).map((_, index) => getFieldMetadata(options.formId, context, {
|
|
138
|
-
name: options.name,
|
|
139
|
-
key: index,
|
|
140
|
-
subjectRef
|
|
141
|
-
}));
|
|
74
|
+
var form = useRegistry(options.formId);
|
|
75
|
+
var state = useFormState(form, subjectRef);
|
|
76
|
+
var noValidate = useNoValidate(options.defaultNoValidate);
|
|
77
|
+
return getFormMetadata(options.formId, state, subjectRef, form, noValidate);
|
|
142
78
|
}
|
|
143
79
|
function useField(options) {
|
|
144
80
|
var subjectRef = useSubjectRef();
|
|
145
|
-
var
|
|
146
|
-
var
|
|
147
|
-
var
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
81
|
+
var context = useRegistry(options.formId);
|
|
82
|
+
var state = useFormState(context, subjectRef);
|
|
83
|
+
var meta = getFieldMetadata(options.formId, state, subjectRef, options.name);
|
|
84
|
+
var form = getFormMetadata(options.formId, state, subjectRef, context, false);
|
|
85
|
+
return {
|
|
86
|
+
meta,
|
|
87
|
+
// @ts-expect-error The types is used as a hint only
|
|
88
|
+
get fields() {
|
|
89
|
+
return meta.getFieldset();
|
|
90
|
+
},
|
|
91
|
+
// @ts-expect-error The types is used as a hint only
|
|
92
|
+
get list() {
|
|
93
|
+
return meta.getFieldList();
|
|
94
|
+
},
|
|
95
|
+
form
|
|
96
|
+
};
|
|
152
97
|
}
|
|
153
98
|
|
|
154
|
-
export { useField,
|
|
99
|
+
export { useField, useForm, useFormId, useFormMetadata, useNoValidate, useSafeLayoutEffect };
|
package/index.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
export { type Submission, type FieldName, requestIntent, isFieldElement, } from '@conform-to/dom';
|
|
2
|
-
export { type
|
|
3
|
-
export { useForm, useFormMetadata,
|
|
4
|
-
export {
|
|
5
|
-
export
|
|
6
|
-
export
|
|
1
|
+
export { type Submission, type SubmissionResult, type Intent, type FormId, type FieldName, requestIntent, requestSubmit, isFieldElement, intent, } from '@conform-to/dom';
|
|
2
|
+
export { type FieldProps, type FieldMetadata, type FormMetadata, FormProvider, FormStateInput, } from './context';
|
|
3
|
+
export { useForm, useFormMetadata, useField } from './hooks';
|
|
4
|
+
export { useInputControl } from './integrations';
|
|
5
|
+
export { validateConstraint } from './validitystate';
|
|
6
|
+
export { getFormProps, getFieldsetProps, getInputProps, getSelectProps, getTextareaProps, getCollectionProps, getControlButtonProps, } from './helpers';
|
package/index.js
CHANGED
|
@@ -6,11 +6,15 @@ var dom = require('@conform-to/dom');
|
|
|
6
6
|
var context = require('./context.js');
|
|
7
7
|
var hooks = require('./hooks.js');
|
|
8
8
|
var integrations = require('./integrations.js');
|
|
9
|
+
var validitystate = require('./validitystate.js');
|
|
9
10
|
var helpers = require('./helpers.js');
|
|
10
|
-
var intent = require('./intent.js');
|
|
11
11
|
|
|
12
12
|
|
|
13
13
|
|
|
14
|
+
Object.defineProperty(exports, 'intent', {
|
|
15
|
+
enumerable: true,
|
|
16
|
+
get: function () { return dom.intent; }
|
|
17
|
+
});
|
|
14
18
|
Object.defineProperty(exports, 'isFieldElement', {
|
|
15
19
|
enumerable: true,
|
|
16
20
|
get: function () { return dom.isFieldElement; }
|
|
@@ -19,13 +23,21 @@ Object.defineProperty(exports, 'requestIntent', {
|
|
|
19
23
|
enumerable: true,
|
|
20
24
|
get: function () { return dom.requestIntent; }
|
|
21
25
|
});
|
|
26
|
+
Object.defineProperty(exports, 'requestSubmit', {
|
|
27
|
+
enumerable: true,
|
|
28
|
+
get: function () { return dom.requestSubmit; }
|
|
29
|
+
});
|
|
22
30
|
exports.FormProvider = context.FormProvider;
|
|
23
31
|
exports.FormStateInput = context.FormStateInput;
|
|
24
32
|
exports.useField = hooks.useField;
|
|
25
|
-
exports.useFieldList = hooks.useFieldList;
|
|
26
|
-
exports.useFieldset = hooks.useFieldset;
|
|
27
33
|
exports.useForm = hooks.useForm;
|
|
28
34
|
exports.useFormMetadata = hooks.useFormMetadata;
|
|
29
|
-
exports.
|
|
30
|
-
exports.
|
|
31
|
-
exports.
|
|
35
|
+
exports.useInputControl = integrations.useInputControl;
|
|
36
|
+
exports.validateConstraint = validitystate.validateConstraint;
|
|
37
|
+
exports.getCollectionProps = helpers.getCollectionProps;
|
|
38
|
+
exports.getControlButtonProps = helpers.getControlButtonProps;
|
|
39
|
+
exports.getFieldsetProps = helpers.getFieldsetProps;
|
|
40
|
+
exports.getFormProps = helpers.getFormProps;
|
|
41
|
+
exports.getInputProps = helpers.getInputProps;
|
|
42
|
+
exports.getSelectProps = helpers.getSelectProps;
|
|
43
|
+
exports.getTextareaProps = helpers.getTextareaProps;
|
package/index.mjs
CHANGED
|
@@ -1,8 +1,6 @@
|
|
|
1
|
-
export { isFieldElement, requestIntent } from '@conform-to/dom';
|
|
1
|
+
export { intent, isFieldElement, requestIntent, requestSubmit } from '@conform-to/dom';
|
|
2
2
|
export { FormProvider, FormStateInput } from './context.mjs';
|
|
3
|
-
export { useField,
|
|
4
|
-
export {
|
|
5
|
-
|
|
6
|
-
export {
|
|
7
|
-
import * as intent from './intent.mjs';
|
|
8
|
-
export { intent };
|
|
3
|
+
export { useField, useForm, useFormMetadata } from './hooks.mjs';
|
|
4
|
+
export { useInputControl } from './integrations.mjs';
|
|
5
|
+
export { validateConstraint } from './validitystate.mjs';
|
|
6
|
+
export { getCollectionProps, getControlButtonProps, getFieldsetProps, getFormProps, getInputProps, getSelectProps, getTextareaProps } from './helpers.mjs';
|
package/integrations.d.ts
CHANGED
|
@@ -1,23 +1,34 @@
|
|
|
1
|
-
import { type FieldElement } from '@conform-to/dom';
|
|
2
|
-
import { type
|
|
3
|
-
export type InputControl = {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
value: string;
|
|
7
|
-
};
|
|
8
|
-
} | string | boolean) => void;
|
|
1
|
+
import { type FieldElement, type FormValue, FieldName, FormId } from '@conform-to/dom';
|
|
2
|
+
import { type FieldMetadata } from './context';
|
|
3
|
+
export type InputControl<Value> = {
|
|
4
|
+
value: Value;
|
|
5
|
+
change: (value: Value) => void;
|
|
9
6
|
focus: () => void;
|
|
10
7
|
blur: () => void;
|
|
11
8
|
};
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
export declare function
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
}): InputControl
|
|
9
|
+
export declare function getFieldElement(formId: string, name: string, match?: (element: FieldElement) => boolean): FieldElement | null;
|
|
10
|
+
export declare function getEventTarget(formId: string, name: string): FieldElement;
|
|
11
|
+
export declare function useInputControl<Schema>(metadata: FieldMetadata<Schema, any, any>, options?: {
|
|
12
|
+
onFocus?: (event: Event) => void;
|
|
13
|
+
}): InputControl<string | undefined>;
|
|
14
|
+
export declare function useInputControl<Schema>(options: {
|
|
15
|
+
key?: string;
|
|
16
|
+
name: FieldName<Schema>;
|
|
17
|
+
formId: FormId<any, any>;
|
|
18
|
+
initialValue: FormValue<Schema>;
|
|
19
|
+
onFocus?: (event: Event) => void;
|
|
20
|
+
}): InputControl<string | undefined>;
|
|
21
|
+
export declare function useInputControl<Schema, Value>(metadata: FieldMetadata<Schema, any, any>, options: {
|
|
22
|
+
initialize: (value: FormValue<Schema> | undefined) => Value;
|
|
23
|
+
serialize?: (value: Value) => string;
|
|
24
|
+
onFocus?: (event: Event) => void;
|
|
25
|
+
}): InputControl<Value>;
|
|
26
|
+
export declare function useInputControl<Schema, Value>(options: {
|
|
27
|
+
key?: string;
|
|
28
|
+
name: FieldName<Schema>;
|
|
29
|
+
formId: FormId<any, any>;
|
|
30
|
+
initialValue: FormValue<Schema>;
|
|
31
|
+
initialize: (value: FormValue<Schema> | undefined) => Value;
|
|
32
|
+
serialize?: (value: Value) => string;
|
|
33
|
+
onFocus?: (event: Event) => void;
|
|
34
|
+
}): InputControl<Value>;
|
package/integrations.js
CHANGED
|
@@ -2,90 +2,104 @@
|
|
|
2
2
|
|
|
3
3
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
4
|
|
|
5
|
+
var _rollupPluginBabelHelpers = require('./_virtual/_rollupPluginBabelHelpers.js');
|
|
5
6
|
var dom = require('@conform-to/dom');
|
|
6
7
|
var react = require('react');
|
|
7
|
-
var hooks = require('./hooks.js');
|
|
8
8
|
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
9
|
+
function getFieldElement(formId, name) {
|
|
10
|
+
var _document$forms$named;
|
|
11
|
+
var match = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : () => true;
|
|
12
|
+
var element = (_document$forms$named = document.forms.namedItem(formId)) === null || _document$forms$named === void 0 ? void 0 : _document$forms$named.elements.namedItem(name);
|
|
13
|
+
if (element) {
|
|
14
|
+
var items = element instanceof Element ? [element] : Array.from(element.values());
|
|
15
|
+
for (var item of items) {
|
|
16
|
+
if (dom.isFieldElement(item) && match(item)) {
|
|
17
|
+
return item;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
return null;
|
|
22
|
+
}
|
|
23
|
+
function getEventTarget(formId, name) {
|
|
24
|
+
var element = getFieldElement(formId, name);
|
|
25
|
+
if (element) {
|
|
26
|
+
return element;
|
|
27
|
+
}
|
|
28
|
+
var form = document.forms.namedItem(formId);
|
|
29
|
+
var input = document.createElement('input');
|
|
30
|
+
input.type = 'hidden';
|
|
31
|
+
input.name = name;
|
|
32
|
+
form === null || form === void 0 ? void 0 : form.appendChild(input);
|
|
33
|
+
return input;
|
|
34
|
+
}
|
|
35
|
+
function useInputControl(metadata, options) {
|
|
36
|
+
var _options$initialize, _options$serialize, _options$onFocus;
|
|
16
37
|
var eventDispatched = react.useRef({
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
38
|
+
change: false,
|
|
39
|
+
focus: false,
|
|
40
|
+
blur: false
|
|
20
41
|
});
|
|
21
|
-
|
|
22
|
-
|
|
42
|
+
var [key, setKey] = react.useState(metadata.key);
|
|
43
|
+
var initialize = (_options$initialize = options === null || options === void 0 ? void 0 : options.initialize) !== null && _options$initialize !== void 0 ? _options$initialize : 'initialize' in metadata && metadata.initialize ? metadata.initialize : value => value === null || value === void 0 ? void 0 : value.toString();
|
|
44
|
+
var serialize = (_options$serialize = options === null || options === void 0 ? void 0 : options.serialize) !== null && _options$serialize !== void 0 ? _options$serialize : 'serialize' in metadata && metadata.serialize ? metadata.serialize : undefined;
|
|
45
|
+
var onFocus = (_options$onFocus = options === null || options === void 0 ? void 0 : options.onFocus) !== null && _options$onFocus !== void 0 ? _options$onFocus : 'onFocus' in metadata ? metadata.onFocus : undefined;
|
|
46
|
+
var optionsRef = react.useRef({
|
|
47
|
+
initialize,
|
|
48
|
+
serialize,
|
|
49
|
+
onFocus
|
|
23
50
|
});
|
|
24
|
-
|
|
51
|
+
var [value, setValue] = react.useState(() => initialize(metadata.initialValue));
|
|
52
|
+
if (key !== metadata.key) {
|
|
53
|
+
setValue(initialize(metadata.initialValue));
|
|
54
|
+
setKey(metadata.key);
|
|
55
|
+
}
|
|
56
|
+
react.useEffect(() => {
|
|
57
|
+
optionsRef.current = {
|
|
58
|
+
initialize,
|
|
59
|
+
serialize,
|
|
60
|
+
onFocus
|
|
61
|
+
};
|
|
62
|
+
});
|
|
63
|
+
react.useEffect(() => {
|
|
25
64
|
var createEventListener = listener => {
|
|
26
65
|
return event => {
|
|
27
|
-
var
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
eventDispatched.current[listener] = true;
|
|
66
|
+
var element = getFieldElement(metadata.formId, metadata.name, element => element === event.target);
|
|
67
|
+
if (element) {
|
|
68
|
+
if (listener === 'focus') {
|
|
69
|
+
var _optionsRef$current, _optionsRef$current$o;
|
|
70
|
+
(_optionsRef$current = optionsRef.current) === null || _optionsRef$current === void 0 || (_optionsRef$current$o = _optionsRef$current.onFocus) === null || _optionsRef$current$o === void 0 ? void 0 : _optionsRef$current$o.call(_optionsRef$current, event);
|
|
33
71
|
}
|
|
34
|
-
|
|
72
|
+
eventDispatched.current[listener] = true;
|
|
35
73
|
}
|
|
36
74
|
};
|
|
37
75
|
};
|
|
38
|
-
var inputHandler = createEventListener('
|
|
39
|
-
var focusHandler = createEventListener('
|
|
40
|
-
var blurHandler = createEventListener('
|
|
41
|
-
var resetHandler = createEventListener('onReset');
|
|
42
|
-
|
|
43
|
-
// focus/blur event does not bubble
|
|
76
|
+
var inputHandler = createEventListener('change');
|
|
77
|
+
var focusHandler = createEventListener('focus');
|
|
78
|
+
var blurHandler = createEventListener('blur');
|
|
44
79
|
document.addEventListener('input', inputHandler, true);
|
|
45
|
-
document.addEventListener('
|
|
46
|
-
document.addEventListener('
|
|
47
|
-
document.addEventListener('reset', resetHandler);
|
|
80
|
+
document.addEventListener('focusin', focusHandler, true);
|
|
81
|
+
document.addEventListener('focusout', blurHandler, true);
|
|
48
82
|
return () => {
|
|
49
83
|
document.removeEventListener('input', inputHandler, true);
|
|
50
|
-
document.removeEventListener('
|
|
51
|
-
document.removeEventListener('
|
|
52
|
-
document.removeEventListener('reset', resetHandler);
|
|
53
|
-
};
|
|
54
|
-
}, []);
|
|
55
|
-
var control = react.useMemo(() => {
|
|
56
|
-
var dispatch = (listener, fn) => {
|
|
57
|
-
if (!eventDispatched.current[listener]) {
|
|
58
|
-
var _optionsRef$current5, _optionsRef$current6, _optionsRef$current7;
|
|
59
|
-
var _element = typeof ((_optionsRef$current5 = optionsRef.current) === null || _optionsRef$current5 === void 0 ? void 0 : _optionsRef$current5.ref) === 'function' ? (_optionsRef$current6 = optionsRef.current) === null || _optionsRef$current6 === void 0 ? void 0 : _optionsRef$current6.ref() : (_optionsRef$current7 = optionsRef.current) === null || _optionsRef$current7 === void 0 ? void 0 : _optionsRef$current7.ref.current;
|
|
60
|
-
if (!dom.isFieldElement(_element)) {
|
|
61
|
-
// eslint-disable-next-line no-console
|
|
62
|
-
console.warn('Failed to dispatch event; is the input mounted?');
|
|
63
|
-
return;
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
// To avoid recursion
|
|
67
|
-
eventDispatched.current[listener] = true;
|
|
68
|
-
fn(_element);
|
|
69
|
-
}
|
|
70
|
-
eventDispatched.current[listener] = false;
|
|
84
|
+
document.removeEventListener('focusin', focusHandler, true);
|
|
85
|
+
document.removeEventListener('focusout', blurHandler, true);
|
|
71
86
|
};
|
|
87
|
+
}, [metadata.formId, metadata.name]);
|
|
88
|
+
var handlers = react.useMemo(() => {
|
|
72
89
|
return {
|
|
73
|
-
change(
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
90
|
+
change(value) {
|
|
91
|
+
if (!eventDispatched.current.change) {
|
|
92
|
+
var _ref, _optionsRef$current$s, _optionsRef$current$s2, _optionsRef$current2;
|
|
93
|
+
var _element = getEventTarget(metadata.formId, metadata.name);
|
|
94
|
+
var serializedValue = (_ref = (_optionsRef$current$s = (_optionsRef$current$s2 = (_optionsRef$current2 = optionsRef.current).serialize) === null || _optionsRef$current$s2 === void 0 ? void 0 : _optionsRef$current$s2.call(_optionsRef$current2, value)) !== null && _optionsRef$current$s !== void 0 ? _optionsRef$current$s : value === null || value === void 0 ? void 0 : value.toString()) !== null && _ref !== void 0 ? _ref : '';
|
|
95
|
+
eventDispatched.current.change = true;
|
|
96
|
+
if (_element instanceof HTMLInputElement && (_element.type === 'checkbox' || _element.type === 'radio')) {
|
|
97
|
+
if (_element.checked ? _element.value !== serializedValue : _element.value === serializedValue) {
|
|
98
|
+
_element.click();
|
|
78
99
|
}
|
|
79
|
-
element.checked = eventOrValue;
|
|
80
100
|
} else {
|
|
81
|
-
if
|
|
82
|
-
|
|
83
|
-
}
|
|
84
|
-
var value = typeof eventOrValue === 'string' ? eventOrValue : eventOrValue.target.value;
|
|
85
|
-
|
|
86
|
-
// No change event will triggered on React if `element.value` is updated
|
|
87
|
-
// before dispatching the event
|
|
88
|
-
if (element.value !== value) {
|
|
101
|
+
// No change event will be triggered on React if `element.value` is already updated
|
|
102
|
+
if (_element.value !== serializedValue) {
|
|
89
103
|
/**
|
|
90
104
|
* Triggering react custom change event
|
|
91
105
|
* Solution based on dom-testing-library
|
|
@@ -94,52 +108,64 @@ function useInputEvent(options) {
|
|
|
94
108
|
*/
|
|
95
109
|
var {
|
|
96
110
|
set: valueSetter
|
|
97
|
-
} = Object.getOwnPropertyDescriptor(
|
|
98
|
-
var prototype = Object.getPrototypeOf(
|
|
111
|
+
} = Object.getOwnPropertyDescriptor(_element, 'value') || {};
|
|
112
|
+
var prototype = Object.getPrototypeOf(_element);
|
|
99
113
|
var {
|
|
100
114
|
set: prototypeValueSetter
|
|
101
115
|
} = Object.getOwnPropertyDescriptor(prototype, 'value') || {};
|
|
102
116
|
if (prototypeValueSetter && valueSetter !== prototypeValueSetter) {
|
|
103
|
-
prototypeValueSetter.call(
|
|
117
|
+
prototypeValueSetter.call(_element, value);
|
|
104
118
|
} else {
|
|
105
119
|
if (valueSetter) {
|
|
106
|
-
valueSetter.call(
|
|
120
|
+
valueSetter.call(_element, value);
|
|
107
121
|
} else {
|
|
108
122
|
throw new Error('The given element does not have a value setter');
|
|
109
123
|
}
|
|
110
124
|
}
|
|
111
125
|
}
|
|
112
|
-
}
|
|
113
126
|
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
127
|
+
// Dispatch input event with the updated input value
|
|
128
|
+
_element.dispatchEvent(new InputEvent('input', {
|
|
129
|
+
bubbles: true
|
|
130
|
+
}));
|
|
131
|
+
// Dispatch change event (necessary for select to update the selected option)
|
|
132
|
+
_element.dispatchEvent(new Event('change', {
|
|
133
|
+
bubbles: true
|
|
134
|
+
}));
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
setValue(value);
|
|
138
|
+
eventDispatched.current.change = false;
|
|
123
139
|
},
|
|
124
140
|
focus() {
|
|
125
|
-
|
|
126
|
-
|
|
141
|
+
if (!eventDispatched.current.focus) {
|
|
142
|
+
var _element2 = getEventTarget(metadata.formId, metadata.name);
|
|
143
|
+
eventDispatched.current.focus = true;
|
|
144
|
+
_element2.dispatchEvent(new FocusEvent('focusin', {
|
|
127
145
|
bubbles: true
|
|
128
146
|
}));
|
|
129
|
-
|
|
130
|
-
}
|
|
147
|
+
_element2.dispatchEvent(new FocusEvent('focus'));
|
|
148
|
+
}
|
|
149
|
+
eventDispatched.current.focus = false;
|
|
131
150
|
},
|
|
132
151
|
blur() {
|
|
133
|
-
|
|
134
|
-
|
|
152
|
+
if (!eventDispatched.current.blur) {
|
|
153
|
+
var _element3 = getEventTarget(metadata.formId, metadata.name);
|
|
154
|
+
eventDispatched.current.blur = true;
|
|
155
|
+
_element3.dispatchEvent(new FocusEvent('focusout', {
|
|
135
156
|
bubbles: true
|
|
136
157
|
}));
|
|
137
|
-
|
|
138
|
-
}
|
|
158
|
+
_element3.dispatchEvent(new FocusEvent('blur'));
|
|
159
|
+
}
|
|
160
|
+
eventDispatched.current.blur = false;
|
|
139
161
|
}
|
|
140
162
|
};
|
|
141
|
-
}, [
|
|
142
|
-
return
|
|
163
|
+
}, [metadata.formId, metadata.name]);
|
|
164
|
+
return _rollupPluginBabelHelpers.objectSpread2(_rollupPluginBabelHelpers.objectSpread2({}, handlers), {}, {
|
|
165
|
+
value
|
|
166
|
+
});
|
|
143
167
|
}
|
|
144
168
|
|
|
145
|
-
exports.
|
|
169
|
+
exports.getEventTarget = getEventTarget;
|
|
170
|
+
exports.getFieldElement = getFieldElement;
|
|
171
|
+
exports.useInputControl = useInputControl;
|