gridjs
Version:
Advanced table plugin
3 lines (2 loc) • 2.15 kB
JavaScript
import{BaseStore as t,BaseActions as s,PluginBaseComponent as e,h as i,className as h}from"gridjs";class o extends t{getInitialState(){return{rowIds:[]}}handle(t,s){if("CHECK"===t){const{ROW_ID:t}=s;this.check(t)}if("UNCHECK"===t){const{ROW_ID:t}=s;this.uncheck(t)}}check(t){this.state.rowIds.indexOf(t)>-1||this.setState({rowIds:[t,...this.state.rowIds]})}uncheck(t){const s=this.state.rowIds.indexOf(t);if(-1===s)return;const e=[...this.state.rowIds];e.splice(s,1),this.setState({rowIds:e})}}class c extends s{check(t){this.dispatch("CHECK",{ROW_ID:t})}uncheck(t){this.dispatch("UNCHECK",{ROW_ID:t})}}class a extends e{constructor(t,s){if(super(t,s),this.actions=void 0,this.store=void 0,this.storeUpdatedFn=void 0,this.isDataCell=t=>void 0!==t.row,this.getParentTR=()=>this.base&&this.base.parentElement&&this.base.parentElement.parentElement,this.state={isChecked:!1},this.isDataCell(t)){if(t.store)this.store=t.store;else{const s=new o(this.config.dispatcher);this.store=s,t.plugin.props.store=s}this.actions=new c(this.config.dispatcher),this.storeUpdatedFn=this.storeUpdated.bind(this),this.store.on("updated",this.storeUpdatedFn),t.cell.data&&this.check()}}componentWillUnmount(){this.store.off("updated",this.storeUpdatedFn)}componentDidMount(){this.store&&this.storeUpdated(this.store.state)}storeUpdated(t){const s=this.getParentTR();if(!s)return;const e=t.rowIds.indexOf(this.props.id(this.props.row))>-1;this.setState({isChecked:e}),e?s.classList.add(this.props.selectedClassName):s.classList.remove(this.props.selectedClassName)}check(){this.actions.check(this.props.id(this.props.row)),this.props.cell.update(!0)}uncheck(){this.actions.uncheck(this.props.id(this.props.row)),this.props.cell.update(!1)}toggle(){this.state.isChecked?this.uncheck():this.check()}render(){return this.isDataCell(this.props)?i("input",{type:"checkbox",checked:this.state.isChecked,onChange:()=>this.toggle(),className:this.props.checkboxClassName}):null}}a.defaultProps={selectedClassName:h("tr","selected"),checkboxClassName:h("checkbox")};export{a as RowSelection,c as RowSelectionActions,o as RowSelectionStore};
//# sourceMappingURL=selection.modern.js.map