@mobilestock-native/form 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.
Files changed (3) hide show
  1. package/index.d.ts +60 -0
  2. package/index.js +14 -0
  3. package/package.json +20 -0
package/index.d.ts ADDED
@@ -0,0 +1,60 @@
1
+ import * as react from 'react';
2
+ import { RefObject } from 'react';
3
+ import { TextInputProps } from 'react-native';
4
+ import { FormHandles } from '@unform/core';
5
+ import * as react_jsx_runtime from 'react/jsx-runtime';
6
+ import { ButtonProps } from '@mobilestock-native/button';
7
+ import { ViewBaseProps } from '@mobilestock-native/container';
8
+ import { FormProps } from '@unform/mobile';
9
+ import { ZodSchema } from 'zod';
10
+
11
+ type InputType = 'text' | 'password' | 'tel' | 'email' | 'number' | 'url' | 'zipcode' | 'hidden';
12
+ interface InputProps extends Omit<TextInputProps, 'hitSlop'> {
13
+ name: string;
14
+ label?: string;
15
+ autofocus?: boolean;
16
+ type?: InputType;
17
+ autoSubmit?: boolean;
18
+ full?: boolean;
19
+ format?(value: string): string;
20
+ }
21
+ interface InputRef {
22
+ focus(): void;
23
+ blur(): void;
24
+ }
25
+
26
+ interface FormButtonProps extends ButtonProps {
27
+ type?: 'SUBMIT' | 'RESET' | 'BUTTON';
28
+ }
29
+ declare function FormButton({ type, isLoading, onPress, ...props }: FormButtonProps): react_jsx_runtime.JSX.Element;
30
+
31
+ declare function FormHorizontal({ children, ...props }: ViewBaseProps): react_jsx_runtime.JSX.Element;
32
+
33
+ declare function FormVertical({ children, ...props }: ViewBaseProps): react_jsx_runtime.JSX.Element;
34
+
35
+ interface FormPropsWithHandler<T extends object> extends FormProps {
36
+ onSubmit(params: SubmitParams<T>): Promise<void> | void;
37
+ schema?: ZodSchema<T>;
38
+ }
39
+ interface PropsContext {
40
+ submitForm(): void;
41
+ clearForm(): void;
42
+ formRef: RefObject<FormHandles>;
43
+ loading?: boolean;
44
+ }
45
+ declare function useForm(): PropsContext;
46
+ declare function FormComponent<T extends object>({ children, onSubmit, schema, ...props }: FormPropsWithHandler<T>): react_jsx_runtime.JSX.Element;
47
+
48
+ interface SubmitParams<T extends object> {
49
+ data: T;
50
+ ref: RefObject<FormHandles>;
51
+ reset(): void;
52
+ }
53
+ declare const Form: typeof FormComponent & {
54
+ Input: react.ForwardRefExoticComponent<InputProps & react.RefAttributes<InputRef>>;
55
+ Button: typeof FormButton;
56
+ Vertical: typeof FormVertical;
57
+ Horizontal: typeof FormHorizontal;
58
+ };
59
+
60
+ export { Form, type SubmitParams, useForm };
package/index.js ADDED
@@ -0,0 +1,14 @@
1
+ "use strict";var vr=Object.create;var A=Object.defineProperty,Tr=Object.defineProperties,xr=Object.getOwnPropertyDescriptor,Pr=Object.getOwnPropertyDescriptors,Cr=Object.getOwnPropertyNames,j=Object.getOwnPropertySymbols,wr=Object.getPrototypeOf,q=Object.prototype.hasOwnProperty,tr=Object.prototype.propertyIsEnumerable;var or=(r,e,o)=>e in r?A(r,e,{enumerable:!0,configurable:!0,writable:!0,value:o}):r[e]=o,g=(r,e)=>{for(var o in e||(e={}))q.call(e,o)&&or(r,o,e[o]);if(j)for(var o of j(e))tr.call(e,o)&&or(r,o,e[o]);return r},F=(r,e)=>Tr(r,Pr(e));var E=(r,e)=>{var o={};for(var n in r)q.call(r,n)&&e.indexOf(n)<0&&(o[n]=r[n]);if(r!=null&&j)for(var n of j(r))e.indexOf(n)<0&&tr.call(r,n)&&(o[n]=r[n]);return o};var yr=(r,e)=>{for(var o in e)A(r,o,{get:e[o],enumerable:!0})},nr=(r,e,o,n)=>{if(e&&typeof e=="object"||typeof e=="function")for(let s of Cr(e))!q.call(r,s)&&s!==o&&A(r,s,{get:()=>e[s],enumerable:!(n=xr(e,s))||n.enumerable});return r};var z=(r,e,o)=>(o=r!=null?vr(wr(r)):{},nr(e||!r||!r.__esModule?A(o,"default",{value:r,enumerable:!0}):o,r)),Sr=r=>nr(A({},"__esModule",{value:!0}),r);var sr=(r,e,o)=>new Promise((n,s)=>{var c=l=>{try{p(o.next(l))}catch(f){s(f)}},m=l=>{try{p(o.throw(l))}catch(f){s(f)}},p=l=>l.done?n(l.value):Promise.resolve(l.value).then(c,m);p((o=o.apply(r,e)).next())});var Hr={};yr(Hr,{Form:()=>zr,useForm:()=>S});module.exports=Sr(Hr);var Ir=require("@mobilestock-native/button");var br=require("@unform/mobile"),h=require("react"),gr=require("zod"),Fr=require("@mobilestock-native/container"),Er=require("@mobilestock-native/modalalert");var er=z(require("react")),dr=z(require("@mobilestock-native/tools"));var J=z(require("react")),ir=require("@mobilestock-native/container");var ar=require("react/jsx-runtime");function G(o){var n=o,{children:r}=n,e=E(n,["children"]);let s=w(r).map((c,m)=>{if(J.default.isValidElement(c)){let p=c,l=`form-horizontal-${m}`;return J.default.cloneElement(p,{full:!0,style:{flex:1},key:l})}return c});return(0,ar.jsx)(ir.Container.Horizontal,F(g({gap:"SM"},e),{children:s}))}var mr=require("@unform/core"),d=require("react"),lr=require("react-native"),y=z(require("styled-components/native")),ur=require("@mobilestock-native/button"),rr=require("@mobilestock-native/container"),Q=z(require("@mobilestock-native/tools")),Y=require("@mobilestock-native/typography");var v=require("react/jsx-runtime"),U=(0,d.forwardRef)(function(H,W){var O=H,{name:e,label:o,defaultValue:n,type:s="text",autoSubmit:c=!1,format:m,onChangeText:p,autoCapitalize:l,full:f,maxLength:x}=O,K=E(O,["name","label","defaultValue","type","autoSubmit","format","onChangeText","autoCapitalize","full","maxLength"]);let{loading:X,submitForm:k}=S(),t=(0,mr.useField)(e),a=(0,d.useRef)(null),[P,M]=(0,d.useState)(!0),[I,T]=(0,d.useState)(""),C=s==="password",$=s==="tel"?15:s==="zipcode"?9:x;(0,d.useEffect)(()=>{let u=I||n||(t==null?void 0:t.defaultValue)||"";a.current.value=u,T(u),t.registerField({name:t.fieldName,ref:a.current,getValue(i){return(i==null?void 0:i.value)||""},setValue(i,R){i.value=R,T(R)},clearValue(i){i.value="",T("")}})},[t==null?void 0:t.fieldName,t==null?void 0:t.registerField]),(0,d.useImperativeHandle)(W,()=>({focus(){var u;(u=a.current)==null||u.focus()},blur(){var u;(u=a.current)==null||u.blur()}}));let L=(0,d.useCallback)(u=>{var R,B;let i=u;switch(s){case"tel":i=Q.default.phoneNumberFormatter(u);break;case"email":case"url":i=u.trim();break;case"number":i=u.replace(/[^0-9.,]/g,"");break;case"zipcode":i=Q.default.formatZipcode(i);break}m&&(i=m(i)),T(i),a.current.value=i,p==null||p(i),s==="tel"&&c&&i.length===15&&((R=a.current)==null||R.blur(),k()),s==="zipcode"&&c&&i.length===9&&((B=a.current)==null||B.blur(),k())},[s,m,c,p,t]),b=(0,d.useCallback)(()=>{switch(s){case"tel":return"phone-pad";case"email":return"email-address";case"url":return"url";case"number":return"numeric";default:return"default"}},[s]);return(0,v.jsxs)(Vr,{full:f,$show:s!=="hidden",children:[o&&(0,v.jsx)(Y.Typography,{children:o}),(0,v.jsxs)(Br,{error:!!(t!=null&&t.error),$isPassword:C,children:[(0,v.jsx)(Ar,F(g({},K),{ref:a,value:I,autoCapitalize:s&&["email","url"].includes(s)?"none":l,keyboardType:b(),secureTextEntry:P&&C,onChangeText:L,maxLength:$,editable:!X})),C&&(0,v.jsx)(ur.Button,{size:"SM",onPress:()=>M(!P),icon:P?"EyeOff":"EyeOutline",variant:"TRANSPARENT"})]}),(t==null?void 0:t.error)&&(0,v.jsx)(Y.Typography,{color:"DANGER",size:"SM",children:t.error})]})}),Vr=(0,y.default)(rr.Container.Vertical)`
2
+ ${({$show:r})=>!r&&y.css`
3
+ display: none;
4
+ `}
5
+ `,Br=(0,y.default)(rr.Container.Horizontal)`
6
+ overflow: hidden;
7
+ background-color: ${({error:r,theme:e})=>r?e.colors.input.error:e.colors.input.default};
8
+ border: 1px solid ${({error:r,theme:e})=>r?e.colors.alert.urgent:e.colors.input.border};
9
+ border-radius: ${({theme:r})=>r.borderRadius.default};
10
+ padding: ${({$isPassword:r})=>r?"0 0 0 10px":"0 10px"};
11
+ `,Ar=(0,y.default)(lr.TextInput)`
12
+ flex: 1;
13
+ height: 45px;
14
+ `;var cr=require("@mobilestock-native/container");var pr=require("react/jsx-runtime");function Z(o){var n=o,{children:r}=n,e=E(n,["children"]);let s=w(r);return(0,pr.jsx)(cr.Container.Vertical,F(g({gap:"SM"},e),{children:s}))}var fr=[{name:"Form.Button",component:D},{name:"Form.Horizontal",component:G},{name:"Form.Vertical",component:Z},{name:"Form.Input",component:U},{name:"Form",component:_}];function w(r){return er.default.Children.toArray(r).map(o=>{if(!er.default.isValidElement(o))throw new Error("Invalid child element. Consider using only Form allowed components.");if(!dr.default.validateSpecificChildren(o,fr.map(n=>n.component)))throw new Error(`Invalid child component. Expected one of ${fr.map(n=>n.name).join(", ")}, but received an invalid child`);return o})}var V=require("react/jsx-runtime"),hr=(0,h.createContext)({});function S(){return(0,h.useContext)(hr)}function _(s){var c=s,{children:r,onSubmit:e,schema:o}=c,n=E(c,["children","onSubmit","schema"]);let m=(0,h.useRef)(null),[p,l]=(0,h.useState)(!1),[f,x]=(0,h.useState)(void 0);function K(){var t;(t=m.current)==null||t.submitForm()}function W(){var t,a;(t=m.current)==null||t.setErrors({}),(a=m.current)==null||a.reset()}function H(t){let a={};for(let P of t){let M=P.path.filter(I=>typeof I=="string"||typeof I=="number").join(".");M&&(a[String(M)]=P.message)}return a}function O(t){if(t instanceof gr.ZodError)return H(t.issues);if(typeof t=="object"&&t!==null&&"errors"in t){let a=t.errors;if(Array.isArray(a)&&a.length>0&&"message"in a[0]&&"path"in a[0])return H(a)}return null}function X(P,M){return sr(this,arguments,function*(t,{reset:a}){var I,T,C,$,L;if((I=m.current)==null||I.setErrors({}),o){let b=o.safeParse(t);if(!b.success){(T=m.current)==null||T.setErrors(H(b.error.issues));return}}try{l(!0),yield e({data:t,ref:m,reset:a})}catch(b){let u=O(b);if(u)(C=m.current)==null||C.setErrors(u);else{let i="Erro ao realizar opera\xE7\xE3o",R=500;if(typeof b=="object"&&b!==null&&"isAxiosError"in b&&b.isAxiosError){let B=b;R=(($=B.response)==null?void 0:$.status)||500;let N=(L=B.response)==null?void 0:L.data;N!=null&&N.message&&(i=N.message)}else b instanceof Error&&(i=b.message||i);x({message:i,status:R})}}finally{l(!1)}})}let k=w(r);return(0,V.jsxs)(hr.Provider,{value:{formRef:m,submitForm:K,clearForm:W,loading:p},children:[(0,V.jsx)(br.Form,F(g({ref:m,onSubmit:X},n),{children:(0,V.jsx)(Fr.Container.Vertical,{gap:"2XS",children:k})})),(0,V.jsx)(Er.ModalAlert,{visible:!!f,message:f==null?void 0:f.message,type:f&&f.status>=500?"FATAL_ERROR":"ERROR_NOTICE",title:"Erro ao enviar o formul\xE1rio",onClose:()=>x(void 0)})]})}var Rr=require("react/jsx-runtime");function D(s){var c=s,{type:r="SUBMIT",isLoading:e,onPress:o}=c,n=E(c,["type","isLoading","onPress"]);let{submitForm:m,clearForm:p,loading:l}=S();function f(x){switch(r){case"SUBMIT":m();break;case"RESET":p();break;default:o==null||o(x)}}return(0,Rr.jsx)(Ir.Button,F(g({size:"LG"},n),{onPress:f,isLoading:r==="SUBMIT"&&l||e,disabled:l||e}))}var zr=Object.assign(_,{Input:U,Button:D,Vertical:Z,Horizontal:G});0&&(module.exports={Form,useForm});
package/package.json ADDED
@@ -0,0 +1,20 @@
1
+ {
2
+ "name": "@mobilestock-native/form",
3
+ "version": "0.0.1",
4
+ "main": "index.js",
5
+ "dependencies": {
6
+ "@unform/core": "^2.1.6",
7
+ "@mobilestock-native/button": "^1.0.6",
8
+ "@mobilestock-native/container": "^0.0.4",
9
+ "@mobilestock-native/icons": "^0.0.5",
10
+ "@mobilestock-native/modalalert": "^0.0.6",
11
+ "@mobilestock-native/tools": "^0.0.8",
12
+ "@mobilestock-native/typography": "^0.0.7"
13
+ },
14
+ "peerDependencies": {
15
+ "react": "18.2.0",
16
+ "react-native": "0.74.5",
17
+ "styled-components": "^6.1.8",
18
+ "zod": "^3.24.2"
19
+ }
20
+ }