import React from 'react'; import { useTranslation } from 'next-i18next'; import PropTypes from 'prop-types'; import { Modal, ModalHeader, ModalBody, ModalFooter, } from 'reactstrap'; import BootstrapGrid from '~/client/models/BootstrapGrid'; import geu from './GridEditorUtil'; import styles from './GridEditModal.module.scss'; const resSizes = BootstrapGrid.ResponsiveSize; const resSizeObj = { [resSizes.XS_SIZE]: { displayText: 'grid_edit.smart_no' }, [resSizes.SM_SIZE]: { displayText: 'tablet' }, [resSizes.MD_SIZE]: { displayText: 'desktop' }, }; class GridEditModal extends React.Component { constructor(props) { super(props); this.state = { colsRatios: [6, 6], responsiveSize: BootstrapGrid.ResponsiveSize.XS_SIZE, show: false, // use when re-edit grid // gridHtml: '', }; this.checkResposiveSize = this.checkResposiveSize.bind(this); this.checkColsRatios = this.checkColsRatios.bind(this); // use when re-edit grid // this.init = this.init.bind(this); this.show = this.show.bind(this); this.hide = this.hide.bind(this); this.cancel = this.cancel.bind(this); this.pasteCodedGrid = this.pasteCodedGrid.bind(this); this.renderSelectedGridPattern = this.renderSelectedGridPattern.bind(this); this.renderBreakPointSetting = this.renderBreakPointSetting.bind(this); } async checkResposiveSize(rs) { await this.setState({ responsiveSize: rs }); } async checkColsRatios(cr) { await this.setState({ colsRatios: cr }); } // use when re-edit grid // init(gridHtml) { // const initGridHtml = gridHtml; // this.setState({ gridHtml: initGridHtml }); // } show(gridHtml) { // use when re-edit grid // this.init(gridHtml); this.setState({ show: true }); } hide() { this.setState({ show: false }); } cancel() { this.hide(); } pasteCodedGrid() { const { colsRatios, responsiveSize } = this.state; const convertedHTML = geu.convertRatiosAndSizeToHTML(colsRatios, responsiveSize); const spaceTab = ' '; const pastedGridData = `::: editable-row\n
\n${spaceTab}
\n${convertedHTML}\n${spaceTab}
\n
\n:::`; if (this.props.onSave != null) { this.props.onSave(pastedGridData); } this.cancel(); } renderSelectedGridPattern() { const colsRatios = this.state.colsRatios; return colsRatios.join(' - '); } renderBreakPointSetting() { const { t } = this.props; const output = Object.entries(resSizeObj).map((responsiveSizeForMap) => { return (
this.checkResposiveSize(responsiveSizeForMap[0])} />
); }); return output; } renderGridDivisionMenu() { const gridDivisions = geu.mappingAllGridDivisionPatterns; const { t } = this.props; return (
{gridDivisions.map((gridDivision) => { const numOfDivisions = gridDivision.numberOfGridDivisions; return (
{numOfDivisions} {t('grid_edit.division')}
{gridDivision.mapping.map((gridOneDivision) => { const keyOfRow = `${numOfDivisions}-divisions-${gridOneDivision.join('-')}`; return ( ); })}
); })}
); } renderPreview() { const { t } = this.props; const isMdSelected = this.state.responsiveSize === BootstrapGrid.ResponsiveSize.MD_SIZE; const isXsSelected = this.state.responsiveSize === BootstrapGrid.ResponsiveSize.XS_SIZE; return (

{t('phone')}

{this.renderGridPreview(!isXsSelected)}

{t('tablet')}

{this.renderGridPreview(isMdSelected)}

{t('desktop')}

{this.renderGridPreview(false)}
); } renderGridPreview(isBreakEnabled) { const { colsRatios } = this.state; const convertedHTML = colsRatios.map((colsRatio, i) => { const ratio = isBreakEnabled ? 12 : colsRatio; const key = `grid-preview-col-${i}`; const className = `col-${ratio} grid-edit-border-for-each-cols`; return (
); }); return (
{convertedHTML}
); } render() { const { t } = this.props; return ( {t('grid_edit.create_bootstrap_4_grid')}

{t('grid_edit.grid_settings')}

{this.renderGridDivisionMenu()}
{this.renderBreakPointSetting()}

{t('preview')}

{this.renderPreview()}
); } } const GridEditModalFc = React.forwardRef((props, ref) => { const { t } = useTranslation(); return ; }); GridEditModal.propTypes = { onSave: PropTypes.func, t: PropTypes.func.isRequired, }; export default GridEditModalFc;