@wwog/react 1.3.10 → 1.3.12
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/dist/index.js +1 -1
- package/package.json +12 -11
- package/src/components/Struct/ArrayRender.tsx +14 -9
- package/src/utils/createExternalState.ts +21 -12
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import i,{useMemo as S,Fragment as v,Children as Z,isValidElement as q,cloneElement as U,useEffect as b,useState as I,useRef as O,useCallback as G}from"react";function j(e,n){if(e===void 0)return;let t=0;if(Array.isArray(e)){for(const l of e)if(n(l,t++)===!1)break}else n(e,t)}const K=(e,n)=>e===n,N=e=>i.createElement(i.Fragment,null,e.children);N.displayName="Switch_Case";const D=e=>i.createElement(i.Fragment,null,e.children);D.displayName="Switch_Default";const w=e=>{const{value:n,compare:t=K,children:l,strict:r=!1}=e,o=new Set;let s=null,u=null,f=!1;return j(l,(a,c)=>{if(!i.isValidElement(a))throw new Error(`Switch Children only accepts valid React elements at index ${c}`);const d=a.type;if(d.displayName===N.displayName){const m=a.props;if(o.has(m.value))throw new Error(`Switch found duplicate Case value at index ${c}: ${JSON.stringify(m.value)}${r?" (detected in strict mode)":""}`);if(o.add(m.value),!s&&t(n,m.value)&&(s=m.children,r===!1))return!1}else if(d.displayName===D.displayName){if(f)throw new Error(`Switch can only have one Default child at index ${c}`);if(f=!0,u=a.props.children,!r&&s)return!1}else throw new Error(`Switch Children only accepts 'Case' or 'Default' elements, found: ${String(d.displayName||d.name||d)} at index ${c}`)}),i.createElement(i.Fragment,null,s??u)};w.displayName="Switch",w.Case=N,w.Default=D,w.createTyped=function(){return{Switch:w,Case:N,Default:D}};const A=e=>i.createElement(i.Fragment,null,e.children),x=({children:e})=>i.createElement(i.Fragment,null,e),M=e=>i.createElement(i.Fragment,null,e.children);A.displayName="If_Then",x.displayName="If_Else",M.displayName="If_ElseIf";const E=({condition:e,children:n})=>{let t=null,l=null;const r=[];if(i.Children.forEach(n,o=>{if(!i.isValidElement(o))throw new Error("If component only accepts valid React elements");const s=o.type;if(s.displayName===A.displayName){if(t)throw new Error("If component can only have one Then child");t=o}else if(s.displayName===M.displayName)r.push(o);else if(s.displayName===x.displayName){if(l)throw new Error("If component can only have one Else child");l=o}else throw new Error(`If component only accepts 'Then', 'ElseIf', or 'Else' elements as children, found: ${String(s.displayName||s.name||s)}`)}),e)return t?i.createElement(i.Fragment,null,t.props.children):null;for(const o of r)if(o.props.condition)return i.createElement(i.Fragment,null,o.props.children);return l?i.createElement(i.Fragment,null,l.props.children):null};E.displayName="If",E.Then=A,E.ElseIf=M,E.Else=x,E.createTyped=function(){return{If:E,Then:A,ElseIf:M,Else:x}};const Q=({condition:e,children:n})=>e?i.createElement(i.Fragment,null,n):null,X=({condition:e,children:n})=>e===!1?i.createElement(i.Fragment,null,n):null,ee=({all:e,any:n,none:t,children:l,fallback:r})=>S(()=>(e&&(n||t)&&console.warn('When: Multiple condition types (all, any, none) provided; "all" takes precedence.'),!!(e&&e.length>0&&e.every(Boolean)||n&&n.length>0&&n.some(Boolean)||t&&t.length>0&&t.every(o=>!o))),[e,n,t])?i.createElement(i.Fragment,null,l):i.createElement(i.Fragment,null,r||null),te=({data:e,transform:n,render:t,fallback:l})=>{const r=S(()=>n.reduce((o,s)=>s(o),e),[e,n]);return r==null?i.createElement(i.Fragment,null,l||null):i.createElement(i.Fragment,null,t(r))},ne=e=>{const{children:n,h:t,w:l,size:r,height:o,width:s,className:u}=e;return i.createElement("div",{style:{width:r||l||s,height:r||t||o,flexShrink:0},className:u},n)},re=({let:e,props:n,children:t,fallback:l})=>{const r=S(()=>typeof e=="function"?e(n):e,[e,n]);return!t||!Object.keys(r).length?i.createElement(i.Fragment,null,l||null):i.createElement(i.Fragment,null,t(r))};function C(...e){const n=new Set;for(const t of e)if(t){if(typeof t=="string")n.add(t);else if(Array.isArray(t))t.forEach(l=>n.add(l));else if(typeof t=="object")for(const[l,r]of Object.entries(t))r&&n.add(l)}return Array.from(n).join(" ")}const le=e=>typeof e=="object"&&!!e,P=({className:e,children:n,asWrapper:t=!1})=>{if(!n)return null;if(!e)return i.createElement(v,null,n);const l=typeof e=="string"?e:C(...Object.values(e));if(t)return i.createElement(t===!0?"div":t,{className:l},n);if(Z.count(n)>1)return console.error("<Styles>: children has more than one child. Please check your code."),i.createElement(v,null,n);if(q(n)){const r=n;let o=r?.props?.className;return r?.type?.displayName===P.displayName&&le(o)&&(o=C(...Object.values(o))),U(n,{className:C(l,o)})}return console.error("<Styles>: children is not a valid React element. Please check your code."),i.createElement(v,null,n)};P.displayName="W/Styles";const oe=e=>{const{index:n=0,options:t,next:l,render:r}=e;b(()=>{if(t.length<n+1)throw new Error(`Index ${n} is out of bounds for options array of length ${t.length}. Defaulting to first option.`)},[n,t]);const[o,s]=I(n),u=()=>{s(f=>t.length?l?l(f,t):(f+1)%t.length:f)};return r(t[o],u)},se=({onIntersect:e,threshold:n=.1,root:t=null,rootMargin:l="0px",triggerOnce:r=!1,disabled:o=!1,children:s,className:u,style:f})=>{const a=O(null),c=O(null),d=O(!1);return b(()=>{if(o||!a.current)return;if(!window.IntersectionObserver){console.warn("IntersectionObserver is not supported in this browser");return}const m=a.current,g=y=>{y.forEach($=>{r&&d.current||(e($,c.current),r&&(d.current=!0,c.current?.unobserve(m)))})};return c.current=new IntersectionObserver(g,{root:t,rootMargin:l,threshold:n}),c.current.observe(m),()=>{c.current&&c.current.disconnect()}},[e,n,t,l,r,o]),b(()=>{r||(d.current=!1)},[r]),i.createElement("div",{ref:a,className:u,style:f},s)};function ae(e){const{items:n,renderItem:t,filter:l,renderEmpty:r,sort:o}=e;if(!n)return console.error("ArrayRender: items is null"),null;if(n.length===0)return r?r():null;if(o){let s=[...n];return l&&(s=s.filter(l)),s=s.sort(o),s.length===0?r?r():null:i.createElement(v,null,s.map((u,f)=>t(u,f)))}return i.createElement(v,null,n.map((s,u)=>l&&!l(s)?null:t(s,u)))}function ie({source:e,format:n,children:t}){const l=S(()=>{if(e instanceof Date)return e;if(typeof e=="string"||typeof e=="number"){const o=new Date(e);return isNaN(o.getTime())?null:o}return null},[e]),r=S(()=>l?n?n(l):l.toLocaleString():null,[l,n]);return!r||!t?null:i.createElement(i.Fragment,null,t(r))}const ue="onChange",ce="value";function fe(e){const{defaultValue:n,onBeforeChange:t,trigger:l=ue,valuePropName:r=ce,props:o}=e,s=Object.prototype.hasOwnProperty.call(o,r),[u,f]=I(n),a=s?o[r]:u,c=S(()=>o[l],[o,l]),d=G(m=>{const g=typeof m=="function"?m(a):m;t&&t(g,a)===!1||(s||f(g),c&&c(g))},[s,t,a,c]);return[a,d]}function de(e,...n){try{const t=e(...n);return t instanceof Promise?t:Promise.resolve(t)}catch(t){return Promise.reject(t)}}const J=typeof Promise.try=="function"?Promise.try.bind(Promise):de,me=typeof Promise.withResolvers=="function"?Promise.withResolvers.bind(Promise):()=>{let e,n;return{promise:new Promise((t,l)=>{e=t,n=l}),resolve:e,reject:n}};function W(e,n={}){let t=typeof e=="function"?e():e;const l=[],{sideEffect:r,transform:o}=n,s=()=>{const a=t;return o?.get?o.get(a):a},u=a=>{const c=t,d=o?.get?o.get(c):c;t=o?.set?o.set(typeof a=="function"?a(d):a):typeof a=="function"?a(d):a,l.forEach(m=>m(t)),r&&J(r,t,c).catch(m=>{console.error("Error in external state side effect, Please do it within side effects:",m)})},f=()=>{const[a,c]=i.useState(t);return i.useEffect(()=>(l.push(c),()=>{const d=l.indexOf(c);d>-1&&l.splice(d,1)}),[]),[o?.get?o.get(a):a,u]};return{get:s,set:u,use:f,useGetter:()=>{const[a]=f();return a},__listeners:l}}function he(e,n,t){const{storageType:l="local",sideEffect:r,transform:o}=t??{},s=l==="local"?localStorage:sessionStorage;let u=n;const f=s.getItem(e);if(f)try{u=JSON.parse(f)}catch(a){console.warn(`Failed to parse ${l}Storage value for key "${e}", using initial state:`,a),u=n}return W(u,{sideEffect:a=>{s.setItem(e,JSON.stringify(a)),r?.(a)},transform:o})}function pe(e,n){const t=n||new Date,l=t.getFullYear(),r=t.getMonth()+1,o=t.getDate(),s=t.getHours(),u=t.getMinutes(),f=t.getSeconds(),a=t.getMilliseconds(),c=t.getDay(),d=["Sun","Mon","Tue","Wed","Thu","Fri","Sat"],m=["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"],g=["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"],y=["January","February","March","April","May","June","July","August","September","October","November","December"],$=m[c],Y=d[c],R=r-1,_=y[R],H=g[R],z={YY:l.toString().slice(2),YYYY:l.toString(),M:r.toString(),MM:r.toString().padStart(2,"0"),MMM:H,MMMM:_,D:o.toString(),DD:o.toString().padStart(2,"0"),d:c.toString(),dd:Y,ddd:Y,dddd:$,H:s.toString(),HH:s.toString().padStart(2,"0"),h:(s%12).toString(),hh:(s%12).toString().padStart(2,"0"),m:u.toString(),mm:u.toString().padStart(2,"0"),s:f.toString(),ss:f.toString().padStart(2,"0"),SSS:a.toString().padStart(3,"0"),Z:"+08:00",ZZ:"+0800",A:s<12?"AM":"PM",a:s<12?"am":"pm"};return e.replace(/YYYY|YY|M{1,4}|D{1,2}|d{1,4}|H{1,2}|h{1,2}|m{1,2}|s{1,2}|SSS|Z{1,2}|A|a/g,V=>z[V])}class ge{count=0;next(){return this.count++}}const F=["base","xs","sm","md","lg","xl","2xl","3xl"],L={xs:475,sm:640,md:768,lg:1024,xl:1280,"2xl":1536,"3xl":1920};function h(e,n,t){t&&(e[n]||(e[n]=[]),e[n].push(t))}function ye(e){return e==null?!1:typeof e=="string"?e.trim().length>0:Array.isArray(e)?e.length>0:!0}function Ee(e){return e!=null}function p(e,n){return`${String(e)} ${n}`}const T={email:/^[^\s@]+@[^\s@]+\.[^\s@]+$/,url:/^(https?:\/\/)?([\w.-]+)\.([a-z]{2,6})([/\w .-]*)*\/?$/,phone:/^1[3-9]\d{9}$/};function k(e,n,t,l,r){const o=ye(n),s=Ee(n);if(t.required&&!o){h(r,e,t.message??p(e,"\u4E3A\u5FC5\u586B\u9879"));return}if(!(!s&&!t.required)){if(t.dependsOn){const u=t.dependsOn(l);u===!1?h(r,e,t.message??p(e,"\u4F9D\u8D56\u6761\u4EF6\u672A\u6EE1\u8DB3")):typeof u=="string"&&h(r,e,u)}if(typeof n=="string"){const u=t,{len:f,min:a,max:c,regex:d,email:m,url:g,phone:y}=u;typeof f=="number"&&n.length!==f&&h(r,e,t.message??p(e,`\u957F\u5EA6\u5FC5\u987B\u4E3A ${f}`)),typeof a=="number"&&n.length<a&&h(r,e,t.message??p(e,`\u957F\u5EA6\u4E0D\u80FD\u5C11\u4E8E ${a}`)),typeof c=="number"&&n.length>c&&h(r,e,t.message??p(e,`\u957F\u5EA6\u4E0D\u80FD\u8D85\u8FC7 ${c}`)),d&&!d.test(n)&&h(r,e,t.message??p(e,"\u683C\u5F0F\u4E0D\u6B63\u786E")),m&&!T.email.test(n)&&h(r,e,t.message??p(e,"\u4E0D\u662F\u6709\u6548\u7684\u90AE\u7BB1")),g&&!T.url.test(n)&&h(r,e,t.message??p(e,"\u4E0D\u662F\u6709\u6548\u7684URL")),y&&!T.phone.test(n)&&h(r,e,t.message??p(e,"\u4E0D\u662F\u6709\u6548\u7684\u624B\u673A\u53F7"))}if(typeof n=="number"){const u=t,{min:f,max:a}=u;typeof f=="number"&&n<f&&h(r,e,t.message??p(e,`\u4E0D\u80FD\u5C0F\u4E8E ${f}`)),typeof a=="number"&&n>a&&h(r,e,t.message??p(e,`\u4E0D\u80FD\u5927\u4E8E ${a}`))}if(Array.isArray(n)){const u=t,{len:f,min:a,max:c,unique:d,elementRule:m}=u;typeof f=="number"&&n.length!==f&&h(r,e,t.message??p(e,`\u957F\u5EA6\u5FC5\u987B\u4E3A ${f}`)),typeof a=="number"&&n.length<a&&h(r,e,t.message??p(e,`\u957F\u5EA6\u4E0D\u80FD\u5C0F\u4E8E ${a}`)),typeof c=="number"&&n.length>c&&h(r,e,t.message??p(e,`\u957F\u5EA6\u4E0D\u80FD\u5927\u4E8E ${c}`)),d&&new Set(n).size!==n.length&&h(r,e,t.message??p(e,"\u5143\u7D20\u5FC5\u987B\u552F\u4E00")),m&&n.forEach((g,y)=>{k(`${String(e)}[${y}]`,g,m,l,r)})}if(t.validator){const u=t.validator?.(n,l);u===!1?h(r,e,t.message??p(e,"\u6821\u9A8C\u672A\u901A\u8FC7")):typeof u=="string"&&h(r,e,u)}}}function Se(e,n){const t={};for(const r in n){const o=r,s=n[o];if(!s)continue;const u=e[o];if(Array.isArray(s))for(const f of s)k(o,u,f,e,t);else k(o,u,s,e,t)}const l=Object.values(t).reduce((r,o)=>(o&&r.push(...o),r),[]);return l.length>0?{valid:!1,errors:l,fieldErrors:t}:{valid:!0,data:e}}const we=[...F].reverse();function B(e,n){for(const t of we){const l=e[t];if(l!==void 0&&!Number.isNaN(l)&&n>=l)return t}return"base"}function ve(e=L){const[n,t]=I(B(e,window.innerWidth));return b(()=>{let l=[],r=[];const o=()=>{r.forEach(c=>c()),l=[],r=[];const s=B(e,window.innerWidth);t(s);const u=F.indexOf(s),f=F[u+1];if(f&&e[f]!==void 0){const c=e[f];if(Number.isNaN(c))throw new Error(`Invalid breakpoint value for ${f}: ${e[f]}`);{const d=window.matchMedia(`(min-width: ${c}px)`);l.push(d);const m=()=>o();d.addEventListener("change",m),r.push(()=>d.removeEventListener("change",m))}}const a=F[u-1];if(a&&e[a]!==void 0){const c=e[a];if(Number.isNaN(c))throw new Error(`Invalid breakpoint value for ${a}: ${e[a]}`);{const d=window.matchMedia(`(max-width: ${c-1}px)`);l.push(d);const m=()=>o();d.addEventListener("change",m),r.push(()=>d.removeEventListener("change",m))}}};return o(),()=>{r.forEach(s=>s())}},[e]),n}export{ae as ArrayRender,ge as Counter,ie as DateRender,L as DefBreakpointDesc,X as False,E as If,se as Observer,te as Pipe,re as Scope,ne as SizeBox,P as Styles,w as Switch,oe as Toggle,Q as True,ee as When,F as breakpoints,j as childrenLoop,W as createExternalState,he as createStorageState,C as cx,pe as formatDate,B as getCurrentBreakpoint,Se as ruleChecker,J as safePromiseTry,me as safePromiseWithResolvers,fe as useControlled,ve as useScreen};
|
|
1
|
+
import i,{useMemo as S,Fragment as v,Children as Z,isValidElement as q,cloneElement as G,useEffect as b,useState as I,useRef as O,useCallback as U}from"react";function j(e,n){if(e===void 0)return;let t=0;if(Array.isArray(e)){for(const o of e)if(n(o,t++)===!1)break}else n(e,t)}const K=(e,n)=>e===n,N=e=>i.createElement(i.Fragment,null,e.children);N.displayName="Switch_Case";const D=e=>i.createElement(i.Fragment,null,e.children);D.displayName="Switch_Default";const w=e=>{const{value:n,compare:t=K,children:o,strict:r=!1}=e,l=new Set;let a=null,c=null,u=!1;return j(o,(s,f)=>{if(!i.isValidElement(s))throw new Error(`Switch Children only accepts valid React elements at index ${f}`);const d=s.type;if(d.displayName===N.displayName){const m=s.props;if(l.has(m.value))throw new Error(`Switch found duplicate Case value at index ${f}: ${JSON.stringify(m.value)}${r?" (detected in strict mode)":""}`);if(l.add(m.value),!a&&t(n,m.value)&&(a=m.children,r===!1))return!1}else if(d.displayName===D.displayName){if(u)throw new Error(`Switch can only have one Default child at index ${f}`);if(u=!0,c=s.props.children,!r&&a)return!1}else throw new Error(`Switch Children only accepts 'Case' or 'Default' elements, found: ${String(d.displayName||d.name||d)} at index ${f}`)}),i.createElement(i.Fragment,null,a??c)};w.displayName="Switch",w.Case=N,w.Default=D,w.createTyped=function(){return{Switch:w,Case:N,Default:D}};const A=e=>i.createElement(i.Fragment,null,e.children),x=({children:e})=>i.createElement(i.Fragment,null,e),M=e=>i.createElement(i.Fragment,null,e.children);A.displayName="If_Then",x.displayName="If_Else",M.displayName="If_ElseIf";const E=({condition:e,children:n})=>{let t=null,o=null;const r=[];if(i.Children.forEach(n,l=>{if(!i.isValidElement(l))throw new Error("If component only accepts valid React elements");const a=l.type;if(a.displayName===A.displayName){if(t)throw new Error("If component can only have one Then child");t=l}else if(a.displayName===M.displayName)r.push(l);else if(a.displayName===x.displayName){if(o)throw new Error("If component can only have one Else child");o=l}else throw new Error(`If component only accepts 'Then', 'ElseIf', or 'Else' elements as children, found: ${String(a.displayName||a.name||a)}`)}),e)return t?i.createElement(i.Fragment,null,t.props.children):null;for(const l of r)if(l.props.condition)return i.createElement(i.Fragment,null,l.props.children);return o?i.createElement(i.Fragment,null,o.props.children):null};E.displayName="If",E.Then=A,E.ElseIf=M,E.Else=x,E.createTyped=function(){return{If:E,Then:A,ElseIf:M,Else:x}};const Q=({condition:e,children:n})=>e?i.createElement(i.Fragment,null,n):null,X=({condition:e,children:n})=>e===!1?i.createElement(i.Fragment,null,n):null,ee=({all:e,any:n,none:t,children:o,fallback:r})=>S(()=>(e&&(n||t)&&console.warn('When: Multiple condition types (all, any, none) provided; "all" takes precedence.'),!!(e&&e.length>0&&e.every(Boolean)||n&&n.length>0&&n.some(Boolean)||t&&t.length>0&&t.every(l=>!l))),[e,n,t])?i.createElement(i.Fragment,null,o):i.createElement(i.Fragment,null,r||null),te=({data:e,transform:n,render:t,fallback:o})=>{const r=S(()=>n.reduce((l,a)=>a(l),e),[e,n]);return r==null?i.createElement(i.Fragment,null,o||null):i.createElement(i.Fragment,null,t(r))},ne=e=>{const{children:n,h:t,w:o,size:r,height:l,width:a,className:c}=e;return i.createElement("div",{style:{width:r||o||a,height:r||t||l,flexShrink:0},className:c},n)},re=({let:e,props:n,children:t,fallback:o})=>{const r=S(()=>typeof e=="function"?e(n):e,[e,n]);return!t||!Object.keys(r).length?i.createElement(i.Fragment,null,o||null):i.createElement(i.Fragment,null,t(r))};function C(...e){const n=new Set;for(const t of e)if(t){if(typeof t=="string")n.add(t);else if(Array.isArray(t))t.forEach(o=>n.add(o));else if(typeof t=="object")for(const[o,r]of Object.entries(t))r&&n.add(o)}return Array.from(n).join(" ")}const oe=e=>typeof e=="object"&&!!e,P=({className:e,children:n,asWrapper:t=!1})=>{if(!n)return null;if(!e)return i.createElement(v,null,n);const o=typeof e=="string"?e:C(...Object.values(e));if(t)return i.createElement(t===!0?"div":t,{className:o},n);if(Z.count(n)>1)return console.error("<Styles>: children has more than one child. Please check your code."),i.createElement(v,null,n);if(q(n)){const r=n;let l=r?.props?.className;return r?.type?.displayName===P.displayName&&oe(l)&&(l=C(...Object.values(l))),G(n,{className:C(o,l)})}return console.error("<Styles>: children is not a valid React element. Please check your code."),i.createElement(v,null,n)};P.displayName="W/Styles";const le=e=>{const{index:n=0,options:t,next:o,render:r}=e;b(()=>{if(t.length<n+1)throw new Error(`Index ${n} is out of bounds for options array of length ${t.length}. Defaulting to first option.`)},[n,t]);const[l,a]=I(n),c=()=>{a(u=>t.length?o?o(u,t):(u+1)%t.length:u)};return r(t[l],c)},se=({onIntersect:e,threshold:n=.1,root:t=null,rootMargin:o="0px",triggerOnce:r=!1,disabled:l=!1,children:a,className:c,style:u})=>{const s=O(null),f=O(null),d=O(!1);return b(()=>{if(l||!s.current)return;if(!window.IntersectionObserver){console.warn("IntersectionObserver is not supported in this browser");return}const m=s.current,g=y=>{y.forEach($=>{r&&d.current||(e($,f.current),r&&(d.current=!0,f.current?.unobserve(m)))})};return f.current=new IntersectionObserver(g,{root:t,rootMargin:o,threshold:n}),f.current.observe(m),()=>{f.current&&f.current.disconnect()}},[e,n,t,o,r,l]),b(()=>{r||(d.current=!1)},[r]),i.createElement("div",{ref:s,className:c,style:u},a)};function ae(e){const{items:n,renderItem:t,filter:o,renderEmpty:r,sort:l}=e;if(!n)return console.error("ArrayRender: items is null"),null;if(n.length===0)return r?r():null;if(l){let u=[...n];return o&&(u=u.filter(o)),u=u.sort(l),u.length===0?r?r():null:i.createElement(v,null,u.map((s,f)=>t(s,f)))}let a=0;const c=n.map((u,s)=>o&&!o(u)?(a++,null):t(u,s));return i.createElement(v,null,a===n.length?r?r():null:c)}function ie({source:e,format:n,children:t}){const o=S(()=>{if(e instanceof Date)return e;if(typeof e=="string"||typeof e=="number"){const l=new Date(e);return isNaN(l.getTime())?null:l}return null},[e]),r=S(()=>o?n?n(o):o.toLocaleString():null,[o,n]);return!r||!t?null:i.createElement(i.Fragment,null,t(r))}const ue="onChange",ce="value";function fe(e){const{defaultValue:n,onBeforeChange:t,trigger:o=ue,valuePropName:r=ce,props:l}=e,a=Object.prototype.hasOwnProperty.call(l,r),[c,u]=I(n),s=a?l[r]:c,f=S(()=>l[o],[l,o]),d=U(m=>{const g=typeof m=="function"?m(s):m;t&&t(g,s)===!1||(a||u(g),f&&f(g))},[a,t,s,f]);return[s,d]}function de(e,...n){try{const t=e(...n);return t instanceof Promise?t:Promise.resolve(t)}catch(t){return Promise.reject(t)}}const J=typeof Promise.try=="function"?Promise.try.bind(Promise):de,me=typeof Promise.withResolvers=="function"?Promise.withResolvers.bind(Promise):()=>{let e,n;return{promise:new Promise((t,o)=>{e=t,n=o}),resolve:e,reject:n}};function W(e,n={}){let t=typeof e=="function"?e():e;const o=[],{sideEffect:r,transform:l}=n,a=()=>{const s=t;return l?.get?l.get(s):s},c=s=>{const f=t,d=l?.get?l.get(f):f;t=l?.set?l.set(typeof s=="function"?s(d):s):typeof s=="function"?s(d):s,o.forEach(m=>m(t)),r&&J(r,t,f).catch(m=>{console.error("Error in external state side effect, Please do it within side effects:",m)})},u=()=>{const[s,f]=i.useState(t);return i.useEffect(()=>(o.push(f),()=>{const d=o.indexOf(f);d>-1&&o.splice(d,1)}),[]),[l?.get?l.get(s):s,c]};return{get:a,set:c,use:u,useGetter:()=>{const[s]=u();return s},__listeners:o}}function he(e,n,t){const{storageType:o="local",sideEffect:r,transform:l}=t??{};let a=n;if(typeof window<"u"){const c=(o==="local"?localStorage:sessionStorage).getItem(e);if(c)try{a=JSON.parse(c)}catch(u){console.warn(`Failed to parse ${o}Storage value for key "${e}", using initial state:`,u),a=n}}return W(a,{sideEffect:c=>{typeof window<"u"&&(o==="local"?localStorage:sessionStorage).setItem(e,JSON.stringify(c)),r?.(c)},transform:l})}function pe(e,n){const t=n||new Date,o=t.getFullYear(),r=t.getMonth()+1,l=t.getDate(),a=t.getHours(),c=t.getMinutes(),u=t.getSeconds(),s=t.getMilliseconds(),f=t.getDay(),d=["Sun","Mon","Tue","Wed","Thu","Fri","Sat"],m=["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"],g=["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"],y=["January","February","March","April","May","June","July","August","September","October","November","December"],$=m[f],Y=d[f],R=r-1,_=y[R],H=g[R],z={YY:o.toString().slice(2),YYYY:o.toString(),M:r.toString(),MM:r.toString().padStart(2,"0"),MMM:H,MMMM:_,D:l.toString(),DD:l.toString().padStart(2,"0"),d:f.toString(),dd:Y,ddd:Y,dddd:$,H:a.toString(),HH:a.toString().padStart(2,"0"),h:(a%12).toString(),hh:(a%12).toString().padStart(2,"0"),m:c.toString(),mm:c.toString().padStart(2,"0"),s:u.toString(),ss:u.toString().padStart(2,"0"),SSS:s.toString().padStart(3,"0"),Z:"+08:00",ZZ:"+0800",A:a<12?"AM":"PM",a:a<12?"am":"pm"};return e.replace(/YYYY|YY|M{1,4}|D{1,2}|d{1,4}|H{1,2}|h{1,2}|m{1,2}|s{1,2}|SSS|Z{1,2}|A|a/g,V=>z[V])}class ge{count=0;next(){return this.count++}}const F=["base","xs","sm","md","lg","xl","2xl","3xl"],L={xs:475,sm:640,md:768,lg:1024,xl:1280,"2xl":1536,"3xl":1920};function h(e,n,t){t&&(e[n]||(e[n]=[]),e[n].push(t))}function ye(e){return e==null?!1:typeof e=="string"?e.trim().length>0:Array.isArray(e)?e.length>0:!0}function Ee(e){return e!=null}function p(e,n){return`${String(e)} ${n}`}const T={email:/^[^\s@]+@[^\s@]+\.[^\s@]+$/,url:/^(https?:\/\/)?([\w.-]+)\.([a-z]{2,6})([/\w .-]*)*\/?$/,phone:/^1[3-9]\d{9}$/};function k(e,n,t,o,r){const l=ye(n),a=Ee(n);if(t.required&&!l){h(r,e,t.message??p(e,"\u4E3A\u5FC5\u586B\u9879"));return}if(!(!a&&!t.required)){if(t.dependsOn){const c=t.dependsOn(o);c===!1?h(r,e,t.message??p(e,"\u4F9D\u8D56\u6761\u4EF6\u672A\u6EE1\u8DB3")):typeof c=="string"&&h(r,e,c)}if(typeof n=="string"){const c=t,{len:u,min:s,max:f,regex:d,email:m,url:g,phone:y}=c;typeof u=="number"&&n.length!==u&&h(r,e,t.message??p(e,`\u957F\u5EA6\u5FC5\u987B\u4E3A ${u}`)),typeof s=="number"&&n.length<s&&h(r,e,t.message??p(e,`\u957F\u5EA6\u4E0D\u80FD\u5C11\u4E8E ${s}`)),typeof f=="number"&&n.length>f&&h(r,e,t.message??p(e,`\u957F\u5EA6\u4E0D\u80FD\u8D85\u8FC7 ${f}`)),d&&!d.test(n)&&h(r,e,t.message??p(e,"\u683C\u5F0F\u4E0D\u6B63\u786E")),m&&!T.email.test(n)&&h(r,e,t.message??p(e,"\u4E0D\u662F\u6709\u6548\u7684\u90AE\u7BB1")),g&&!T.url.test(n)&&h(r,e,t.message??p(e,"\u4E0D\u662F\u6709\u6548\u7684URL")),y&&!T.phone.test(n)&&h(r,e,t.message??p(e,"\u4E0D\u662F\u6709\u6548\u7684\u624B\u673A\u53F7"))}if(typeof n=="number"){const c=t,{min:u,max:s}=c;typeof u=="number"&&n<u&&h(r,e,t.message??p(e,`\u4E0D\u80FD\u5C0F\u4E8E ${u}`)),typeof s=="number"&&n>s&&h(r,e,t.message??p(e,`\u4E0D\u80FD\u5927\u4E8E ${s}`))}if(Array.isArray(n)){const c=t,{len:u,min:s,max:f,unique:d,elementRule:m}=c;typeof u=="number"&&n.length!==u&&h(r,e,t.message??p(e,`\u957F\u5EA6\u5FC5\u987B\u4E3A ${u}`)),typeof s=="number"&&n.length<s&&h(r,e,t.message??p(e,`\u957F\u5EA6\u4E0D\u80FD\u5C0F\u4E8E ${s}`)),typeof f=="number"&&n.length>f&&h(r,e,t.message??p(e,`\u957F\u5EA6\u4E0D\u80FD\u5927\u4E8E ${f}`)),d&&new Set(n).size!==n.length&&h(r,e,t.message??p(e,"\u5143\u7D20\u5FC5\u987B\u552F\u4E00")),m&&n.forEach((g,y)=>{k(`${String(e)}[${y}]`,g,m,o,r)})}if(t.validator){const c=t.validator?.(n,o);c===!1?h(r,e,t.message??p(e,"\u6821\u9A8C\u672A\u901A\u8FC7")):typeof c=="string"&&h(r,e,c)}}}function Se(e,n){const t={};for(const r in n){const l=r,a=n[l];if(!a)continue;const c=e[l];if(Array.isArray(a))for(const u of a)k(l,c,u,e,t);else k(l,c,a,e,t)}const o=Object.values(t).reduce((r,l)=>(l&&r.push(...l),r),[]);return o.length>0?{valid:!1,errors:o,fieldErrors:t}:{valid:!0,data:e}}const we=[...F].reverse();function B(e,n){for(const t of we){const o=e[t];if(o!==void 0&&!Number.isNaN(o)&&n>=o)return t}return"base"}function ve(e=L){const[n,t]=I(B(e,window.innerWidth));return b(()=>{let o=[],r=[];const l=()=>{r.forEach(f=>f()),o=[],r=[];const a=B(e,window.innerWidth);t(a);const c=F.indexOf(a),u=F[c+1];if(u&&e[u]!==void 0){const f=e[u];if(Number.isNaN(f))throw new Error(`Invalid breakpoint value for ${u}: ${e[u]}`);{const d=window.matchMedia(`(min-width: ${f}px)`);o.push(d);const m=()=>l();d.addEventListener("change",m),r.push(()=>d.removeEventListener("change",m))}}const s=F[c-1];if(s&&e[s]!==void 0){const f=e[s];if(Number.isNaN(f))throw new Error(`Invalid breakpoint value for ${s}: ${e[s]}`);{const d=window.matchMedia(`(max-width: ${f-1}px)`);o.push(d);const m=()=>l();d.addEventListener("change",m),r.push(()=>d.removeEventListener("change",m))}}};return l(),()=>{r.forEach(a=>a())}},[e]),n}export{ae as ArrayRender,ge as Counter,ie as DateRender,L as DefBreakpointDesc,X as False,E as If,se as Observer,te as Pipe,re as Scope,ne as SizeBox,P as Styles,w as Switch,le as Toggle,Q as True,ee as When,F as breakpoints,j as childrenLoop,W as createExternalState,he as createStorageState,C as cx,pe as formatDate,B as getCurrentBreakpoint,Se as ruleChecker,J as safePromiseTry,me as safePromiseWithResolvers,fe as useControlled,ve as useScreen};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wwog/react",
|
|
3
|
-
"version": "1.3.
|
|
3
|
+
"version": "1.3.12",
|
|
4
4
|
"description": "A practical React component library providing declarative flow control and common UI utility components to make your React code more concise and readable.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"react",
|
|
@@ -20,6 +20,15 @@
|
|
|
20
20
|
],
|
|
21
21
|
"homepage": "https://github.com/wwog/react",
|
|
22
22
|
"license": "MIT",
|
|
23
|
+
"scripts": {
|
|
24
|
+
"build": "unbuild",
|
|
25
|
+
"format": "biome format --write src",
|
|
26
|
+
"check": "biome check --write src",
|
|
27
|
+
"test:unit": "vitest run --browser.headless",
|
|
28
|
+
"test:types": "tsc --noEmit --skipLibCheck",
|
|
29
|
+
"all-suites": "pnpm run format && pnpm run check && pnpm run test:types && pnpm run test:unit",
|
|
30
|
+
"test:watch": "vitest"
|
|
31
|
+
},
|
|
23
32
|
"devDependencies": {
|
|
24
33
|
"@biomejs/biome": "^1.9.4",
|
|
25
34
|
"@types/react": "^19.1.2",
|
|
@@ -41,13 +50,5 @@
|
|
|
41
50
|
"node": ">= 20.0.0",
|
|
42
51
|
"pnpm": ">=8.15.0"
|
|
43
52
|
},
|
|
44
|
-
"
|
|
45
|
-
|
|
46
|
-
"format": "biome format --write src",
|
|
47
|
-
"check": "biome check --write src",
|
|
48
|
-
"test:unit": "vitest run --browser.headless",
|
|
49
|
-
"test:types": "tsc --noEmit --skipLibCheck",
|
|
50
|
-
"all-suites": "pnpm run format && pnpm run check && pnpm run test:types && pnpm run test:unit",
|
|
51
|
-
"test:watch": "vitest"
|
|
52
|
-
}
|
|
53
|
-
}
|
|
53
|
+
"packageManager": "pnpm@10.11.0+sha512.6540583f41cc5f628eb3d9773ecee802f4f9ef9923cc45b69890fb47991d4b092964694ec3a4f738a420c918a333062c8b925d312f42e4f0c263eb603551f977"
|
|
54
|
+
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import React, { Fragment, type ReactNode } from "react";
|
|
1
|
+
import React, { Fragment, useRef, type ReactNode } from "react";
|
|
2
2
|
|
|
3
3
|
//#region component Types
|
|
4
4
|
export interface ArrayRenderProps<T> {
|
|
@@ -26,11 +26,11 @@ export function ArrayRender<T>(props: ArrayRenderProps<T>): ReactNode {
|
|
|
26
26
|
// 如果需要排序,先处理排序和过滤
|
|
27
27
|
if (sort) {
|
|
28
28
|
let processedItems = [...items];
|
|
29
|
-
|
|
29
|
+
|
|
30
30
|
if (filter) {
|
|
31
31
|
processedItems = processedItems.filter(filter);
|
|
32
32
|
}
|
|
33
|
-
|
|
33
|
+
|
|
34
34
|
processedItems = processedItems.sort(sort);
|
|
35
35
|
|
|
36
36
|
if (processedItems.length === 0) {
|
|
@@ -46,15 +46,20 @@ export function ArrayRender<T>(props: ArrayRenderProps<T>): ReactNode {
|
|
|
46
46
|
);
|
|
47
47
|
}
|
|
48
48
|
|
|
49
|
+
let nullCount = 0;
|
|
50
|
+
|
|
51
|
+
const renderItems = items.map((item, index) => {
|
|
52
|
+
if (filter && !filter(item)) {
|
|
53
|
+
nullCount++;
|
|
54
|
+
return null;
|
|
55
|
+
}
|
|
56
|
+
return renderItem(item, index);
|
|
57
|
+
})
|
|
58
|
+
|
|
49
59
|
// 如果不需要排序,保持原来的循环中过滤方式
|
|
50
60
|
return (
|
|
51
61
|
<Fragment>
|
|
52
|
-
{items.
|
|
53
|
-
if (filter && !filter(item)) {
|
|
54
|
-
return null;
|
|
55
|
-
}
|
|
56
|
-
return renderItem(item, index);
|
|
57
|
-
})}
|
|
62
|
+
{nullCount === items.length ? renderEmpty ? renderEmpty() : null : renderItems}
|
|
58
63
|
</Fragment>
|
|
59
64
|
);
|
|
60
65
|
}
|
|
@@ -213,23 +213,32 @@ export function createStorageState<T, U = T>(
|
|
|
213
213
|
options?: StorageStateOptions<T, U>
|
|
214
214
|
) {
|
|
215
215
|
const { storageType = "local", sideEffect, transform } = options ?? {};
|
|
216
|
-
const storage = storageType === "local" ? localStorage : sessionStorage;
|
|
217
216
|
let _initState: T = initialState;
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
)
|
|
227
|
-
|
|
217
|
+
|
|
218
|
+
// 只在客户端环境中读取存储
|
|
219
|
+
if (typeof window !== 'undefined') {
|
|
220
|
+
const storage = storageType === "local" ? localStorage : sessionStorage;
|
|
221
|
+
const storedValue = storage.getItem(key);
|
|
222
|
+
if (storedValue) {
|
|
223
|
+
try {
|
|
224
|
+
_initState = JSON.parse(storedValue);
|
|
225
|
+
} catch (error) {
|
|
226
|
+
console.warn(
|
|
227
|
+
`Failed to parse ${storageType}Storage value for key "${key}", using initial state:`,
|
|
228
|
+
error
|
|
229
|
+
);
|
|
230
|
+
_initState = initialState;
|
|
231
|
+
}
|
|
228
232
|
}
|
|
229
233
|
}
|
|
234
|
+
|
|
230
235
|
return createExternalState(_initState, {
|
|
231
236
|
sideEffect: (newState) => {
|
|
232
|
-
|
|
237
|
+
// 只在客户端环境中写入存储
|
|
238
|
+
if (typeof window !== 'undefined') {
|
|
239
|
+
const storage = storageType === "local" ? localStorage : sessionStorage;
|
|
240
|
+
storage.setItem(key, JSON.stringify(newState));
|
|
241
|
+
}
|
|
233
242
|
sideEffect?.(newState);
|
|
234
243
|
},
|
|
235
244
|
transform,
|