2
0

GridEditModal.jsx 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. import React from 'react';
  2. import PropTypes from 'prop-types';
  3. import {
  4. Modal, ModalHeader, ModalBody, ModalFooter,
  5. } from 'reactstrap';
  6. import geu from './GridEditorUtil';
  7. import BootstrapGrid from '../../models/BootstrapGrid';
  8. export default class GridEditModal extends React.Component {
  9. constructor(props) {
  10. super(props);
  11. this.state = {
  12. colsRatios: [6, 6],
  13. responsiveSize: BootstrapGrid.ResponsiveSize.XS_SIZE,
  14. show: false,
  15. gridHtml: '',
  16. };
  17. this.init = this.init.bind(this);
  18. this.show = this.show.bind(this);
  19. this.hide = this.hide.bind(this);
  20. this.cancel = this.cancel.bind(this);
  21. this.pasteCodedGrid = this.pasteCodedGrid.bind(this);
  22. this.showGridPattern = this.showGridPattern.bind(this);
  23. this.checkResposiveSize = this.checkResposiveSize.bind(this);
  24. }
  25. async checkResposiveSize(rs) {
  26. await this.setState({ responsiveSize: rs });
  27. }
  28. async checkColsRatios(cr) {
  29. await this.setState({ colsRatios: cr });
  30. }
  31. showGridPattern() {
  32. const colsRatios = this.state.colsRatios;
  33. const createdCol = colsRatios.map((colsRatio) => {
  34. return `- ${colsRatio} `;
  35. });
  36. return createdCol.join('').slice(1);
  37. }
  38. init(gridHtml) {
  39. const initGridHtml = gridHtml;
  40. this.setState({ gridHtml: initGridHtml }, function() {
  41. // display gridHtml for re-editing
  42. console.log(this.state.gridHtml);
  43. });
  44. }
  45. show(gridHtml) {
  46. this.init(gridHtml);
  47. this.setState({ show: true });
  48. }
  49. hide() {
  50. this.setState({ show: false });
  51. }
  52. cancel() {
  53. this.hide();
  54. }
  55. pasteCodedGrid() {
  56. // dummy data
  57. const convertedHTML = geu.convertRatiosAndSizeToHTML([1, 5, 6], 'sm');
  58. const pastedGridData = `::: editable-row\n<div class="container">\n\t<div class="row">\n${convertedHTML}\n\t</div>\n</div>\n:::`;
  59. // display converted html on console
  60. console.log(convertedHTML);
  61. if (this.props.onSave != null) {
  62. this.props.onSave(pastedGridData);
  63. }
  64. this.cancel();
  65. }
  66. gridDivisionMenu() {
  67. const gridDivisions = geu.mappingAllGridDivisionPatterns;
  68. return (
  69. <div className="container">
  70. <div className="row">
  71. {gridDivisions.map((gridDivion, i) => {
  72. return (
  73. <div className="col-md-4 text-center">
  74. <h6 className="dropdown-header">{i + 2}分割</h6>
  75. {gridDivion.map((gridOneDivision) => {
  76. return (
  77. <button className="dropdown-item" type="button" onClick={() => { this.checkColsRatios(gridOneDivision) }}>
  78. <div className="row">
  79. {gridOneDivision.map((gtd) => {
  80. const className = `bg-info col-${gtd} border`;
  81. return <span className={className}>{gtd}</span>;
  82. })}
  83. </div>
  84. </button>
  85. );
  86. })}
  87. </div>
  88. );
  89. })}
  90. </div>
  91. </div>
  92. );
  93. }
  94. render() {
  95. return (
  96. <Modal isOpen={this.state.show} toggle={this.cancel} size="xl" className="grw-grid-edit-modal">
  97. <ModalHeader tag="h4" toggle={this.cancel} className="bg-primary text-light">
  98. Create Bootstrap 4 Grid
  99. </ModalHeader>
  100. <ModalBody>
  101. <div className="container">
  102. <div className="row">
  103. <div className="col-3">
  104. <div className="mr-3 d-inline">
  105. <label htmlFor="gridPattern">Grid Pattern :</label>
  106. </div>
  107. <button
  108. className="btn btn-outline-secondary dropdown-toggle text-right col-12 col-md-auto"
  109. type="button"
  110. id="dropdownMenuButton"
  111. data-toggle="dropdown"
  112. aria-haspopup="true"
  113. aria-expanded="false"
  114. >
  115. {this.showGridPattern()}
  116. </button>
  117. <div className="dropdown-menu grid-division-menu" aria-labelledby="dropdownMenuButton">
  118. {this.gridDivisionMenu()}
  119. </div>
  120. </div>
  121. <div className="col-3 text-right pr-0">
  122. <label className="pr-3">Break point by display size :</label>
  123. </div>
  124. <div className="col-3 text-left pl-0">
  125. <div className="form-group inline-block">
  126. <div className="custom-control custom-radio">
  127. {/* TODO unite radio button style with that of AppSetting.jsx by GW-3342 */}
  128. <input
  129. type="radio"
  130. id={`radio-${BootstrapGrid.ResponsiveSize.XS_SIZE}`}
  131. className="custom-control-input"
  132. name="responsiveSize"
  133. value={BootstrapGrid.ResponsiveSize.XS_SIZE}
  134. onChange={(e) => { this.checkResposiveSize(e.target.value) }}
  135. checked={this.state.responsiveSize === BootstrapGrid.ResponsiveSize.XS_SIZE}
  136. />
  137. <label htmlFor={`radio-${BootstrapGrid.ResponsiveSize.XS_SIZE}`} className="custom-control-label">
  138. <i className="pl-2 pr-1 icon-screen-smartphone"></i> Mobile / No break point
  139. </label>
  140. </div>
  141. <div className="custom-control custom-radio">
  142. <input
  143. type="radio"
  144. id={`radio-${BootstrapGrid.ResponsiveSize.SM_SIZE}`}
  145. className="custom-control-input"
  146. name="responsiveSize"
  147. value={BootstrapGrid.ResponsiveSize.SM_SIZE}
  148. onChange={(e) => { this.checkResposiveSize(e.target.value) }}
  149. checked={this.state.responsiveSize === BootstrapGrid.ResponsiveSize.SM_SIZE}
  150. />
  151. <label htmlFor={`radio-${BootstrapGrid.ResponsiveSize.SM_SIZE}`} className="custom-control-label">
  152. <i className="pl-2 pr-1 icon-screen-tablet"></i> Tablet
  153. </label>
  154. </div>
  155. <div className="custom-control custom-radio">
  156. <input
  157. type="radio"
  158. id={`radio-${BootstrapGrid.ResponsiveSize.MD_SIZE}`}
  159. className="custom-control-input"
  160. name="responsiveSize"
  161. value={BootstrapGrid.ResponsiveSize.MD_SIZE}
  162. onChange={(e) => { this.checkResposiveSize(e.target.value) }}
  163. checked={this.state.responsiveSize === BootstrapGrid.ResponsiveSize.MD_SIZE}
  164. />
  165. <label htmlFor={`radio-${BootstrapGrid.ResponsiveSize.MD_SIZE}`} className="custom-control-label">
  166. <i className="pl-2 pr-1 icon-screen-desktop"></i> Desktop
  167. </label>
  168. </div>
  169. </div>
  170. </div>
  171. </div>
  172. <div className="row">
  173. <h1 className="pl-3 w-100">Preview</h1>
  174. <div className="col-6">
  175. <label className="d-block"><i className="pr-2 icon-screen-desktop"></i>Desktop</label>
  176. <div className="desktop-preview border d-block"></div>
  177. </div>
  178. <div className="col-3">
  179. <label className="d-block"><i className="pr-2 icon-screen-tablet"></i>Tablet</label>
  180. <div className="tablet-preview border d-block"></div>
  181. </div>
  182. <div className="col-3">
  183. <label className="d-block"><i className="pr-2 icon-screen-smartphone"></i>Mobile</label>
  184. <div className="mobile-preview border d-block"></div>
  185. </div>
  186. </div>
  187. </div>
  188. </ModalBody>
  189. <ModalFooter className="grw-modal-footer">
  190. <div className="ml-auto">
  191. <button type="button" className="mr-2 btn btn-secondary" onClick={this.cancel}>
  192. Cancel
  193. </button>
  194. <button type="button" className="btn btn-primary" onClick={this.pasteCodedGrid}>
  195. Done
  196. </button>
  197. </div>
  198. </ModalFooter>
  199. </Modal>
  200. );
  201. }
  202. }
  203. GridEditModal.propTypes = {
  204. onSave: PropTypes.func,
  205. };