GridEditModal.jsx 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. import React from 'react';
  2. import { useTranslation } from 'next-i18next';
  3. import PropTypes from 'prop-types';
  4. import {
  5. Modal, ModalHeader, ModalBody, ModalFooter,
  6. } from 'reactstrap';
  7. import BootstrapGrid from '~/client/models/BootstrapGrid';
  8. import geu from './GridEditorUtil';
  9. import styles from './GridEditModal.module.scss';
  10. const resSizes = BootstrapGrid.ResponsiveSize;
  11. const resSizeObj = {
  12. [resSizes.XS_SIZE]: { displayText: 'grid_edit.smart_no' },
  13. [resSizes.SM_SIZE]: { displayText: 'tablet' },
  14. [resSizes.MD_SIZE]: { displayText: 'desktop' },
  15. };
  16. class GridEditModal extends React.Component {
  17. constructor(props) {
  18. super(props);
  19. this.state = {
  20. colsRatios: [6, 6],
  21. responsiveSize: BootstrapGrid.ResponsiveSize.XS_SIZE,
  22. show: false,
  23. // use when re-edit grid
  24. // gridHtml: '',
  25. };
  26. this.checkResposiveSize = this.checkResposiveSize.bind(this);
  27. this.checkColsRatios = this.checkColsRatios.bind(this);
  28. // use when re-edit grid
  29. // this.init = this.init.bind(this);
  30. this.show = this.show.bind(this);
  31. this.hide = this.hide.bind(this);
  32. this.cancel = this.cancel.bind(this);
  33. this.pasteCodedGrid = this.pasteCodedGrid.bind(this);
  34. this.renderSelectedGridPattern = this.renderSelectedGridPattern.bind(this);
  35. this.renderBreakPointSetting = this.renderBreakPointSetting.bind(this);
  36. }
  37. async checkResposiveSize(rs) {
  38. await this.setState({ responsiveSize: rs });
  39. }
  40. async checkColsRatios(cr) {
  41. await this.setState({ colsRatios: cr });
  42. }
  43. // use when re-edit grid
  44. // init(gridHtml) {
  45. // const initGridHtml = gridHtml;
  46. // this.setState({ gridHtml: initGridHtml });
  47. // }
  48. show(gridHtml) {
  49. // use when re-edit grid
  50. // this.init(gridHtml);
  51. this.setState({ show: true });
  52. }
  53. hide() {
  54. this.setState({ show: false });
  55. }
  56. cancel() {
  57. this.hide();
  58. }
  59. pasteCodedGrid() {
  60. const { colsRatios, responsiveSize } = this.state;
  61. const convertedHTML = geu.convertRatiosAndSizeToHTML(colsRatios, responsiveSize);
  62. const spaceTab = ' ';
  63. const pastedGridData = `::: editable-row\n<div class="container">\n${spaceTab}<div class="row">\n${convertedHTML}\n${spaceTab}</div>\n</div>\n:::`;
  64. if (this.props.onSave != null) {
  65. this.props.onSave(pastedGridData);
  66. }
  67. this.cancel();
  68. }
  69. renderSelectedGridPattern() {
  70. const colsRatios = this.state.colsRatios;
  71. return colsRatios.join(' - ');
  72. }
  73. renderBreakPointSetting() {
  74. const { t } = this.props;
  75. const output = Object.entries(resSizeObj).map((responsiveSizeForMap) => {
  76. return (
  77. <div key={responsiveSizeForMap[0]} className="form-check form-check-inline">
  78. <input
  79. type="radio"
  80. className="form-check-input"
  81. id={responsiveSizeForMap[1].displayText}
  82. value={responsiveSizeForMap[1].displayText}
  83. checked={this.state.responsiveSize === responsiveSizeForMap[0]}
  84. onChange={e => this.checkResposiveSize(responsiveSizeForMap[0])}
  85. />
  86. <label className="form-label form-check-label" htmlFor={responsiveSizeForMap[1].displayText}>
  87. {t(responsiveSizeForMap[1].displayText)}
  88. </label>
  89. </div>
  90. );
  91. });
  92. return output;
  93. }
  94. renderGridDivisionMenu() {
  95. const gridDivisions = geu.mappingAllGridDivisionPatterns;
  96. const { t } = this.props;
  97. return (
  98. <div className="container">
  99. <div className="row">
  100. {gridDivisions.map((gridDivision) => {
  101. const numOfDivisions = gridDivision.numberOfGridDivisions;
  102. return (
  103. <div key={`${numOfDivisions}-divisions`} className="col-md-4 text-center">
  104. <h6 className="dropdown-header">{numOfDivisions} {t('grid_edit.division')}</h6>
  105. {gridDivision.mapping.map((gridOneDivision) => {
  106. const keyOfRow = `${numOfDivisions}-divisions-${gridOneDivision.join('-')}`;
  107. return (
  108. <button key={keyOfRow} className="dropdown-item" type="button" onClick={() => { this.checkColsRatios(gridOneDivision) }}>
  109. <div className="row">
  110. {gridOneDivision.map((god, i) => {
  111. const keyOfCol = `${keyOfRow}-${i}`;
  112. const className = `bg-info col-${god} border`;
  113. return <span key={keyOfCol} className={className}>{god}</span>;
  114. })}
  115. </div>
  116. </button>
  117. );
  118. })}
  119. </div>
  120. );
  121. })}
  122. </div>
  123. </div>
  124. );
  125. }
  126. renderPreview() {
  127. const { t } = this.props;
  128. const isMdSelected = this.state.responsiveSize === BootstrapGrid.ResponsiveSize.MD_SIZE;
  129. const isXsSelected = this.state.responsiveSize === BootstrapGrid.ResponsiveSize.XS_SIZE;
  130. return (
  131. <div className="row grw-grid-edit-preview border my-4 p-3">
  132. <div className="col-lg-2">
  133. <h4 className="d-block mt-2">{t('phone')}</h4>
  134. <div className="mobile-preview d-block px-3 py-2">
  135. {this.renderGridPreview(!isXsSelected)}
  136. </div>
  137. </div>
  138. <div className="col-lg-3">
  139. <h4 className="d-block mt-2">{t('tablet')}</h4>
  140. <div className="tablet-preview d-block px-3 py-2">
  141. {this.renderGridPreview(isMdSelected)}
  142. </div>
  143. </div>
  144. <div className="col-lg-4">
  145. <h4 className="d-block mt-2">{t('desktop')}</h4>
  146. <div className="desktop-preview d-block px-3 py-2">
  147. {this.renderGridPreview(false)}
  148. </div>
  149. </div>
  150. </div>
  151. );
  152. }
  153. renderGridPreview(isBreakEnabled) {
  154. const { colsRatios } = this.state;
  155. const convertedHTML = colsRatios.map((colsRatio, i) => {
  156. const ratio = isBreakEnabled ? 12 : colsRatio;
  157. const key = `grid-preview-col-${i}`;
  158. const className = `col-${ratio} grid-edit-border-for-each-cols`;
  159. return (
  160. <div key={key} className={`${key} ${className}`}></div>
  161. );
  162. });
  163. return (
  164. <div className="row">{convertedHTML}</div>
  165. );
  166. }
  167. render() {
  168. const { t } = this.props;
  169. return (
  170. <Modal isOpen={this.state.show} toggle={this.cancel} size="xl" className={`grw-grid-edit-modal ${styles['grw-grid-edit-modal']}`}>
  171. <ModalHeader tag="h4" toggle={this.cancel}>
  172. {t('grid_edit.create_bootstrap_4_grid')}
  173. </ModalHeader>
  174. <ModalBody className="container">
  175. <div className="row">
  176. <div className="col-12">
  177. <h3 className="grw-modal-head">{t('grid_edit.grid_settings')}</h3>
  178. <form className="mb-0">
  179. <div className="row my-3">
  180. <label className="form-label col-sm-3" htmlFor="gridPattern">
  181. {t('grid_edit.grid_pattern')}
  182. </label>
  183. <div className="col-sm-9">
  184. <button
  185. className="btn btn-outline-secondary dropdown-toggle"
  186. type="button"
  187. id="dropdownMenuButton"
  188. data-bs-toggle="dropdown"
  189. aria-haspopup="true"
  190. aria-expanded="false"
  191. >
  192. {this.renderSelectedGridPattern()}
  193. </button>
  194. <div className="dropdown-menu grid-division-menu" aria-labelledby="dropdownMenuButton">
  195. {this.renderGridDivisionMenu()}
  196. </div>
  197. </div>
  198. </div>
  199. <div className="row">
  200. <label className="form-label col-sm-3" htmlFor="breakPoint">
  201. {t('grid_edit.break_point')}
  202. </label>
  203. <div className="col-sm-9">
  204. {this.renderBreakPointSetting()}
  205. </div>
  206. </div>
  207. </form>
  208. </div>
  209. </div>
  210. <h3 className="grw-modal-head">{t('preview')}</h3>
  211. <div className="col-12">
  212. {this.renderPreview()}
  213. </div>
  214. </ModalBody>
  215. <ModalFooter className="grw-modal-footer">
  216. <div className="ms-auto">
  217. <button type="button" className="me-2 btn btn-secondary" onClick={this.cancel}>
  218. Cancel
  219. </button>
  220. <button type="button" className="btn btn-primary" onClick={this.pasteCodedGrid}>
  221. Done
  222. </button>
  223. </div>
  224. </ModalFooter>
  225. </Modal>
  226. );
  227. }
  228. }
  229. const GridEditModalFc = React.forwardRef((props, ref) => {
  230. const { t } = useTranslation();
  231. return <GridEditModal t={t} ref={ref} {...props} />;
  232. });
  233. GridEditModal.propTypes = {
  234. onSave: PropTypes.func,
  235. t: PropTypes.func.isRequired,
  236. };
  237. export default GridEditModalFc;