@wwog/react 1.2.18 → 1.2.20
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 +1 -1
- package/src/components/Sundry/Clamp.tsx +31 -18
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import r,{useMemo as
|
|
1
|
+
import r,{useMemo as y,Children as B,Fragment as w,isValidElement as W,cloneElement as J,useEffect as R,useState as T,useRef as _,useLayoutEffect as L,useCallback as Z}from"react";function P(t,n){if(t===void 0)return;let e=0;if(Array.isArray(t)){for(const l of t)if(n(l,e++)===!1)break}else n(t,e)}const z=(t,n)=>t===n,v=t=>r.createElement(r.Fragment,null,t.children);v.displayName="Switch_Case";const N=t=>r.createElement(r.Fragment,null,t.children);N.displayName="Switch_Default";const S=t=>{const{value:n,compare:e=z,children:l,strict:o=!1}=t,a=new Set;let i=null,c=null,u=!1;return P(l,(s,m)=>{if(!r.isValidElement(s))throw new Error(`Switch Children only accepts valid React elements at index ${m}`);const d=s.type;if(d.displayName===v.displayName){const f=s.props;if(a.has(f.value))throw new Error(`Switch found duplicate Case value at index ${m}: ${JSON.stringify(f.value)}${o?" (detected in strict mode)":""}`);if(a.add(f.value),!i&&e(n,f.value)&&(i=f.children,o===!1))return!1}else if(d.displayName===N.displayName){if(u)throw new Error(`Switch can only have one Default child at index ${m}`);if(u=!0,c=s.props.children,!o&&i)return!1}else throw new Error(`Switch Children only accepts 'Case' or 'Default' elements, found: ${String(d.displayName||d.name||d)} at index ${m}`)}),r.createElement(r.Fragment,null,i??c)};S.displayName="Switch",S.Case=v,S.Default=N,S.createTyped=function(){return{Switch:S,Case:v,Default:N}};const b=t=>r.createElement(r.Fragment,null,t.children),M=({children:t})=>r.createElement(r.Fragment,null,t),F=t=>r.createElement(r.Fragment,null,t.children);b.displayName="If_Then",M.displayName="If_Else",F.displayName="If_ElseIf";const g=({condition:t,children:n})=>{let e=null,l=null;const o=[];if(r.Children.forEach(n,a=>{if(!r.isValidElement(a))throw new Error("If component only accepts valid React elements");const i=a.type;if(i.displayName===b.displayName){if(e)throw new Error("If component can only have one Then child");e=a}else if(i.displayName===F.displayName)o.push(a);else if(i.displayName===M.displayName){if(l)throw new Error("If component can only have one Else child");l=a}else throw new Error(`If component only accepts 'Then', 'ElseIf', or 'Else' elements as children, found: ${String(i.displayName||i.name||i)}`)}),t)return e?r.createElement(r.Fragment,null,e.props.children):null;for(const a of o)if(a.props.condition)return r.createElement(r.Fragment,null,a.props.children);return l?r.createElement(r.Fragment,null,l.props.children):null};g.displayName="If",g.Then=b,g.ElseIf=F,g.Else=M,g.createTyped=function(){return{If:g,Then:b,ElseIf:F,Else:M}};const I=({condition:t,children:n})=>t?r.createElement(r.Fragment,null,n):null,V=({condition:t,children:n})=>t===!1?r.createElement(r.Fragment,null,n):null,G=({all:t,any:n,none:e,children:l,fallback:o})=>y(()=>(t&&(n||e)&&console.warn('When: Multiple condition types (all, any, none) provided; "all" takes precedence.'),!!(t&&t.length>0&&t.every(Boolean)||n&&n.length>0&&n.some(Boolean)||e&&e.length>0&&e.every(a=>!a))),[t,n,e])?r.createElement(r.Fragment,null,l):r.createElement(r.Fragment,null,o||null),q=({data:t,transform:n,render:e,fallback:l})=>{const o=y(()=>n.reduce((a,i)=>i(a),t),[t,n]);return o==null?r.createElement(r.Fragment,null,l||null):r.createElement(r.Fragment,null,e(o))},K=t=>{const{children:n,h:e,w:l,size:o,height:a,width:i,className:c}=t;return r.createElement("div",{style:{width:o||l||i,height:o||e||a,flexShrink:0},className:c},n)},Q=({let:t,props:n,children:e,fallback:l})=>{const o=y(()=>typeof t=="function"?t(n):t,[t,n]);return!e||!Object.keys(o).length?r.createElement(r.Fragment,null,l||null):r.createElement(r.Fragment,null,e(o))};function x(...t){const n=new Set;for(const e of t)if(e){if(typeof e=="string")n.add(e);else if(Array.isArray(e))e.forEach(l=>n.add(l));else if(typeof e=="object")for(const[l,o]of Object.entries(e))o&&n.add(l)}return Array.from(n).join(" ")}const U=t=>typeof t=="object"&&!!t,A=({className:t,children:n,asWrapper:e=!1})=>{if(!n)return null;if(B.count(n)>1)return console.error("<Styles>: children has more than one child. Please check your code."),r.createElement(w,null,n);if(!t)return r.createElement(w,null,n);const l=typeof t=="string"?t:x(...Object.values(t));if(e)return r.createElement(e===!0?"div":e,{className:l},n);if(W(n)){const o=n;let a=o?.props?.className;return o?.type?.displayName===A.displayName&&U(a)&&(a=x(...Object.values(a))),J(n,{className:x(l,a)})}return console.error("<Styles>: children is not a valid React element. Please check your code."),r.createElement(w,null,n)};A.displayName="W/Styles";const X=t=>{const{index:n=0,options:e,next:l,render:o}=t;R(()=>{if(e.length<n+1)throw new Error(`Index ${n} is out of bounds for options array of length ${e.length}. Defaulting to first option.`)},[n,e]);const[a,i]=T(n),c=()=>{i(u=>e.length?l?l(u,e):(u+1)%e.length:u)};return o(e[a],c)},$=t=>{const{maxLine:n=1,text:e,extraHeight:l=22,extraContent:o,wrapperStyle:a}=t,i=_(null),[c,u]=T(!1),s=y(()=>!(e==null||e===""),[e]),m=()=>{if(!i.current)return;const d=document.createElement("div");d.textContent=e;const f=getComputedStyle(i.current);if(d.style.width=f.width,d.style.fontSize=f.fontSize,d.style.lineHeight=f.lineHeight,d.style.wordBreak=f.wordBreak,d.style.visibility="hidden",a){const E=Object.keys(a).map(p=>p.replace(/[A-Z]/g,k=>`-${k.toLowerCase()}`));for(const p of E)d.style[p]=f[p]}document.body.appendChild(d);const h=parseInt(getComputedStyle(d).lineHeight)||20,C=d.offsetHeight,D=Math.round(C/h);document.body.removeChild(d),u(D>n)};return L(()=>{s&&m()},[n,e,s]),r.createElement(I,{condition:s},r.createElement("div",{ref:i,style:{overflow:"hidden",width:"100%",display:"flex",...a}},r.createElement("div",{style:{display:"-webkit-box",WebkitBoxOrient:"vertical",WebkitLineClamp:n,overflow:"hidden",wordBreak:"break-all"}},r.createElement(I,{condition:c},r.createElement("div",{style:{float:"right",height:"100%",marginBottom:-l}}),r.createElement("div",{style:{float:"right",clear:"both",height:l}},o)),e)))};function ee(t){const{items:n,renderItem:e,filter:l}=t;return n?r.createElement(w,null,n.map((o,a)=>l&&!l(o)?null:e(o,a))):(console.error("ArrayRender: items is null"),null)}function te({source:t,format:n,children:e}){const l=y(()=>{if(t instanceof Date)return t;if(typeof t=="string"||typeof t=="number"){const a=new Date(t);return isNaN(a.getTime())?null:a}return null},[t]),o=y(()=>l?n?n(l):l.toLocaleString():null,[l,n]);return!o||!e?null:r.createElement(r.Fragment,null,e(o))}const ne="onChange",re="value";function le(t){const{defaultValue:n,onBeforeChange:e,trigger:l=ne,valuePropName:o=re,props:a}=t,i=Object.prototype.hasOwnProperty.call(a,o),[c,u]=T(n),s=i?a[o]:c,m=y(()=>a[l],[a,l]),d=Z(f=>{const h=typeof f=="function"?f(s):f;e&&e(h,s)===!1||(i||u(h),m&&m(h))},[i,e,s,m]);return[s,d]}function ae(t,...n){try{const e=t(...n);return e instanceof Promise?e:Promise.resolve(e)}catch(e){return Promise.reject(e)}}const Y=typeof Promise.try=="function"?Promise.try.bind(Promise):ae;function oe(t,n){let e=typeof t=="function"?t():t;const l=[],o=()=>e,a=c=>{const u=e;e=typeof c=="function"?c():c,l.forEach(s=>s(e)),n&&Y(n,e,u).catch(s=>{console.error("Error in external state side effect, Please do it within side effects:",s)})},i=()=>{const[c,u]=r.useState(e);return r.useEffect(()=>(l.push(u),()=>{const s=l.indexOf(u);s>-1&&l.splice(s,1)}),[]),[c,a]};return{get:o,set:a,use:i,useGetter:()=>{const[c]=i();return c},__listeners:l}}function ie(t,n){const e=n||new Date,l=e.getFullYear(),o=e.getMonth()+1,a=e.getDate(),i=e.getHours(),c=e.getMinutes(),u=e.getSeconds(),s=e.getMilliseconds(),m=e.getDay(),d=["Sun","Mon","Tue","Wed","Thu","Fri","Sat"],f=["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"],h=["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"],C=["January","February","March","April","May","June","July","August","September","October","November","December"],D=f[m],E=d[m],p=o-1,k=C[p],O=h[p],j={YY:l.toString().slice(2),YYYY:l.toString(),M:o.toString(),MM:o.toString().padStart(2,"0"),MMM:O,MMMM:k,D:a.toString(),DD:a.toString().padStart(2,"0"),d:m.toString(),dd:E,ddd:E,dddd:D,H:i.toString(),HH:i.toString().padStart(2,"0"),h:(i%12).toString(),hh:(i%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:i<12?"AM":"PM",a:i<12?"am":"pm"};return t.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,H=>j[H])}class se{count=0;next(){return this.count++}}export{ee as ArrayRender,$ as Clamp,se as Counter,te as DateRender,V as False,g as If,q as Pipe,Q as Scope,K as SizeBox,A as Styles,S as Switch,X as Toggle,I as True,G as When,P as childrenLoop,oe as createExternalState,x as cx,ie as formatDate,Y as safePromiseTry,le as useControlled};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wwog/react",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.20",
|
|
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",
|
|
@@ -44,37 +44,50 @@ export const Clamp: FC<ClampProps> = (props) => {
|
|
|
44
44
|
return true;
|
|
45
45
|
}, [text]);
|
|
46
46
|
|
|
47
|
-
|
|
47
|
+
const checkTextOverflow = () => {
|
|
48
48
|
if (!wrapperRef.current) {
|
|
49
49
|
return;
|
|
50
50
|
}
|
|
51
51
|
const measureWrapper = document.createElement("div");
|
|
52
52
|
measureWrapper.textContent = text;
|
|
53
|
-
|
|
54
|
-
measureWrapper.style.width =
|
|
55
|
-
measureWrapper.style.
|
|
56
|
-
measureWrapper.style.
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
53
|
+
const _wrapperStyle = getComputedStyle(wrapperRef.current);
|
|
54
|
+
measureWrapper.style.width = _wrapperStyle.width;
|
|
55
|
+
measureWrapper.style.fontSize = _wrapperStyle.fontSize;
|
|
56
|
+
measureWrapper.style.lineHeight = _wrapperStyle.lineHeight;
|
|
57
|
+
measureWrapper.style.wordBreak = _wrapperStyle.wordBreak;
|
|
58
|
+
measureWrapper.style.visibility = "hidden";
|
|
59
|
+
//由于允许修改样式,会出现很多问题,很多问题影响了测量元素与实际渲染不一致。
|
|
60
|
+
if (wrapperStyle) {
|
|
61
|
+
const wrapperStyleKey = Object.keys(wrapperStyle).map((key) => {
|
|
62
|
+
return key.replace(/[A-Z]/g, (match) => `-${match.toLowerCase()}`);
|
|
63
|
+
});
|
|
64
|
+
for (const key of wrapperStyleKey) {
|
|
65
|
+
measureWrapper.style[key as any] = _wrapperStyle[key as any];
|
|
66
|
+
}
|
|
67
67
|
}
|
|
68
|
+
document.body.appendChild(measureWrapper);
|
|
69
|
+
|
|
70
|
+
const lineHeight =
|
|
71
|
+
parseInt(getComputedStyle(measureWrapper).lineHeight) || 20;
|
|
72
|
+
const height = measureWrapper.offsetHeight;
|
|
73
|
+
const lines = Math.round(height / lineHeight);
|
|
74
|
+
|
|
75
|
+
document.body.removeChild(measureWrapper);
|
|
68
76
|
|
|
69
|
-
|
|
70
|
-
}
|
|
77
|
+
setShowExtra(lines > maxLine);
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
useLayoutEffect(() => {
|
|
81
|
+
if (isValidText) {
|
|
82
|
+
checkTextOverflow();
|
|
83
|
+
}
|
|
84
|
+
}, [maxLine, text, isValidText]);
|
|
71
85
|
return (
|
|
72
86
|
<True condition={isValidText}>
|
|
73
87
|
<div
|
|
74
88
|
ref={wrapperRef}
|
|
75
89
|
style={{
|
|
76
90
|
overflow: "hidden",
|
|
77
|
-
position: "relative",
|
|
78
91
|
width: "100%",
|
|
79
92
|
display: "flex",
|
|
80
93
|
...wrapperStyle,
|