GridEditModal.jsx 9.4 KB

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