@jswork/react-interactive-list
Version:
React interactive-list.
2 lines • 6.53 kB
JavaScript
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }var x=Object.defineProperty,$=Object.defineProperties;var B=Object.getOwnPropertyDescriptors;var A=Object.getOwnPropertySymbols;var _=Object.prototype.hasOwnProperty,R=Object.prototype.propertyIsEnumerable;var b=(i,a,t)=>a in i?x(i,a,{enumerable:!0,configurable:!0,writable:!0,value:t}):i[a]=t,m=(i,a)=>{for(var t in a||(a={}))_.call(a,t)&&b(i,t,a[t]);if(A)for(var t of A(a))R.call(a,t)&&b(i,t,a[t]);return i},E=(i,a)=>$(i,B(a)),n=(i,a)=>x(i,"name",{value:a,configurable:!0});var M=(i,a)=>{var t={};for(var e in i)_.call(i,e)&&a.indexOf(e)<0&&(t[e]=i[e]);if(i!=null&&A)for(var e of A(i))a.indexOf(e)<0&&R.call(i,e)&&(t[e]=i[e]);return t};var o=(i,a,t)=>(b(i,typeof a!="symbol"?a+"":a,t),t);var _react = require('react'); var _react2 = _interopRequireDefault(_react);var _reactlist = require('@jswork/react-list'); var _reactlist2 = _interopRequireDefault(_reactlist);var _classnames = require('classnames'); var _classnames2 = _interopRequireDefault(_classnames);var _fastdeepequal = require('fast-deep-equal'); var _fastdeepequal2 = _interopRequireDefault(_fastdeepequal);var _harmonyevents = require('@jswork/harmony-events');var P="react-interactive-list",q={items:[],item:null,index:-1,options:null},l,H=(l=class extends _react.Component{constructor(t){super(t);o(this,"harmonyEvents",null);o(this,"eventBus",l.event);o(this,"currentAction","");o(this,"currentPayload",null);o(this,"checkInitial",n(()=>{let{initial:t,defaults:e}=this.props,s=this.stateValue;if(t&&s.length<t){let c=s.slice(0);for(let h=0;h<t-s.length;h++)c.push(e());this.handleChange(c)}},"checkInitial"));o(this,"add",n(()=>{let{defaults:t}=this.props,e=this.stateValue.slice(0);this.isGteMax||(e.push(t()),this.currentAction="add",this.handleChange(e))},"add"));o(this,"remove",n(t=>{let e=typeof t=="number"?{index:t,action:"remove"}:t,s=this.stateValue.slice(0);this.isLteMin||(s.splice(e.index,1),this.currentAction=e.action,this.currentPayload=e.payload,this.handleChange(s))},"remove"));o(this,"update",n(t=>{let e=t,s=this.stateValue.slice(0);e.index<0||e.index>=s.length||(s[e.index]=e.item,this.currentAction=e.action,this.currentPayload=e.payload,this.handleChange(s))},"update"));o(this,"set",n(t=>{this.currentAction="set",this.handleChange(t)},"set"));o(this,"up",n(t=>{let e=this.stateValue.slice(0);if(t===0)return;let s=e[t-1];e[t-1]=e[t],e[t]=s,this.currentAction="up",this.handleChange(e)},"up"));o(this,"down",n(t=>{let e=this.stateValue.slice(0);if(t===e.length-1)return;let s=e[t+1];e[t+1]=e[t],e[t]=s,this.currentAction="down",this.handleChange(e)},"down"));o(this,"top",n(t=>{let e=this.stateValue.slice(0),s=e.splice(t,1);e.unshift(s[0]),this.currentAction="top",this.handleChange(e)},"top"));o(this,"bottom",n(t=>{let e=this.stateValue.slice(0),s=e.splice(t,1);e.push(s[0]),this.currentAction="bottom",this.handleChange(e)},"bottom"));o(this,"clear",n(()=>{this.currentAction="clear",this.handleChange([])},"clear"));o(this,"notify",n(t=>{this.currentAction=(t==null?void 0:t.action)||"notify",this.handleChange(this.stateValue.slice(0))},"notify"));o(this,"template",n(({item:t,index:e})=>{let{template:s,options:c,hookable:h}=this.props,u=this.stateValue.slice();if(h){let d=s;return _react2.default.createElement(d,{item:t,index:e,items:u,options:c})}return s==null?void 0:s({item:t,index:e,items:u,options:c})},"template"));o(this,"templateEmpty",n(()=>{let{templateEmpty:t,hookable:e}=this.props;if(!t)return null;if(e){let s=t;return _react2.default.createElement(s,this.emptyArgs)}return t(this.emptyArgs)},"templateEmpty"));o(this,"handleChange",n(t=>{let{onChange:e,onError:s,min:c,max:h,name:u,options:d}=this.props,C=this.state.value,f=[...t];this.setState({value:t},()=>{let g={name:u,options:d,action:this.currentAction,payload:this.currentPayload,oldValue:C,newValue:f};e==null||e(t,g),this.eventBus.emit(`${u}:change`,g),this.length<c&&(s==null||s("EQ_MIN")),this.length>h&&(s==null||s("EQ_MAX"))})},"handleChange"));let{value:e}=t,s=e||[];this.state={value:[...s]}}get emptyArgs(){return E(m({},q),{options:this.props.options})}get length(){return this.stateValue.length}get stateValue(){return this.state.value||[]}get isLteMin(){let{min:t}=this.props;return this.length<=t}get isGteMax(){let{max:t}=this.props;return this.length>=t}get listView(){let{hookable:t,options:e,listProps:s}=this.props,c=m({items:this.stateValue,template:this.template,options:e,hookable:t},s);return _react2.default.createElement(_reactlist2.default,c)}componentDidUpdate(){let{value:t,onChange:e}=this.props,{value:s}=this.state,c=_fastdeepequal2.default.call(void 0, t,s);t!==void 0&&!c&&(this.setState({value:t}),e==null||e(t))}componentDidMount(){this.checkInitial(),this.harmonyEvents=_harmonyevents.ReactHarmonyEvents.create(this),this.eventBus=l.event}componentWillUnmount(){var t;(t=this.harmonyEvents)==null||t.destroy()}render(){let w=this.props,{className:t,name:e,options:s,listProps:c,forwardedRef:h,initial:u,min:d,max:C,value:f,hookable:g,template:L,templateEmpty:N,defaults:r,onChange:y,onError:p}=w,S=M(w,["className","name","options","listProps","forwardedRef","initial","min","max","value","hookable","template","templateEmpty","defaults","onChange","onError"]);return!f||f.length===0?this.templateEmpty():_react2.default.createElement("div",m({className:_classnames2.default.call(void 0, P,t),ref:h},S),this.listView)}},n(l,"ReactInteractiveList"),o(l,"displayName",P),o(l,"event"),o(l,"events",["add","remove","cancel","set","up","down","top","bottom","clear","notify","change"]),o(l,"defaultProps",{name:"@",initial:0,min:0,max:100,value:[]}),l),v= exports.ReactInteractiveList =H;var W=n(i=>{let a=i||"@",t=n((r,...y)=>{var p;return(p=v.event)==null?void 0:p.emit(`${a}:${r}`,...y)},"execute");return{listen:n((r,y)=>{var p;return(p=v.event)==null?void 0:p.on(`${a}:${r}`,y)},"listen"),add:n(()=>t("add"),"add"),remove:n(r=>t("remove",r),"remove"),notify:n(r=>t("notify",r),"notify"),update:n(r=>t("update",r),"update"),set:n(r=>t("set",r),"set"),up:n(r=>t("up",r),"up"),down:n(r=>t("down",r),"down"),clear:n(()=>t("clear"),"clear"),top:n(r=>t("top",r),"top"),bottom:n(r=>t("bottom",r),"bottom")}},"useCommand"),X= exports.useCommand =W;var it=_react2.default.forwardRef((i,a)=>_react2.default.createElement(v,E(m({},i),{ref:a})));exports.ReactInteractiveList = v; exports.default = it; exports.useCommand = X;
//# sourceMappingURL=main.cjs.js.map