amis-playground
Version:
amis-playground 是一个amis在线代码编辑器可实时预览运行效果
2 lines (1 loc) • 12.2 kB
JavaScript
import{a as J,R as m,b1 as je,bS as Be,a1 as Pe,ap as Ue,ad as Q,bK as Me,bq as X,e as F,b3 as G,j as D,k as j,D as R,bT as qe,W as B,am as ze,i as We,r as $e,bU as Ke,by as x,Q as Qe,S as Xe,U as Ge,f as g,p as _,q as s,aT as Je,b8 as Ye,bV as Ze,X as et,w as tt}from"./index-7c288cb3.js";var it=function(I){J(a,I);function a(e){var t=this,i;return t=I.call(this,e)||this,t.rootRef=m.createRef(),t.state={keyword:"",filteredOptions:(i=t.props.options)!==null&&i!==void 0?i:[],allowSearch:!1,virtualListHeight:0,othersHeight:0},t.handleSearch=je(t.handleSearch.bind(t),250,{trailing:!0,leading:!1}),t}return a.prototype.componentDidMount=function(){var e=this,t;this.props.heightAuto&&(this.initOthersHeight(),this.handleVirtualListHeight(),this.unSensor=Be((t=this.rootRef.current)===null||t===void 0?void 0:t.parentElement,function(){e.handleVirtualListHeight()},!1,"height"))},a.prototype.componentDidUpdate=function(e,t){var i=this.props,r=this.state,n=this.state.keyword;if(e.options!==i.options||e.searchable!==i.searchable){var l=i.options,o=i.searchable;this.setState({filteredOptions:o&&n?this.filterOptions(l,n):l})}Pe(["label","searchable","creatable","hideRoot","themeCss","wrapperCustomStyle","heightAuto"],e,i)&&i.heightAuto&&this.initOthersHeight(),t.othersHeight!==r.othersHeight&&i.heightAuto&&this.handleVirtualListHeight()},a.prototype.componentWillUnmount=function(){this.unSensor&&(this.unSensor(),delete this.unSensor)},a.prototype.initOthersHeight=function(){var e=this,t,i,r=this.rootRef.current,n=(t=this.rootRef.current)===null||t===void 0?void 0:t.parentElement,l=this.treeRef.virtualListRef.current,o=(i=this.treeRef.virtualListRef.current)===null||i===void 0?void 0:i.parentElement;if(!(!n||!o||!l||!r)){var u=function(p,c){return Array.from(p.children).filter(function(h){return h!==c}).reduce(function(h,v){return h+v.offsetHeight+e.getElementVerticalMargin(v)},0)},d=u(n,r)+this.getElementVerticalMargin(r)+u(o,l)+r.offsetHeight-o.offsetHeight;this.setState({othersHeight:d})}},a.prototype.handleVirtualListHeight=function(){var e,t=this.state.othersHeight,i=(e=this.rootRef.current)===null||e===void 0?void 0:e.parentElement;if(i){var r=i.offsetHeight-t;this.setState({virtualListHeight:r>0?r:0})}},a.prototype.getElementVerticalMargin=function(e){var t=window.getComputedStyle(e,null);return parseInt(t.marginTop||"0")+parseInt(t.marginBottom||"0")},a.prototype.reload=function(){var e=this.props.reloadOptions;e&&e()},a.prototype.doAction=function(e,t,i){var r,n,l,o,u,d,p,c,h,v,y,A=e==null?void 0:e.actionType,b=this.props,T=b.resetValue,f=b.onChange,C=b.formStore,O=b.store,E=b.name;if(A==="clear")f==null||f("");else if(A==="reset"){var S=(n=Ue((r=C==null?void 0:C.pristine)!==null&&r!==void 0?r:O==null?void 0:O.pristine,E))!==null&&n!==void 0?n:T;f==null||f(S??"")}else e.actionType==="expand"?this.treeRef.syncUnFolded(this.props,(l=e.args)===null||l===void 0?void 0:l.openLevel):e.actionType==="collapse"?this.treeRef.syncUnFolded(this.props,((o=e.args)===null||o===void 0?void 0:o.closeLevel)||0):e.actionType==="add"?this.addItemFromAction((u=e.args)===null||u===void 0?void 0:u.item,(d=e.args)===null||d===void 0?void 0:d.parentValue):e.actionType==="edit"?this.editItemFromAction((p=e.args)===null||p===void 0?void 0:p.item,(c=e.args)===null||c===void 0?void 0:c.originValue):e.actionType==="delete"?this.deleteItemFromAction((h=e.args)===null||h===void 0?void 0:h.value):e.actionType==="reload"?this.reload():A==="search"&&(this.setState({allowSearch:!!(!((v=e.args)===null||v===void 0)&&v.keyword)}),this.handleSearch((y=e.args)===null||y===void 0?void 0:y.keyword))},a.prototype.resolveOption=function(e,t){var i=this;return Q(e,function(r){var n=i.props.valueField||"value",l=X(r,n)?r[n]:"";return l===t})},a.prototype.addItemFromAction=function(e,t){var i=this.props,r=i.onAdd,n=i.options,l=i.valueField,o=Me(n,function(u){var d=l||"value",p=X(u,d)?u[d]:"";return p===t})||[];r&&r(o.concat(0),e,!0)},a.prototype.editItemFromAction=function(e,t){var i=this.props,r=i.onEdit,n=i.options,l=this.resolveOption(n,t);r&&l&&r(F(F({},e),{originValue:t}),l,!0)},a.prototype.deleteItemFromAction=function(e){var t=this.props,i=t.onDelete,r=t.options,n=this.resolveOption(r,e);i&&n&&i(n)},a.prototype.filterOptions=function(e,t){var i=this,r=this.props,n=r.labelField,l=r.valueField;return e.map(function(o){if(o=F({},o),o.visible=!!G([o],t,{keys:[n||"label",l||"value"],threshold:G.rankings.CONTAINS}).length,!o.visible&&o.children){o.children=i.filterOptions(o.children,t);var u=o.children.filter(function(d){return d.visible}).length;o.visible=!!u}return o.visible&&(o.collapsed=!1),o})},a.prototype.handleChange=function(e){return D(this,void 0,void 0,function(){var t,i,r,n,l,o,u,d,p,c,h,v,y,A,b,T,f,C;return j(this,function(O){switch(O.label){case 0:return t=this.props,i=t.onChange,r=t.searchable,n=t.options,l=t.dispatchEvent,o=t.multiple,u=t.delimiter,d=t.valueField,p=t.selectedOptions,c=t.joinValues,h=t.extractValue,v=this.state,y=v.filteredOptions,A=v.allowSearch,b=r||A?y:n,T=Ze(e,{multiple:o,delimiter:u,valueField:d,options:y}),f=o?null:T[0],o&&(r||A)&&p.length&&(p.forEach(function(E){var S=Q(y,function(k){return k[d||"value"]===E[d||"value"]});S||T.push(E)}),e=T.map(function(E){return h||c?E[d||"value"]:E}),c&&(e=e.join(u||","))),[4,l("change",et(this.props,{value:e,item:f,items:b,selectedItems:T}))];case 1:return C=O.sent(),C!=null&&C.prevented?[2]:(i&&i(e),[2])}})})},a.prototype.handleNodeClick=function(e){return D(this,void 0,void 0,function(){var t,i,r,n;return j(this,function(l){switch(l.label){case 0:return t=this.props,i=t.dispatchEvent,r=t.data,[4,i("itemClick",R(r,{item:e}))];case 1:return n=l.sent(),n!=null&&n.prevented?[2]:[2]}})})},a.prototype.handleSearch=function(e){return D(this,void 0,void 0,function(){var t,i,r,n,l,o,u,d,p,c;return j(this,function(h){switch(h.label){case 0:if(t=this.props,i=t.searchApi,r=t.options,n=t.env,l=t.data,o=t.translate,u=[],!tt(i))return[3,5];h.label=1;case 1:return h.trys.push([1,3,,4]),[4,n.fetcher(i,R(l,{term:e}))];case 2:if(d=h.sent(),!d.ok)throw new Error(o(d.msg||"networkError"));if(p=d.data.options||d.data.items||d.data,!Array.isArray(p))throw new Error(o("Tree.invalidArray"));return u=p,[3,4];case 3:return c=h.sent(),n.isCancel(c)||!i.silent&&n.notify("error",c.message),[3,4];case 4:return[3,6];case 5:e&&(u=this.filterOptions(r,e)),h.label=6;case 6:return this.setState({keyword:e,filteredOptions:e?u:r}),[2]}})})},a.prototype.domRef=function(e){this.treeRef=e},a.prototype.validate=function(){var e=this.props,t=e.value,i=e.minLength,r=e.maxLength,n=e.delimiter,l=Array.isArray(t)?t:(t?String(t):"").split(n||",");if(i&&l.length<i)return"已选择数量低于设定的最小个数".concat(i,",请选择更多的选项。");if(r&&l.length>r)return"已选择数量超出设定的最大个数".concat(r,",请取消选择超出的选项。")},a.prototype.renderOptionItem=function(e,t){var i=this.props,r=i.menuTpl,n=i.render,l=i.data;return n("option/".concat(t.index),r,{data:R(R(l,F({},t)),e)})},a.prototype.renderItemActions=function(e,t){var i=this.props,r=i.itemActions,n=i.data,l=i.render;return l("action/".concat(t.index),r||"",{data:R(R(n,F({},t)),e)})},a.prototype.renderSearch=function(){var e=this.props,t=e.classPrefix,i=e.searchConfig,r=e.mobileUI,n=e.testIdBuilder;return m.createElement(qe,F({className:B("".concat(t,"TreeControl-searchbox"),i==null?void 0:i.className,{"is-sticky":i==null?void 0:i.sticky}),mini:!1,clearable:!0},ze(i,"className","sticky"),{onSearch:this.handleSearch,mobileUI:r,testIdBuilder:n==null?void 0:n.getChild("search")}))},a.prototype.render=function(){var e=this.props,t=e.className,i=e.treeContainerClassName,r=e.classPrefix,n=e.value,l=e.enableNodePath,o=e.pathSeparator,u=o===void 0?"/":o,d=e.disabled,p=e.joinValues,c=e.extractValue,h=e.delimiter,v=e.placeholder,y=e.options,A=e.multiple,b=e.valueField,T=e.initiallyOpen,f=e.unfoldedLevel,C=e.withChildren,O=e.onlyChildren,E=e.onlyLeaf,S=e.loading,k=e.hideRoot,Y=e.rootLabel,Z=e.autoCheckChildren,ee=e.autoCancelParent,te=e.cascade,ie=e.rootValue,re=e.showIcon,ne=e.showRadio,ae=e.showOutline,oe=e.onAdd,le=e.creatable,se=e.createTip,de=e.addControls,ue=e.onEdit,he=e.editable,pe=e.editTip,ce=e.editControls,ve=e.removable,fe=e.removeTip,me=e.onDelete,ge=e.rootCreatable,ye=e.rootCreateTip,be=e.labelField,Ce=e.iconField,Fe=e.deferField,Ae=e.nodePath,Te=e.deferLoad,Ee=e.expandTreeOptions,P=e.translate,_e=e.data,Oe=e.virtualThreshold,U=e.itemHeight,Se=e.loadingConfig,Ie=e.menuTpl,we=e.enableDefaultIcon,L=e.searchable,M=e.searchConfig,N=M===void 0?{}:M,Re=e.heightAuto,Le=e.mobileUI,w=e.testIdBuilder,He=e.nodeBehavior,Ve=e.itemActions,q=e.id,xe=e.wrapperCustomStyle,z=e.themeCss,ke=e.env,H=this.props.highlightTxt,V=this.state,Ne=V.filteredOptions,De=V.keyword,W=V.allowSearch,$=V.virtualListHeight;We(H)&&(H=$e(H,_e));var K=m.createElement(Ke,{classPrefix:r,onRef:this.domRef,labelField:be,valueField:b,iconField:Ce,deferField:Fe,disabled:d,onChange:this.handleChange,onHandleNodeClick:this.handleNodeClick,joinValues:p,extractValue:c,delimiter:h,placeholder:P(v),options:L||W?Ne:y,highlightTxt:L||W?De:H,multiple:A,initiallyOpen:T,unfoldedLevel:f,withChildren:C,onlyChildren:O,onlyLeaf:E,hideRoot:k,rootLabel:P(Y),rootValue:ie,showIcon:re,showRadio:ne,showOutline:ae,autoCheckChildren:Z,autoCancelParent:ee,cascade:te,foldedField:"collapsed",value:n||"",nodePath:Ae,enableNodePath:l,pathSeparator:u,selfDisabledAffectChildren:!1,onAdd:oe,creatable:le,createTip:se,rootCreatable:ge,rootCreateTip:ye,onEdit:ue,editable:he,editTip:pe,removable:ve,removeTip:fe,onDelete:me,bultinCUD:!de&&!ce,onDeferLoad:Te,onExpandTree:Ee,virtualThreshold:Oe,virtualHeight:x($)>=0?x($):void 0,itemHeight:x(U)>0?x(U):void 0,itemRender:Ie?this.renderOptionItem:void 0,enableDefaultIcon:we,mobileUI:Le,nodeBehavior:He,itemActionsRender:Ve?this.renderItemActions:void 0,actionClassName:B(Qe(F(F({},this.props),{name:"actionControlClassName",id:q,themeCss:z}))),testIdBuilder:w==null?void 0:w.getChild("tree")});return m.createElement(m.Fragment,null,m.createElement("div",F({ref:this.rootRef,className:B("".concat(r,"TreeControl"),t,i,{"is-sticky":L&&(N==null?void 0:N.sticky),"h-auto":Re})},w==null?void 0:w.getChild("control").getTestId()),m.createElement(Xe,{size:"sm",key:"info",show:S,loadingConfig:Se}),S?null:L?m.createElement(m.Fragment,null,this.renderSearch(),K):K),m.createElement(Ge,F({},this.props,{config:{wrapperCustomStyle:xe,id:q,themeCss:z,classNames:[{key:"actionControlClassName"}]},env:ke})))},a.defaultProps={placeholder:"placeholder.noData",multiple:!1,rootLabel:"Tree.root",rootValue:"",showIcon:!0,enableNodePath:!1,pathSeparator:"/"},g([_,s("design:type",Function),s("design:paramtypes",[Object,Object]),s("design:returntype",void 0)],a.prototype,"addItemFromAction",null),g([_,s("design:type",Function),s("design:paramtypes",[Object,Object]),s("design:returntype",void 0)],a.prototype,"editItemFromAction",null),g([_,s("design:type",Function),s("design:paramtypes",[Object]),s("design:returntype",void 0)],a.prototype,"deleteItemFromAction",null),g([_,s("design:type",Function),s("design:paramtypes",[Object]),s("design:returntype",Promise)],a.prototype,"handleChange",null),g([_,s("design:type",Function),s("design:paramtypes",[Object]),s("design:returntype",Promise)],a.prototype,"handleNodeClick",null),g([_,s("design:type",Function),s("design:paramtypes",[Object]),s("design:returntype",void 0)],a.prototype,"domRef",null),g([_,s("design:type",Function),s("design:paramtypes",[Object,Object]),s("design:returntype",void 0)],a.prototype,"renderOptionItem",null),g([_,s("design:type",Function),s("design:paramtypes",[Object,Object]),s("design:returntype",void 0)],a.prototype,"renderItemActions",null),g([_,s("design:type",Function),s("design:paramtypes",[]),s("design:returntype",void 0)],a.prototype,"renderSearch",null),g([Je(),s("design:type",Function),s("design:paramtypes",[]),s("design:returntype",void 0)],a.prototype,"render",null),a}(m.Component),nt=function(I){J(a,I);function a(){return I!==null&&I.apply(this,arguments)||this}return a=g([Ye({type:"input-tree"})],a),a}(it);export{nt as TreeControlRenderer,it as default};