@jlnstack/form 0.0.0 → 0.0.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/LICENSE +7 -0
- package/dist/_virtual/_@oxc-project_runtime@0.108.0/helpers/defineProperty.cjs +14 -0
- package/dist/_virtual/_@oxc-project_runtime@0.108.0/helpers/defineProperty.mjs +14 -0
- package/dist/_virtual/_@oxc-project_runtime@0.108.0/helpers/objectSpread2.cjs +27 -0
- package/dist/_virtual/_@oxc-project_runtime@0.108.0/helpers/objectSpread2.mjs +27 -0
- package/dist/_virtual/_@oxc-project_runtime@0.108.0/helpers/toPrimitive.cjs +16 -0
- package/dist/_virtual/_@oxc-project_runtime@0.108.0/helpers/toPrimitive.mjs +16 -0
- package/dist/_virtual/_@oxc-project_runtime@0.108.0/helpers/toPropertyKey.cjs +11 -0
- package/dist/_virtual/_@oxc-project_runtime@0.108.0/helpers/toPropertyKey.mjs +11 -0
- package/dist/_virtual/_@oxc-project_runtime@0.108.0/helpers/typeof.cjs +18 -0
- package/dist/_virtual/_@oxc-project_runtime@0.108.0/helpers/typeof.mjs +12 -0
- package/dist/index.cjs +0 -0
- package/dist/index.d.cts +2 -0
- package/dist/index.d.mts +2 -0
- package/dist/index.mjs +1 -0
- package/dist/react-hook-form.cjs +15 -0
- package/dist/react-hook-form.d.cts +12 -0
- package/dist/react-hook-form.d.cts.map +1 -0
- package/dist/react-hook-form.d.mts +12 -0
- package/dist/react-hook-form.d.mts.map +1 -0
- package/dist/react-hook-form.mjs +16 -0
- package/dist/react-hook-form.mjs.map +1 -0
- package/dist/react.cjs +50 -0
- package/dist/react.d.cts +14 -0
- package/dist/react.d.cts.map +1 -0
- package/dist/react.d.mts +14 -0
- package/dist/react.d.mts.map +1 -0
- package/dist/react.mjs +50 -0
- package/dist/react.mjs.map +1 -0
- package/dist/types.d.cts +32 -0
- package/dist/types.d.cts.map +1 -0
- package/dist/types.d.mts +32 -0
- package/dist/types.d.mts.map +1 -0
- package/package.json +13 -12
package/LICENSE
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
The MIT License (MIT) Copyright (c) 2025 - present, jln13x
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
4
|
+
|
|
5
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
6
|
+
|
|
7
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
const require_toPropertyKey = require('./toPropertyKey.cjs');
|
|
2
|
+
|
|
3
|
+
//#region \0@oxc-project+runtime@0.108.0/helpers/defineProperty.js
|
|
4
|
+
function _defineProperty(e, r, t) {
|
|
5
|
+
return (r = require_toPropertyKey.toPropertyKey(r)) in e ? Object.defineProperty(e, r, {
|
|
6
|
+
value: t,
|
|
7
|
+
enumerable: !0,
|
|
8
|
+
configurable: !0,
|
|
9
|
+
writable: !0
|
|
10
|
+
}) : e[r] = t, e;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
//#endregion
|
|
14
|
+
exports._defineProperty = _defineProperty;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { toPropertyKey } from "./toPropertyKey.mjs";
|
|
2
|
+
|
|
3
|
+
//#region \0@oxc-project+runtime@0.108.0/helpers/defineProperty.js
|
|
4
|
+
function _defineProperty(e, r, t) {
|
|
5
|
+
return (r = toPropertyKey(r)) in e ? Object.defineProperty(e, r, {
|
|
6
|
+
value: t,
|
|
7
|
+
enumerable: !0,
|
|
8
|
+
configurable: !0,
|
|
9
|
+
writable: !0
|
|
10
|
+
}) : e[r] = t, e;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
//#endregion
|
|
14
|
+
export { _defineProperty };
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
const require_defineProperty = require('./defineProperty.cjs');
|
|
2
|
+
|
|
3
|
+
//#region \0@oxc-project+runtime@0.108.0/helpers/objectSpread2.js
|
|
4
|
+
function ownKeys(e, r) {
|
|
5
|
+
var t = Object.keys(e);
|
|
6
|
+
if (Object.getOwnPropertySymbols) {
|
|
7
|
+
var o = Object.getOwnPropertySymbols(e);
|
|
8
|
+
r && (o = o.filter(function(r$1) {
|
|
9
|
+
return Object.getOwnPropertyDescriptor(e, r$1).enumerable;
|
|
10
|
+
})), t.push.apply(t, o);
|
|
11
|
+
}
|
|
12
|
+
return t;
|
|
13
|
+
}
|
|
14
|
+
function _objectSpread2(e) {
|
|
15
|
+
for (var r = 1; r < arguments.length; r++) {
|
|
16
|
+
var t = null != arguments[r] ? arguments[r] : {};
|
|
17
|
+
r % 2 ? ownKeys(Object(t), !0).forEach(function(r$1) {
|
|
18
|
+
require_defineProperty._defineProperty(e, r$1, t[r$1]);
|
|
19
|
+
}) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function(r$1) {
|
|
20
|
+
Object.defineProperty(e, r$1, Object.getOwnPropertyDescriptor(t, r$1));
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
return e;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
//#endregion
|
|
27
|
+
exports._objectSpread2 = _objectSpread2;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { _defineProperty } from "./defineProperty.mjs";
|
|
2
|
+
|
|
3
|
+
//#region \0@oxc-project+runtime@0.108.0/helpers/objectSpread2.js
|
|
4
|
+
function ownKeys(e, r) {
|
|
5
|
+
var t = Object.keys(e);
|
|
6
|
+
if (Object.getOwnPropertySymbols) {
|
|
7
|
+
var o = Object.getOwnPropertySymbols(e);
|
|
8
|
+
r && (o = o.filter(function(r$1) {
|
|
9
|
+
return Object.getOwnPropertyDescriptor(e, r$1).enumerable;
|
|
10
|
+
})), t.push.apply(t, o);
|
|
11
|
+
}
|
|
12
|
+
return t;
|
|
13
|
+
}
|
|
14
|
+
function _objectSpread2(e) {
|
|
15
|
+
for (var r = 1; r < arguments.length; r++) {
|
|
16
|
+
var t = null != arguments[r] ? arguments[r] : {};
|
|
17
|
+
r % 2 ? ownKeys(Object(t), !0).forEach(function(r$1) {
|
|
18
|
+
_defineProperty(e, r$1, t[r$1]);
|
|
19
|
+
}) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function(r$1) {
|
|
20
|
+
Object.defineProperty(e, r$1, Object.getOwnPropertyDescriptor(t, r$1));
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
return e;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
//#endregion
|
|
27
|
+
export { _objectSpread2 };
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
const require_typeof = require('./typeof.cjs');
|
|
2
|
+
|
|
3
|
+
//#region \0@oxc-project+runtime@0.108.0/helpers/toPrimitive.js
|
|
4
|
+
function toPrimitive(t, r) {
|
|
5
|
+
if ("object" != require_typeof._typeof(t) || !t) return t;
|
|
6
|
+
var e = t[Symbol.toPrimitive];
|
|
7
|
+
if (void 0 !== e) {
|
|
8
|
+
var i = e.call(t, r || "default");
|
|
9
|
+
if ("object" != require_typeof._typeof(i)) return i;
|
|
10
|
+
throw new TypeError("@@toPrimitive must return a primitive value.");
|
|
11
|
+
}
|
|
12
|
+
return ("string" === r ? String : Number)(t);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
//#endregion
|
|
16
|
+
exports.toPrimitive = toPrimitive;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { _typeof } from "./typeof.mjs";
|
|
2
|
+
|
|
3
|
+
//#region \0@oxc-project+runtime@0.108.0/helpers/toPrimitive.js
|
|
4
|
+
function toPrimitive(t, r) {
|
|
5
|
+
if ("object" != _typeof(t) || !t) return t;
|
|
6
|
+
var e = t[Symbol.toPrimitive];
|
|
7
|
+
if (void 0 !== e) {
|
|
8
|
+
var i = e.call(t, r || "default");
|
|
9
|
+
if ("object" != _typeof(i)) return i;
|
|
10
|
+
throw new TypeError("@@toPrimitive must return a primitive value.");
|
|
11
|
+
}
|
|
12
|
+
return ("string" === r ? String : Number)(t);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
//#endregion
|
|
16
|
+
export { toPrimitive };
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
const require_typeof = require('./typeof.cjs');
|
|
2
|
+
const require_toPrimitive = require('./toPrimitive.cjs');
|
|
3
|
+
|
|
4
|
+
//#region \0@oxc-project+runtime@0.108.0/helpers/toPropertyKey.js
|
|
5
|
+
function toPropertyKey(t) {
|
|
6
|
+
var i = require_toPrimitive.toPrimitive(t, "string");
|
|
7
|
+
return "symbol" == require_typeof._typeof(i) ? i : i + "";
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
//#endregion
|
|
11
|
+
exports.toPropertyKey = toPropertyKey;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { _typeof } from "./typeof.mjs";
|
|
2
|
+
import { toPrimitive } from "./toPrimitive.mjs";
|
|
3
|
+
|
|
4
|
+
//#region \0@oxc-project+runtime@0.108.0/helpers/toPropertyKey.js
|
|
5
|
+
function toPropertyKey(t) {
|
|
6
|
+
var i = toPrimitive(t, "string");
|
|
7
|
+
return "symbol" == _typeof(i) ? i : i + "";
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
//#endregion
|
|
11
|
+
export { toPropertyKey };
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
|
|
2
|
+
//#region \0@oxc-project+runtime@0.108.0/helpers/typeof.js
|
|
3
|
+
function _typeof(o) {
|
|
4
|
+
"@babel/helpers - typeof";
|
|
5
|
+
return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function(o$1) {
|
|
6
|
+
return typeof o$1;
|
|
7
|
+
} : function(o$1) {
|
|
8
|
+
return o$1 && "function" == typeof Symbol && o$1.constructor === Symbol && o$1 !== Symbol.prototype ? "symbol" : typeof o$1;
|
|
9
|
+
}, _typeof(o);
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
//#endregion
|
|
13
|
+
Object.defineProperty(exports, '_typeof', {
|
|
14
|
+
enumerable: true,
|
|
15
|
+
get: function () {
|
|
16
|
+
return _typeof;
|
|
17
|
+
}
|
|
18
|
+
});
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
//#region \0@oxc-project+runtime@0.108.0/helpers/typeof.js
|
|
2
|
+
function _typeof(o) {
|
|
3
|
+
"@babel/helpers - typeof";
|
|
4
|
+
return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function(o$1) {
|
|
5
|
+
return typeof o$1;
|
|
6
|
+
} : function(o$1) {
|
|
7
|
+
return o$1 && "function" == typeof Symbol && o$1.constructor === Symbol && o$1 !== Symbol.prototype ? "symbol" : typeof o$1;
|
|
8
|
+
}, _typeof(o);
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
//#endregion
|
|
12
|
+
export { _typeof };
|
package/dist/index.cjs
ADDED
|
File without changes
|
package/dist/index.d.cts
ADDED
package/dist/index.d.mts
ADDED
package/dist/index.mjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { };
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
const require_objectSpread2 = require('./_virtual/_@oxc-project_runtime@0.108.0/helpers/objectSpread2.cjs');
|
|
2
|
+
let _hookform_resolvers_standard_schema = require("@hookform/resolvers/standard-schema");
|
|
3
|
+
let react_hook_form = require("react-hook-form");
|
|
4
|
+
|
|
5
|
+
//#region src/react-hook-form.ts
|
|
6
|
+
function createForm(schema) {
|
|
7
|
+
const resolver = (0, _hookform_resolvers_standard_schema.standardSchemaResolver)(schema);
|
|
8
|
+
return {
|
|
9
|
+
useForm: (props) => (0, react_hook_form.useForm)(require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, props), {}, { resolver })),
|
|
10
|
+
useFormContext: () => (0, react_hook_form.useFormContext)()
|
|
11
|
+
};
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
//#endregion
|
|
15
|
+
exports.createForm = createForm;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { StandardSchemaV1 } from "@standard-schema/spec";
|
|
2
|
+
import { FieldValues, UseFormProps, UseFormReturn } from "react-hook-form";
|
|
3
|
+
|
|
4
|
+
//#region src/react-hook-form.d.ts
|
|
5
|
+
interface CreateFormReturn<TFieldValues extends FieldValues, TTransformedValues> {
|
|
6
|
+
useForm: <TContext = any>(props?: Omit<UseFormProps<TFieldValues, TContext, TTransformedValues>, "resolver">) => UseFormReturn<TFieldValues, TContext, TTransformedValues>;
|
|
7
|
+
useFormContext: <TContext = any>() => UseFormReturn<TFieldValues, TContext, TTransformedValues>;
|
|
8
|
+
}
|
|
9
|
+
declare function createForm<TFieldValues extends FieldValues, TTransformedValues = TFieldValues>(schema: StandardSchemaV1<TFieldValues, TTransformedValues>): CreateFormReturn<TFieldValues, TTransformedValues>;
|
|
10
|
+
//#endregion
|
|
11
|
+
export { CreateFormReturn, createForm };
|
|
12
|
+
//# sourceMappingURL=react-hook-form.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"react-hook-form.d.cts","names":[],"sources":["../src/react-hook-form.ts"],"sourcesContent":[],"mappings":";;;;UAUiB,sCACM;oCAIX,KACN,aAAa,cAAc,UAAU,qCAGpC,cAAc,cAAc,UAAU;EAT5B,cAAA,EAAA,CAAA,WAAgB,GAAA,CAAA,GAAA,GAWO,aAXP,CAY7B,YAZ6B,EAa7B,QAb6B,EAc7B,kBAd6B,CAAA;;AACV,iBAiBP,UAjBO,CAAA,qBAkBA,WAlBA,EAAA,qBAmBA,YAnBA,CAAA,CAAA,MAAA,EAqBb,gBArBa,CAqBI,YArBJ,EAqBkB,kBArBlB,CAAA,CAAA,EAsBpB,gBAtBoB,CAsBH,YAtBG,EAsBW,kBAtBX,CAAA"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { FieldValues, UseFormProps, UseFormReturn } from "react-hook-form";
|
|
2
|
+
import { StandardSchemaV1 } from "@standard-schema/spec";
|
|
3
|
+
|
|
4
|
+
//#region src/react-hook-form.d.ts
|
|
5
|
+
interface CreateFormReturn<TFieldValues extends FieldValues, TTransformedValues> {
|
|
6
|
+
useForm: <TContext = any>(props?: Omit<UseFormProps<TFieldValues, TContext, TTransformedValues>, "resolver">) => UseFormReturn<TFieldValues, TContext, TTransformedValues>;
|
|
7
|
+
useFormContext: <TContext = any>() => UseFormReturn<TFieldValues, TContext, TTransformedValues>;
|
|
8
|
+
}
|
|
9
|
+
declare function createForm<TFieldValues extends FieldValues, TTransformedValues = TFieldValues>(schema: StandardSchemaV1<TFieldValues, TTransformedValues>): CreateFormReturn<TFieldValues, TTransformedValues>;
|
|
10
|
+
//#endregion
|
|
11
|
+
export { CreateFormReturn, createForm };
|
|
12
|
+
//# sourceMappingURL=react-hook-form.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"react-hook-form.d.mts","names":[],"sources":["../src/react-hook-form.ts"],"sourcesContent":[],"mappings":";;;;UAUiB,sCACM;oCAIX,KACN,aAAa,cAAc,UAAU,qCAGpC,cAAc,cAAc,UAAU;EAT5B,cAAA,EAAA,CAAA,WAAgB,GAAA,CAAA,GAAA,GAWO,aAXP,CAY7B,YAZ6B,EAa7B,QAb6B,EAc7B,kBAd6B,CAAA;;AACV,iBAiBP,UAjBO,CAAA,qBAkBA,WAlBA,EAAA,qBAmBA,YAnBA,CAAA,CAAA,MAAA,EAqBb,gBArBa,CAqBI,YArBJ,EAqBkB,kBArBlB,CAAA,CAAA,EAsBpB,gBAtBoB,CAsBH,YAtBG,EAsBW,kBAtBX,CAAA"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { _objectSpread2 } from "./_virtual/_@oxc-project_runtime@0.108.0/helpers/objectSpread2.mjs";
|
|
2
|
+
import { standardSchemaResolver } from "@hookform/resolvers/standard-schema";
|
|
3
|
+
import { useForm, useFormContext } from "react-hook-form";
|
|
4
|
+
|
|
5
|
+
//#region src/react-hook-form.ts
|
|
6
|
+
function createForm(schema) {
|
|
7
|
+
const resolver = standardSchemaResolver(schema);
|
|
8
|
+
return {
|
|
9
|
+
useForm: (props) => useForm(_objectSpread2(_objectSpread2({}, props), {}, { resolver })),
|
|
10
|
+
useFormContext: () => useFormContext()
|
|
11
|
+
};
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
//#endregion
|
|
15
|
+
export { createForm };
|
|
16
|
+
//# sourceMappingURL=react-hook-form.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"react-hook-form.mjs","names":["rhfUseForm","rhfUseFormContext"],"sources":["../src/react-hook-form.ts"],"sourcesContent":["import { standardSchemaResolver } from \"@hookform/resolvers/standard-schema\";\nimport type { StandardSchemaV1 } from \"@standard-schema/spec\";\nimport {\n type FieldValues,\n useForm as rhfUseForm,\n useFormContext as rhfUseFormContext,\n type UseFormProps,\n type UseFormReturn,\n} from \"react-hook-form\";\n\nexport interface CreateFormReturn<\n TFieldValues extends FieldValues,\n TTransformedValues,\n> {\n useForm: <TContext = any>(\n props?: Omit<\n UseFormProps<TFieldValues, TContext, TTransformedValues>,\n \"resolver\"\n >,\n ) => UseFormReturn<TFieldValues, TContext, TTransformedValues>;\n\n useFormContext: <TContext = any>() => UseFormReturn<\n TFieldValues,\n TContext,\n TTransformedValues\n >;\n}\n\nexport function createForm<\n TFieldValues extends FieldValues,\n TTransformedValues = TFieldValues,\n>(\n schema: StandardSchemaV1<TFieldValues, TTransformedValues>,\n): CreateFormReturn<TFieldValues, TTransformedValues> {\n const resolver = standardSchemaResolver(\n schema as StandardSchemaV1<FieldValues, FieldValues>,\n );\n\n return {\n useForm: (props) =>\n rhfUseForm({\n ...props,\n resolver,\n } as UseFormProps<FieldValues>) as unknown as UseFormReturn<\n TFieldValues,\n any,\n TTransformedValues\n >,\n\n useFormContext: () =>\n rhfUseFormContext() as unknown as UseFormReturn<\n TFieldValues,\n any,\n TTransformedValues\n >,\n };\n}\n"],"mappings":";;;;;AA4BA,SAAgB,WAId,QACoD;CACpD,MAAM,WAAW,uBACf,OACD;AAED,QAAO;EACL,UAAU,UACRA,0CACK,cACH,YAC6B;EAMjC,sBACEC,gBAAmB;EAKtB"}
|
package/dist/react.cjs
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
let react = require("react");
|
|
4
|
+
let react_jsx_runtime = require("react/jsx-runtime");
|
|
5
|
+
|
|
6
|
+
//#region src/react.tsx
|
|
7
|
+
const FormContext = (0, react.createContext)(null);
|
|
8
|
+
function FormProvider({ children }) {
|
|
9
|
+
const [isFormRegistered, setIsFormRegistered] = (0, react.useState)(false);
|
|
10
|
+
const formId = `form-${(0, react.useId)()}`;
|
|
11
|
+
const onSubmitRef = (0, react.useRef)(() => {});
|
|
12
|
+
const ref = (0, react.useCallback)((node) => {
|
|
13
|
+
if (node) setIsFormRegistered(true);
|
|
14
|
+
else setIsFormRegistered(false);
|
|
15
|
+
}, []);
|
|
16
|
+
const registerForm = (0, react.useCallback)((onSubmit) => {
|
|
17
|
+
onSubmitRef.current = onSubmit;
|
|
18
|
+
return {
|
|
19
|
+
id: formId,
|
|
20
|
+
ref
|
|
21
|
+
};
|
|
22
|
+
}, [formId, ref]);
|
|
23
|
+
const submitForm = (0, react.useCallback)(() => {
|
|
24
|
+
if (!isFormRegistered) throw new Error("Cannot submit form: no form is registered. Call registerForm first.");
|
|
25
|
+
return onSubmitRef.current();
|
|
26
|
+
}, [isFormRegistered]);
|
|
27
|
+
return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(FormContext, {
|
|
28
|
+
value: (0, react.useMemo)(() => ({
|
|
29
|
+
registerForm,
|
|
30
|
+
isFormRegistered,
|
|
31
|
+
submitForm,
|
|
32
|
+
formId: isFormRegistered ? formId : void 0
|
|
33
|
+
}), [
|
|
34
|
+
registerForm,
|
|
35
|
+
isFormRegistered,
|
|
36
|
+
submitForm,
|
|
37
|
+
formId
|
|
38
|
+
]),
|
|
39
|
+
children
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
function useFormContext() {
|
|
43
|
+
const context = (0, react.useContext)(FormContext);
|
|
44
|
+
if (!context) throw new Error("useFormContext must be used within a FormProvider");
|
|
45
|
+
return context;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
//#endregion
|
|
49
|
+
exports.FormProvider = FormProvider;
|
|
50
|
+
exports.useFormContext = useFormContext;
|
package/dist/react.d.cts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { FormContextValue, FormProps, FormSubmitHandler } from "./types.cjs";
|
|
2
|
+
import * as react_jsx_runtime0 from "react/jsx-runtime";
|
|
3
|
+
|
|
4
|
+
//#region src/react.d.ts
|
|
5
|
+
type FormProviderProps = {
|
|
6
|
+
children: React.ReactNode;
|
|
7
|
+
};
|
|
8
|
+
declare function FormProvider({
|
|
9
|
+
children
|
|
10
|
+
}: FormProviderProps): react_jsx_runtime0.JSX.Element;
|
|
11
|
+
declare function useFormContext(): FormContextValue;
|
|
12
|
+
//#endregion
|
|
13
|
+
export { type FormContextValue, type FormProps, FormProvider, FormProviderProps, type FormSubmitHandler, useFormContext };
|
|
14
|
+
//# sourceMappingURL=react.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"react.d.cts","names":[],"sources":["../src/react.tsx"],"sourcesContent":[],"mappings":";;;;KAiBY,iBAAA;YACA,KAAA,CAAM;AADlB,CAAA;AAIgB,iBAAA,YAAA,CAAY;EAAA;AAAA,CAAA,EAAe,iBAAf,CAAA,EAAgC,kBAAA,CAAA,GAAA,CAAA,OAAhC;AAAA,iBA4CZ,cAAA,CAAA,CA5CY,EA4CM,gBA5CN"}
|
package/dist/react.d.mts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { FormContextValue, FormProps, FormSubmitHandler } from "./types.mjs";
|
|
2
|
+
import * as react_jsx_runtime0 from "react/jsx-runtime";
|
|
3
|
+
|
|
4
|
+
//#region src/react.d.ts
|
|
5
|
+
type FormProviderProps = {
|
|
6
|
+
children: React.ReactNode;
|
|
7
|
+
};
|
|
8
|
+
declare function FormProvider({
|
|
9
|
+
children
|
|
10
|
+
}: FormProviderProps): react_jsx_runtime0.JSX.Element;
|
|
11
|
+
declare function useFormContext(): FormContextValue;
|
|
12
|
+
//#endregion
|
|
13
|
+
export { type FormContextValue, type FormProps, FormProvider, FormProviderProps, type FormSubmitHandler, useFormContext };
|
|
14
|
+
//# sourceMappingURL=react.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"react.d.mts","names":[],"sources":["../src/react.tsx"],"sourcesContent":[],"mappings":";;;;KAiBY,iBAAA;YACA,KAAA,CAAM;AADlB,CAAA;AAIgB,iBAAA,YAAA,CAAY;EAAA;AAAA,CAAA,EAAe,iBAAf,CAAA,EAAgC,kBAAA,CAAA,GAAA,CAAA,OAAhC;AAAA,iBA4CZ,cAAA,CAAA,CA5CY,EA4CM,gBA5CN"}
|
package/dist/react.mjs
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import { createContext, useCallback, useContext, useId, useMemo, useRef, useState } from "react";
|
|
4
|
+
import { jsx } from "react/jsx-runtime";
|
|
5
|
+
|
|
6
|
+
//#region src/react.tsx
|
|
7
|
+
const FormContext = createContext(null);
|
|
8
|
+
function FormProvider({ children }) {
|
|
9
|
+
const [isFormRegistered, setIsFormRegistered] = useState(false);
|
|
10
|
+
const formId = `form-${useId()}`;
|
|
11
|
+
const onSubmitRef = useRef(() => {});
|
|
12
|
+
const ref = useCallback((node) => {
|
|
13
|
+
if (node) setIsFormRegistered(true);
|
|
14
|
+
else setIsFormRegistered(false);
|
|
15
|
+
}, []);
|
|
16
|
+
const registerForm = useCallback((onSubmit) => {
|
|
17
|
+
onSubmitRef.current = onSubmit;
|
|
18
|
+
return {
|
|
19
|
+
id: formId,
|
|
20
|
+
ref
|
|
21
|
+
};
|
|
22
|
+
}, [formId, ref]);
|
|
23
|
+
const submitForm = useCallback(() => {
|
|
24
|
+
if (!isFormRegistered) throw new Error("Cannot submit form: no form is registered. Call registerForm first.");
|
|
25
|
+
return onSubmitRef.current();
|
|
26
|
+
}, [isFormRegistered]);
|
|
27
|
+
return /* @__PURE__ */ jsx(FormContext, {
|
|
28
|
+
value: useMemo(() => ({
|
|
29
|
+
registerForm,
|
|
30
|
+
isFormRegistered,
|
|
31
|
+
submitForm,
|
|
32
|
+
formId: isFormRegistered ? formId : void 0
|
|
33
|
+
}), [
|
|
34
|
+
registerForm,
|
|
35
|
+
isFormRegistered,
|
|
36
|
+
submitForm,
|
|
37
|
+
formId
|
|
38
|
+
]),
|
|
39
|
+
children
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
function useFormContext() {
|
|
43
|
+
const context = useContext(FormContext);
|
|
44
|
+
if (!context) throw new Error("useFormContext must be used within a FormProvider");
|
|
45
|
+
return context;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
//#endregion
|
|
49
|
+
export { FormProvider, useFormContext };
|
|
50
|
+
//# sourceMappingURL=react.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"react.mjs","names":[],"sources":["../src/react.tsx"],"sourcesContent":["\"use client\";\n\nimport {\n createContext,\n useCallback,\n useContext,\n useId,\n useMemo,\n useRef,\n useState,\n} from \"react\";\nimport type { FormContextValue, FormProps, FormSubmitHandler } from \"./types\";\n\nexport type { FormContextValue, FormProps, FormSubmitHandler };\n\nconst FormContext = createContext<FormContextValue | null>(null);\n\nexport type FormProviderProps = {\n children: React.ReactNode;\n};\n\nexport function FormProvider({ children }: FormProviderProps) {\n const [isFormRegistered, setIsFormRegistered] = useState(false);\n const generatedId = useId();\n const formId = `form-${generatedId}`;\n const onSubmitRef = useRef<FormSubmitHandler>(() => {});\n\n const ref = useCallback((node: HTMLFormElement | null) => {\n if (node) {\n setIsFormRegistered(true);\n } else {\n setIsFormRegistered(false);\n }\n }, []);\n\n const registerForm = useCallback(\n (onSubmit: FormSubmitHandler): FormProps => {\n onSubmitRef.current = onSubmit;\n return { id: formId, ref };\n },\n [formId, ref],\n );\n\n const submitForm = useCallback(() => {\n if (!isFormRegistered) {\n throw new Error(\n \"Cannot submit form: no form is registered. Call registerForm first.\",\n );\n }\n return onSubmitRef.current();\n }, [isFormRegistered]);\n\n const value = useMemo<FormContextValue>(\n () => ({\n registerForm,\n isFormRegistered,\n submitForm,\n formId: isFormRegistered ? formId : undefined,\n }),\n [registerForm, isFormRegistered, submitForm, formId],\n );\n\n return <FormContext value={value}>{children}</FormContext>;\n}\n\nexport function useFormContext(): FormContextValue {\n const context = useContext(FormContext);\n if (!context) {\n throw new Error(\"useFormContext must be used within a FormProvider\");\n }\n return context;\n}\n"],"mappings":";;;;;;AAeA,MAAM,cAAc,cAAuC,KAAK;AAMhE,SAAgB,aAAa,EAAE,YAA+B;CAC5D,MAAM,CAAC,kBAAkB,uBAAuB,SAAS,MAAM;CAE/D,MAAM,SAAS,QADK,OAAO;CAE3B,MAAM,cAAc,aAAgC,GAAG;CAEvD,MAAM,MAAM,aAAa,SAAiC;AACxD,MAAI,KACF,qBAAoB,KAAK;MAEzB,qBAAoB,MAAM;IAE3B,EAAE,CAAC;CAEN,MAAM,eAAe,aAClB,aAA2C;AAC1C,cAAY,UAAU;AACtB,SAAO;GAAE,IAAI;GAAQ;GAAK;IAE5B,CAAC,QAAQ,IAAI,CACd;CAED,MAAM,aAAa,kBAAkB;AACnC,MAAI,CAAC,iBACH,OAAM,IAAI,MACR,sEACD;AAEH,SAAO,YAAY,SAAS;IAC3B,CAAC,iBAAiB,CAAC;AAYtB,QAAO,oBAAC;EAAY,OAVN,eACL;GACL;GACA;GACA;GACA,QAAQ,mBAAmB,SAAS;GACrC,GACD;GAAC;GAAc;GAAkB;GAAY;GAAO,CACrD;EAEkC;GAAuB;;AAG5D,SAAgB,iBAAmC;CACjD,MAAM,UAAU,WAAW,YAAY;AACvC,KAAI,CAAC,QACH,OAAM,IAAI,MAAM,oDAAoD;AAEtE,QAAO"}
|
package/dist/types.d.cts
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
//#region src/types.d.ts
|
|
2
|
+
type FormSubmitHandler = () => void | Promise<void>;
|
|
3
|
+
type FormProps = {
|
|
4
|
+
id: string;
|
|
5
|
+
ref: (node: HTMLFormElement | null) => void;
|
|
6
|
+
};
|
|
7
|
+
type FormContextValue = {
|
|
8
|
+
/**
|
|
9
|
+
* Register a form with its submit handler.
|
|
10
|
+
* Only one form can be registered at a time.
|
|
11
|
+
* @returns Form props including id and ref to spread on the form element.
|
|
12
|
+
* The ref handles automatic unregistration when the form unmounts.
|
|
13
|
+
*/
|
|
14
|
+
registerForm: (onSubmit: FormSubmitHandler) => FormProps;
|
|
15
|
+
/**
|
|
16
|
+
* Whether a form is currently registered.
|
|
17
|
+
*/
|
|
18
|
+
isFormRegistered: boolean;
|
|
19
|
+
/**
|
|
20
|
+
* Submit the registered form.
|
|
21
|
+
* @throws Error if no form is registered
|
|
22
|
+
*/
|
|
23
|
+
submitForm: () => void | Promise<void>;
|
|
24
|
+
/**
|
|
25
|
+
* The id of the currently registered form, or undefined if none.
|
|
26
|
+
* Use this for the form attribute on buttons outside the form.
|
|
27
|
+
*/
|
|
28
|
+
formId: string | undefined;
|
|
29
|
+
};
|
|
30
|
+
//#endregion
|
|
31
|
+
export { FormContextValue, FormProps, FormSubmitHandler };
|
|
32
|
+
//# sourceMappingURL=types.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.cts","names":[],"sources":["../src/types.ts"],"sourcesContent":[],"mappings":";KAAY,iBAAA,gBAAiC;AAAjC,KAEA,SAAA,GAFiB;EAEjB,EAAA,EAAA,MAAA;EAKA,GAAA,EAAA,CAAA,IAAA,EAHE,eAGc,GAAA,IAAA,EAAA,GAAA,IAAA;CAAA;AAOD,KAPf,gBAAA,GAOe;;;;;;;2BAAA,sBAAsB;;;;;;;;;2BAWtB"}
|
package/dist/types.d.mts
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
//#region src/types.d.ts
|
|
2
|
+
type FormSubmitHandler = () => void | Promise<void>;
|
|
3
|
+
type FormProps = {
|
|
4
|
+
id: string;
|
|
5
|
+
ref: (node: HTMLFormElement | null) => void;
|
|
6
|
+
};
|
|
7
|
+
type FormContextValue = {
|
|
8
|
+
/**
|
|
9
|
+
* Register a form with its submit handler.
|
|
10
|
+
* Only one form can be registered at a time.
|
|
11
|
+
* @returns Form props including id and ref to spread on the form element.
|
|
12
|
+
* The ref handles automatic unregistration when the form unmounts.
|
|
13
|
+
*/
|
|
14
|
+
registerForm: (onSubmit: FormSubmitHandler) => FormProps;
|
|
15
|
+
/**
|
|
16
|
+
* Whether a form is currently registered.
|
|
17
|
+
*/
|
|
18
|
+
isFormRegistered: boolean;
|
|
19
|
+
/**
|
|
20
|
+
* Submit the registered form.
|
|
21
|
+
* @throws Error if no form is registered
|
|
22
|
+
*/
|
|
23
|
+
submitForm: () => void | Promise<void>;
|
|
24
|
+
/**
|
|
25
|
+
* The id of the currently registered form, or undefined if none.
|
|
26
|
+
* Use this for the form attribute on buttons outside the form.
|
|
27
|
+
*/
|
|
28
|
+
formId: string | undefined;
|
|
29
|
+
};
|
|
30
|
+
//#endregion
|
|
31
|
+
export { FormContextValue, FormProps, FormSubmitHandler };
|
|
32
|
+
//# sourceMappingURL=types.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.mts","names":[],"sources":["../src/types.ts"],"sourcesContent":[],"mappings":";KAAY,iBAAA,gBAAiC;AAAjC,KAEA,SAAA,GAFiB;EAEjB,EAAA,EAAA,MAAA;EAKA,GAAA,EAAA,CAAA,IAAA,EAHE,eAGc,GAAA,IAAA,EAAA,GAAA,IAAA;CAAA;AAOD,KAPf,gBAAA,GAOe;;;;;;;2BAAA,sBAAsB;;;;;;;;;2BAWtB"}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jlnstack/form",
|
|
3
3
|
"description": "Type-safe form utilities",
|
|
4
|
-
"version": "0.0.
|
|
4
|
+
"version": "0.0.1",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "jln13x",
|
|
7
7
|
"repository": {
|
|
@@ -9,20 +9,13 @@
|
|
|
9
9
|
"url": "https://github.com/jln13x/jlnstack"
|
|
10
10
|
},
|
|
11
11
|
"publishConfig": {
|
|
12
|
-
"access": "public"
|
|
12
|
+
"access": "public",
|
|
13
|
+
"provenance": true
|
|
13
14
|
},
|
|
14
15
|
"files": [
|
|
15
16
|
"dist",
|
|
16
17
|
"package.json"
|
|
17
18
|
],
|
|
18
|
-
"scripts": {
|
|
19
|
-
"build": "tsdown",
|
|
20
|
-
"dev": "tsdown --watch",
|
|
21
|
-
"lint": "tsc --noEmit",
|
|
22
|
-
"test": "vitest",
|
|
23
|
-
"test:run": "vitest run",
|
|
24
|
-
"test:ui": "vitest --ui"
|
|
25
|
-
},
|
|
26
19
|
"peerDependencies": {
|
|
27
20
|
"@hookform/resolvers": ">=4.0.0",
|
|
28
21
|
"react": ">=19",
|
|
@@ -46,7 +39,7 @@
|
|
|
46
39
|
"@vitest/ui": "^2.1.8",
|
|
47
40
|
"react-hook-form": "^7.54.0",
|
|
48
41
|
"tsdown": "^0.15.4",
|
|
49
|
-
"typescript": "
|
|
42
|
+
"typescript": "5.9.2",
|
|
50
43
|
"vitest": "^2.1.8"
|
|
51
44
|
},
|
|
52
45
|
"exports": {
|
|
@@ -81,5 +74,13 @@
|
|
|
81
74
|
"default": "./dist/react-hook-form.cjs"
|
|
82
75
|
}
|
|
83
76
|
}
|
|
77
|
+
},
|
|
78
|
+
"scripts": {
|
|
79
|
+
"build": "tsdown",
|
|
80
|
+
"dev": "tsdown --watch",
|
|
81
|
+
"lint": "tsc --noEmit",
|
|
82
|
+
"test": "vitest",
|
|
83
|
+
"test:run": "vitest run",
|
|
84
|
+
"test:ui": "vitest --ui"
|
|
84
85
|
}
|
|
85
|
-
}
|
|
86
|
+
}
|