remix-validated-form 4.5.5 → 4.5.6
Sign up to get free protection for your applications and to get access to all the features.
- package/.turbo/turbo-build.log +13 -13
- package/browser/ValidatedForm.js +1 -1
- package/browser/internal/logic/requestSubmit.d.ts +0 -0
- package/browser/internal/logic/requestSubmit.js +27 -2
- package/dist/remix-validated-form.cjs.js +1 -1
- package/dist/remix-validated-form.cjs.js.map +1 -1
- package/dist/remix-validated-form.es.js +1 -1
- package/dist/remix-validated-form.es.js.map +1 -1
- package/dist/remix-validated-form.umd.js +1 -1
- package/dist/remix-validated-form.umd.js.map +1 -1
- package/package.json +1 -1
- package/src/ValidatedForm.tsx +1 -1
package/.turbo/turbo-build.log
CHANGED
@@ -1,17 +1,17 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
$ vite build
|
2
|
+
vite v2.9.5 building for production...
|
3
3
|
transforming...
|
4
|
-
|
4
|
+
✓ 483 modules transformed.
|
5
5
|
rendering chunks...
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
6
|
+
dist/remix-validated-form.cjs.js 45.60 KiB / gzip: 17.17 KiB
|
7
|
+
dist/remix-validated-form.cjs.js.map 274.96 KiB
|
8
|
+
dist/remix-validated-form.es.js 101.90 KiB / gzip: 24.05 KiB
|
9
|
+
dist/remix-validated-form.es.js.map 269.50 KiB
|
10
|
+
dist/remix-validated-form.umd.js 45.80 KiB / gzip: 17.27 KiB
|
11
|
+
dist/remix-validated-form.umd.js.map 274.92 KiB
|
12
|
+
|
13
|
+
[vite:dts] Start generate declaration files...
|
14
|
+
[vite:dts] Declaration files built in 2964ms.
|
15
|
+
|
16
16
|
No name was provided for external module 'react' in output.globals – guessing 'React'
|
17
17
|
No name was provided for external module '@remix-run/react' in output.globals – guessing 'react'
|
package/browser/ValidatedForm.js
CHANGED
@@ -167,8 +167,8 @@ export function ValidatedForm({ validator, onSubmit, children, fetcher, action,
|
|
167
167
|
startSubmit();
|
168
168
|
const result = await validator.validate(getDataFromForm(e.currentTarget));
|
169
169
|
if (result.error) {
|
170
|
-
endSubmit();
|
171
170
|
setFieldErrors(result.error.fieldErrors);
|
171
|
+
endSubmit();
|
172
172
|
if (!disableFocusOnError) {
|
173
173
|
focusFirstInvalidInput(result.error.fieldErrors, customFocusHandlers(), formRef.current);
|
174
174
|
}
|
File without changes
|
@@ -1,4 +1,3 @@
|
|
1
|
-
const isVitest = !!import.meta.vitest;
|
2
1
|
/**
|
3
2
|
* Ponyfill of the HTMLFormElement.requestSubmit() method.
|
4
3
|
* Based on polyfill from: https://github.com/javan/form-request-submit-polyfill/blob/main/form-request-submit-polyfill.js
|
@@ -7,7 +6,7 @@ export const requestSubmit = (element, submitter) => {
|
|
7
6
|
// In vitest, let's test the polyfill.
|
8
7
|
// Cypress will test the native implementation by nature of using chrome.
|
9
8
|
if (typeof Object.getPrototypeOf(element).requestSubmit === "function" &&
|
10
|
-
!
|
9
|
+
!import.meta.vitest) {
|
11
10
|
element.requestSubmit(submitter);
|
12
11
|
return;
|
13
12
|
}
|
@@ -39,3 +38,29 @@ function validateSubmitter(element, submitter) {
|
|
39
38
|
function raise(errorConstructor, message, name) {
|
40
39
|
throw new errorConstructor("Failed to execute 'requestSubmit' on 'HTMLFormElement': " + message + ".", name);
|
41
40
|
}
|
41
|
+
if (import.meta.vitest) {
|
42
|
+
const { it, expect } = import.meta.vitest;
|
43
|
+
it("should validate the submitter", () => {
|
44
|
+
const form = document.createElement("form");
|
45
|
+
document.body.appendChild(form);
|
46
|
+
const submitter = document.createElement("input");
|
47
|
+
expect(() => validateSubmitter(null, null)).toThrow();
|
48
|
+
expect(() => validateSubmitter(form, null)).toThrow();
|
49
|
+
expect(() => validateSubmitter(form, submitter)).toThrow();
|
50
|
+
expect(() => validateSubmitter(form, document.createElement("div"))).toThrow();
|
51
|
+
submitter.type = "submit";
|
52
|
+
expect(() => validateSubmitter(form, submitter)).toThrow();
|
53
|
+
form.appendChild(submitter);
|
54
|
+
expect(() => validateSubmitter(form, submitter)).not.toThrow();
|
55
|
+
form.removeChild(submitter);
|
56
|
+
expect(() => validateSubmitter(form, submitter)).toThrow();
|
57
|
+
document.body.appendChild(submitter);
|
58
|
+
form.id = "test-form";
|
59
|
+
submitter.setAttribute("form", "test-form");
|
60
|
+
expect(() => validateSubmitter(form, submitter)).not.toThrow();
|
61
|
+
const button = document.createElement("button");
|
62
|
+
button.type = "submit";
|
63
|
+
form.appendChild(button);
|
64
|
+
expect(() => validateSubmitter(form, button)).not.toThrow();
|
65
|
+
});
|
66
|
+
}
|
@@ -23,5 +23,5 @@
|
|
23
23
|
* LICENSE.md file in the root directory of this source tree.
|
24
24
|
*
|
25
25
|
* @license MIT
|
26
|
-
*/const Jv=(e,t={})=>{let r=typeof t=="number"?{status:t}:t,n=new Headers(r.headers);return n.has("Content-Type")||n.set("Content-Type","application/json; charset=utf-8"),new Response(JSON.stringify(e),{...r,headers:n})};function Yv(e,t,r){return Jv({fieldErrors:e.fieldErrors,subaction:e.subaction,repopulateFields:t,formId:e.formId},{status:422,...r})}const Zv=(e,t)=>({[Br(e)]:t});function Qv(e,t,r,n){for(var a=e.length,s=r+(n?1:-1);n?s--:++s<a;)if(t(e[s],s,e))return s;return-1}var eh=Qv;function th(e){return e!==e}var rh=th;function nh(e,t,r){for(var n=r-1,a=e.length;++n<a;)if(e[n]===t)return n;return-1}var ah=nh,sh=eh,oh=rh,ih=ah;function uh(e,t,r){return t===t?ih(e,t,r):sh(e,oh,r)}var ch=uh,fh=ch;function lh(e,t){var r=e==null?0:e.length;return!!r&&fh(e,t,0)>-1}var dh=lh;function vh(e,t,r){for(var n=-1,a=e==null?0:e.length;++n<a;)if(r(t,e[n]))return!0;return!1}var hh=vh;function yh(){}var ph=yh,je=Mr,mh=ph,gh=Qe,bh=1/0,_h=je&&1/gh(new je([,-0]))[1]==bh?function(e){return new je(e)}:mh,$h=_h,Sh=br,Eh=dh,Ph=hh,Fh=_r,Ih=$h,Th=Qe,Ah=200;function Oh(e,t,r){var n=-1,a=Eh,s=e.length,o=!0,i=[],u=i;if(r)o=!1,a=Ph;else if(s>=Ah){var c=t?null:Ih(e);if(c)return Th(c);o=!1,a=Fh,u=new Sh}else u=t?[]:i;e:for(;++n<s;){var f=e[n],d=t?t(f):f;if(f=r||f!==0?f:0,o&&d===d){for(var l=u.length;l--;)if(u[l]===d)continue e;t&&u.push(d),i.push(f)}else a(u,d,r)||(u!==i&&u.push(d),i.push(f))}return i}var wh=Oh,Ch=wh;function xh(e){return e&&e.length?Ch(e):[]}var Dh=xh;class yn{constructor(){this.dict=new Map,this.add=(t,r)=>{this.dict.has(t)?this.dict.get(t).push(r):this.dict.set(t,[r])},this.delete=t=>{this.dict.delete(t)},this.remove=(t,r)=>{if(!this.dict.has(t))return;const n=this.dict.get(t),a=n.indexOf(r);a!==-1&&n.splice(a,1),n.length===0&&this.dict.delete(t)},this.getAll=t=>{var r;return(r=this.dict.get(t))!=null?r:[]},this.entries=()=>this.dict.entries(),this.values=()=>this.dict.values(),this.has=t=>this.dict.has(t)}}const Mh=()=>{const e=g.useRef(null);return g.useCallback(()=>(e.current||(e.current=new yn),e.current),[])};function pn(e,t){const r=g.useRef(!1);g.useEffect(()=>{e&&(r.current=!0),!e&&r.current&&(r.current=!1,t())})}var jh=rt;function Rh(e,t){return jh(e,t)}var Lh=Rh;const Hh=e=>t=>{e.filter(Boolean).forEach(r=>{typeof r=="function"?r(t):r!=null&&(r.current=t)})},Re=typeof window!="undefined"?g.useLayoutEffect:g.useEffect,Vh=e=>{const t=g.useRef(e),r=t.current===e||Lh(t.current,e);return g.useEffect(()=>{r||(t.current=e)}),r?t.current:e},Gh=e=>new FormData(e);function Nh(e){return e!==null}const Kh=(e,t,r)=>{var s;const n=[...r.elements].map(o=>{const i=o instanceof RadioNodeList?o[0]:o;return i instanceof HTMLElement&&"name"in i?i.name:null}).filter(Nh).filter(o=>o in e),a=Dh(n);for(const o of a){if(t.has(o)){t.getAll(o).forEach(u=>{u()});break}const i=r.elements.namedItem(o);if(!!i){if(i instanceof RadioNodeList){const u=(s=[...i].filter(c=>c instanceof HTMLInputElement).find(c=>c.value===i.value))!=null?s:i[0];if(u&&u instanceof HTMLInputElement){u.focus();break}}if(i instanceof HTMLElement){if(i instanceof HTMLInputElement&&i.type==="hidden")continue;i.focus();break}}}},Uh=e=>{const[t]=g.useState(()=>Symbol("remix-validated-form-id"));return e!=null?e:t},Bh=({resetAfterSubmit:e,formRef:t})=>{const r=vn(),n=hn();return pn(r,()=>{var a;n&&e&&((a=t.current)==null||a.reset())}),null};function qh(e){let t=!1;return new Proxy(e,{get:(r,n)=>n==="preventDefault"?()=>{t=!0}:n==="defaultPrevented"?t:r[n]})}function zh({validator:e,onSubmit:t,children:r,fetcher:n,action:a,defaultValues:s,formRef:o,onReset:i,subaction:u,resetAfterSubmit:c=!1,disableFocusOnError:f,method:d,replace:l,id:v,...p}){var St;const h=Uh(v),_=Vh(s),S=g.useMemo(()=>({formId:h,action:a,subaction:u,defaultValuesProp:_,fetcher:n}),[a,n,h,_,u]),$=pt(S),A=tn(S),D=wv(S),F=g.useRef(null),mn=(St=n==null?void 0:n.Form)!=null?St:Z.Form,gn=Z.useSubmit(),Ee=dn(h),bn=m(h,y=>y.setFieldError),_n=m(h,y=>y.reset),$n=O(y=>y.reset),Sn=m(h,y=>y.startSubmit),Pe=m(h,y=>y.endSubmit),mt=m(h,y=>y.syncFormProps),gt=m(h,y=>y.setFormElement),bt=Xe(y=>y.cleanupForm),_t=Xe(y=>y.registerForm),se=Mh(),$t=g.useCallback((y,oe)=>(se().add(y,oe),()=>{se().remove(y,oe)}),[se]);Re(()=>(_t(h),()=>bt(h)),[bt,h,_t]),Re(()=>{var y;mt({action:a,defaultValues:(y=_!=null?_:A)!=null?y:{},subaction:u,registerReceiveFocus:$t,validator:e})},[a,_,$t,u,mt,A,e]),Re(()=>{gt(F.current)},[gt]),g.useEffect(()=>{var y;Ee((y=$==null?void 0:$.fieldErrors)!=null?y:{})},[$==null?void 0:$.fieldErrors,Ee,bn]),pn(D,()=>{Pe()});const En=async(y,oe,Pn)=>{Sn();const ie=await e.validate(Gh(y.currentTarget));if(ie.error)
|
26
|
+
*/const Jv=(e,t={})=>{let r=typeof t=="number"?{status:t}:t,n=new Headers(r.headers);return n.has("Content-Type")||n.set("Content-Type","application/json; charset=utf-8"),new Response(JSON.stringify(e),{...r,headers:n})};function Yv(e,t,r){return Jv({fieldErrors:e.fieldErrors,subaction:e.subaction,repopulateFields:t,formId:e.formId},{status:422,...r})}const Zv=(e,t)=>({[Br(e)]:t});function Qv(e,t,r,n){for(var a=e.length,s=r+(n?1:-1);n?s--:++s<a;)if(t(e[s],s,e))return s;return-1}var eh=Qv;function th(e){return e!==e}var rh=th;function nh(e,t,r){for(var n=r-1,a=e.length;++n<a;)if(e[n]===t)return n;return-1}var ah=nh,sh=eh,oh=rh,ih=ah;function uh(e,t,r){return t===t?ih(e,t,r):sh(e,oh,r)}var ch=uh,fh=ch;function lh(e,t){var r=e==null?0:e.length;return!!r&&fh(e,t,0)>-1}var dh=lh;function vh(e,t,r){for(var n=-1,a=e==null?0:e.length;++n<a;)if(r(t,e[n]))return!0;return!1}var hh=vh;function yh(){}var ph=yh,je=Mr,mh=ph,gh=Qe,bh=1/0,_h=je&&1/gh(new je([,-0]))[1]==bh?function(e){return new je(e)}:mh,$h=_h,Sh=br,Eh=dh,Ph=hh,Fh=_r,Ih=$h,Th=Qe,Ah=200;function Oh(e,t,r){var n=-1,a=Eh,s=e.length,o=!0,i=[],u=i;if(r)o=!1,a=Ph;else if(s>=Ah){var c=t?null:Ih(e);if(c)return Th(c);o=!1,a=Fh,u=new Sh}else u=t?[]:i;e:for(;++n<s;){var f=e[n],d=t?t(f):f;if(f=r||f!==0?f:0,o&&d===d){for(var l=u.length;l--;)if(u[l]===d)continue e;t&&u.push(d),i.push(f)}else a(u,d,r)||(u!==i&&u.push(d),i.push(f))}return i}var wh=Oh,Ch=wh;function xh(e){return e&&e.length?Ch(e):[]}var Dh=xh;class yn{constructor(){this.dict=new Map,this.add=(t,r)=>{this.dict.has(t)?this.dict.get(t).push(r):this.dict.set(t,[r])},this.delete=t=>{this.dict.delete(t)},this.remove=(t,r)=>{if(!this.dict.has(t))return;const n=this.dict.get(t),a=n.indexOf(r);a!==-1&&n.splice(a,1),n.length===0&&this.dict.delete(t)},this.getAll=t=>{var r;return(r=this.dict.get(t))!=null?r:[]},this.entries=()=>this.dict.entries(),this.values=()=>this.dict.values(),this.has=t=>this.dict.has(t)}}const Mh=()=>{const e=g.useRef(null);return g.useCallback(()=>(e.current||(e.current=new yn),e.current),[])};function pn(e,t){const r=g.useRef(!1);g.useEffect(()=>{e&&(r.current=!0),!e&&r.current&&(r.current=!1,t())})}var jh=rt;function Rh(e,t){return jh(e,t)}var Lh=Rh;const Hh=e=>t=>{e.filter(Boolean).forEach(r=>{typeof r=="function"?r(t):r!=null&&(r.current=t)})},Re=typeof window!="undefined"?g.useLayoutEffect:g.useEffect,Vh=e=>{const t=g.useRef(e),r=t.current===e||Lh(t.current,e);return g.useEffect(()=>{r||(t.current=e)}),r?t.current:e},Gh=e=>new FormData(e);function Nh(e){return e!==null}const Kh=(e,t,r)=>{var s;const n=[...r.elements].map(o=>{const i=o instanceof RadioNodeList?o[0]:o;return i instanceof HTMLElement&&"name"in i?i.name:null}).filter(Nh).filter(o=>o in e),a=Dh(n);for(const o of a){if(t.has(o)){t.getAll(o).forEach(u=>{u()});break}const i=r.elements.namedItem(o);if(!!i){if(i instanceof RadioNodeList){const u=(s=[...i].filter(c=>c instanceof HTMLInputElement).find(c=>c.value===i.value))!=null?s:i[0];if(u&&u instanceof HTMLInputElement){u.focus();break}}if(i instanceof HTMLElement){if(i instanceof HTMLInputElement&&i.type==="hidden")continue;i.focus();break}}}},Uh=e=>{const[t]=g.useState(()=>Symbol("remix-validated-form-id"));return e!=null?e:t},Bh=({resetAfterSubmit:e,formRef:t})=>{const r=vn(),n=hn();return pn(r,()=>{var a;n&&e&&((a=t.current)==null||a.reset())}),null};function qh(e){let t=!1;return new Proxy(e,{get:(r,n)=>n==="preventDefault"?()=>{t=!0}:n==="defaultPrevented"?t:r[n]})}function zh({validator:e,onSubmit:t,children:r,fetcher:n,action:a,defaultValues:s,formRef:o,onReset:i,subaction:u,resetAfterSubmit:c=!1,disableFocusOnError:f,method:d,replace:l,id:v,...p}){var St;const h=Uh(v),_=Vh(s),S=g.useMemo(()=>({formId:h,action:a,subaction:u,defaultValuesProp:_,fetcher:n}),[a,n,h,_,u]),$=pt(S),A=tn(S),D=wv(S),F=g.useRef(null),mn=(St=n==null?void 0:n.Form)!=null?St:Z.Form,gn=Z.useSubmit(),Ee=dn(h),bn=m(h,y=>y.setFieldError),_n=m(h,y=>y.reset),$n=O(y=>y.reset),Sn=m(h,y=>y.startSubmit),Pe=m(h,y=>y.endSubmit),mt=m(h,y=>y.syncFormProps),gt=m(h,y=>y.setFormElement),bt=Xe(y=>y.cleanupForm),_t=Xe(y=>y.registerForm),se=Mh(),$t=g.useCallback((y,oe)=>(se().add(y,oe),()=>{se().remove(y,oe)}),[se]);Re(()=>(_t(h),()=>bt(h)),[bt,h,_t]),Re(()=>{var y;mt({action:a,defaultValues:(y=_!=null?_:A)!=null?y:{},subaction:u,registerReceiveFocus:$t,validator:e})},[a,_,$t,u,mt,A,e]),Re(()=>{gt(F.current)},[gt]),g.useEffect(()=>{var y;Ee((y=$==null?void 0:$.fieldErrors)!=null?y:{})},[$==null?void 0:$.fieldErrors,Ee,bn]),pn(D,()=>{Pe()});const En=async(y,oe,Pn)=>{Sn();const ie=await e.validate(Gh(y.currentTarget));if(ie.error)Ee(ie.error.fieldErrors),Pe(),f||Kh(ie.error.fieldErrors,se(),F.current);else{const Et=qh(y);if(await(t==null?void 0:t(ie.data,Et)),Et.defaultPrevented){Pe();return}const Pt=Pn.submitter;n?n.submit(Pt||y.currentTarget):gn(Pt||oe,{replace:l})}};return C.default.createElement(mn,{ref:Hh([F,o]),...p,id:v,action:a,method:d,replace:l,onSubmit:y=>{y.preventDefault(),En(y,y.currentTarget,y.nativeEvent)},onReset:y=>{i==null||i(y),!y.defaultPrevented&&(_n(),$n(h))}},C.default.createElement(qr.Provider,{value:S},C.default.createElement(C.default.Fragment,null,C.default.createElement(Bh,{formRef:F,resetAfterSubmit:c}),u&&C.default.createElement("input",{type:"hidden",value:u,name:"subaction"}),v&&C.default.createElement("input",{type:"hidden",value:v,name:fe}),r)))}var Wh=Ur;function kh(e,t,r){return e==null?e:Wh(e,t,r)}var Xh=kh;const dr=e=>{const t=new yn;return e.forEach(([r,n])=>t.add(r,n)),[...t.entries()].reduce((r,[n,a])=>Xh(r,n,a.length===1?a[0]:a),{})},vr=e=>"entries"in e&&typeof e.entries=="function"?dr([...e.entries()]):dr(Object.entries(e));function Jh(e){return{validate:async t=>{const r=vr(t),n=await e.validate(r);return n.error?{data:void 0,error:{fieldErrors:n.error,subaction:r.subaction,formId:r[fe]},submittedData:r,formId:r[fe]}:{data:n.data,error:void 0,submittedData:r,formId:r[fe]}},validateField:(t,r)=>e.validateField(vr(t),r)}}const Yh=e=>{const t=j(e,"useFormState"),r=sn(t.formId),n=un(t.formId),a=Lv(t.formId),s=on(t.formId),o=Nv(t.formId),i=Kv(t.formId),u=ln(t.formId),f=rn(t).hydrateTo(u),d=Hv(t.formId),v=en(t).hydrateTo(d);return g.useMemo(()=>({action:o,subaction:i,defaultValues:f,fieldErrors:v!=null?v:{},hasBeenSubmitted:n,isSubmitting:r,touchedFields:a,isValid:s}),[o,n,f,v,r,s,i,a])},Zh=e=>{const t=j(e,"useFormHelpers"),r=Rv(t),n=cn(t.formId),a=Dv(t.formId),s=nn(t),o=dn(t.formId),i=Vv(t.formId),u=Gv(t.formId),c=Uv(t.formId);return g.useMemo(()=>({setTouched:r,validateField:n,clearError:s,validate:a,clearAllErrors:()=>o({}),reset:i,submit:u,getValues:c}),[s,i,o,r,u,a,n,c])},Qh=e=>{const t=j(e,"useFormContext"),r=Yh(e),{clearError:n,setTouched:a,validateField:s,clearAllErrors:o,validate:i,reset:u,submit:c,getValues:f}=Zh(e),d=fn(t.formId),l=g.useCallback((...v)=>{v.forEach(p=>{n(p)})},[n]);return g.useMemo(()=>({...r,setFieldTouched:a,validateField:s,clearError:l,registerReceiveFocus:d,clearAllErrors:o,validate:i,reset:u,submit:c,getValues:f}),[o,l,d,u,a,r,c,i,s,f])};exports.ValidatedForm=zh;exports.createValidator=Jh;exports.setFormDefaults=Zv;exports.useControlField=kv;exports.useField=Wv;exports.useFormContext=Qh;exports.useIsSubmitting=vn;exports.useIsValid=hn;exports.useUpdateControlledField=Xv;exports.validationError=Yv;
|
27
27
|
//# sourceMappingURL=remix-validated-form.cjs.js.map
|