Form.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. var _react = _interopRequireDefault(require("react"));
  7. var _modularityFront = _interopRequireDefault(require("modularity-front"));
  8. var _reactMaterialize = require("react-materialize");
  9. var _reactGoogleRecaptcha = _interopRequireDefault(require("react-google-recaptcha"));
  10. var _DropdownFilter = _interopRequireDefault(require("./UserInputs/DropdownFilter"));
  11. var _Editor = _interopRequireDefault(require("./UserInputs/Editor"));
  12. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  13. function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
  14. function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; var ownKeys = Object.keys(source); if (typeof Object.getOwnPropertySymbols === 'function') { ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) { return Object.getOwnPropertyDescriptor(source, sym).enumerable; })); } ownKeys.forEach(function (key) { _defineProperty(target, key, source[key]); }); } return target; }
  15. function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
  16. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  17. function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
  18. function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
  19. function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }
  20. function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
  21. function _get(target, property, receiver) { if (typeof Reflect !== "undefined" && Reflect.get) { _get = Reflect.get; } else { _get = function _get(target, property, receiver) { var base = _superPropBase(target, property); if (!base) return; var desc = Object.getOwnPropertyDescriptor(base, property); if (desc.get) { return desc.get.call(receiver); } return desc.value; }; } return _get(target, property, receiver || target); }
  22. function _superPropBase(object, property) { while (!Object.prototype.hasOwnProperty.call(object, property)) { object = _getPrototypeOf(object); if (object === null) break; } return object; }
  23. function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
  24. function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }
  25. function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
  26. var Input = _reactMaterialize.TextInput;
  27. var Module = _modularityFront.default.Module;
  28. var FormModule =
  29. /*#__PURE__*/
  30. function (_Modularity$Module) {
  31. _inherits(FormModule, _Modularity$Module);
  32. function FormModule(props) {
  33. var _this;
  34. _classCallCheck(this, FormModule);
  35. _this = _possibleConstructorReturn(this, _getPrototypeOf(FormModule).call(this, props));
  36. _this.state = {};
  37. _this.FormInputs.onUpdate(function (e) {
  38. _this.setState(_this.updateFromInput());
  39. });
  40. _this.state = _objectSpread({}, _this.state, _this.updateFromInput());
  41. _this.formRef = _react.default.createRef();
  42. return _this;
  43. }
  44. _createClass(FormModule, [{
  45. key: "updateFromInput",
  46. value: function updateFromInput() {
  47. var _this2 = this;
  48. return Object.keys(this.FormInputs.value).reduce(function (obj, key, index) {
  49. return _objectSpread({}, obj, _defineProperty({}, key, _this2.FormInputs.value[key].value));
  50. }, {});
  51. }
  52. }, {
  53. key: "updateField",
  54. value: function updateField(key, value, filter) {
  55. var field = _defineProperty({}, key, value);
  56. var state = _objectSpread({}, this.state, field);
  57. this.setState(_objectSpread({}, field));
  58. this.formData.set(state);
  59. }
  60. }, {
  61. key: "isValid",
  62. value: function isValid() {
  63. console.log(this.state);
  64. for (var i in this.FormInputs.value) {
  65. var item = this.FormInputs.value[i];
  66. console.log(item, item.filter.required, this.state[i]);
  67. if (item.filter && item.filter.required && !this.state[i]) return false;
  68. }
  69. return true;
  70. }
  71. }, {
  72. key: "captchaComplete",
  73. value: function captchaComplete(token) {
  74. if (!token || token.legth < 10) return;
  75. this.setState({
  76. captcha: token.toString()
  77. });
  78. }
  79. }, {
  80. key: "submitData",
  81. value: function submitData(e) {
  82. e.preventDefault();
  83. this.onSubmit.set(this.state);
  84. }
  85. }, {
  86. key: "render",
  87. value: function render() {
  88. var _this3 = this;
  89. return _react.default.createElement("div", {
  90. className: " " + this.props.className
  91. }, _react.default.createElement("form", {
  92. ref: this.formRef,
  93. className: " ",
  94. onSubmit: function onSubmit(e) {
  95. return _this3.submitData(e);
  96. }
  97. }, _react.default.createElement("div", {
  98. className: "row"
  99. }, Object.keys(this.FormInputs.value).map(function (key, index) {
  100. var input = _this3.FormInputs.value[key];
  101. if (input) {
  102. if (input.filter && ["manytomany"].includes(input.filter.type)) {
  103. return _react.default.createElement("div", {
  104. className: "input-field " + input.columns,
  105. key: key
  106. }, _react.default.createElement(_DropdownFilter.default, {
  107. filter: input.filter,
  108. defaultValue: (input.value || [])[0],
  109. Filters: function Filters(data) {
  110. return data ? _this3.updateField(input.filter.field, data.values, input.filter) : null;
  111. }
  112. }));
  113. } else if (input.filter && ["onetoone"].includes(input.filter.type)) {
  114. return _react.default.createElement("div", {
  115. className: "input-field " + input.columns,
  116. key: key
  117. }, _react.default.createElement(_DropdownFilter.default, {
  118. filter: input.filter,
  119. defaultValue: input.value.name,
  120. Filters: function Filters(data) {
  121. return data ? _this3.updateField(input.filter.field, data.values[0], input.filter) : null;
  122. }
  123. }));
  124. } else if (input.filter && ["exact", "select"].includes(input.filter.type)) {
  125. return _react.default.createElement("div", {
  126. className: "input-field " + input.columns,
  127. key: key
  128. }, _react.default.createElement(_DropdownFilter.default, {
  129. filter: input.filter,
  130. defaultValue: input.value,
  131. Filters: function Filters(data) {
  132. return data ? _this3.updateField(input.filter.field, data.values[0], input.filter) : null;
  133. }
  134. }));
  135. } else if (input.filter && ["editor"].includes(input.filter.type)) {
  136. // Editor here
  137. return _react.default.createElement("div", {
  138. className: "input-field col s12",
  139. key: key
  140. }, _react.default.createElement(_Editor.default, {
  141. Name: input.filter.name || key,
  142. data: input.value,
  143. onChange: function onChange(data) {
  144. return data ? _this3.updateField(input.filter.field, data, input.filter) : null;
  145. }
  146. }));
  147. } else if (input.filter && ["textarea"].includes(input.filter.type)) {
  148. // todo textarea
  149. return _react.default.createElement("div", {
  150. className: "input-field col s12",
  151. key: key
  152. }, _react.default.createElement(_reactMaterialize.Textarea, {
  153. Name: input.filter.name || key,
  154. data: input.value,
  155. onChange: function onChange(data) {
  156. return _this3.updateField(input.filter.field, data.target.value, input.filter);
  157. },
  158. label: input.placeholder,
  159. noLayout: true,
  160. email: input.filter.email,
  161. password: input.filter.password,
  162. validate: input.filter.required,
  163. success: input.filter.success,
  164. error: input.filter.error || "Required field",
  165. required: input.filter.required
  166. }));
  167. } else {
  168. input.filter = input.filter || {};
  169. return _react.default.createElement("div", {
  170. className: input.columns,
  171. key: key
  172. }, _react.default.createElement(Input, {
  173. noLayout: true,
  174. key: key,
  175. value: input.value === undefined || Number.isNaN(input.value) ? input.value || '' : '' + input.value,
  176. onChange: function onChange(e) {
  177. return _this3.updateField(key, e.target.value, input.filter);
  178. },
  179. label: input.placeholder,
  180. email: input.filter.email,
  181. password: input.filter.password,
  182. validate: input.filter.required,
  183. success: input.filter.success,
  184. error: input.filter.error || "Required field",
  185. required: input.filter.required
  186. }));
  187. }
  188. }
  189. })), this.withCaptcha.value && this.CaptchaKey.value && _react.default.createElement("div", null, _react.default.createElement(_reactGoogleRecaptcha.default, {
  190. sitekey: this.CaptchaKey.value,
  191. onChange: function onChange(e) {
  192. return _this3.captchaComplete(e);
  193. }
  194. }), ","), this.activeButton.value ? _react.default.createElement("div", {
  195. className: 'row center-children'
  196. }, _react.default.createElement("div", {
  197. className: "col s" + this.button.columns
  198. }, _react.default.createElement(_reactMaterialize.Button, {
  199. type: "submit",
  200. className: this.button.class,
  201. waves: "light"
  202. }, this.button.value || "Submit"))) : ''), _get(_getPrototypeOf(FormModule.prototype), "render", this).call(this));
  203. }
  204. }]);
  205. return FormModule;
  206. }(_modularityFront.default.Module); //[{label:"Email",type:"email",placeholder:"",columns:12,disabled:false},
  207. // {label:"Password",type:"password",placeholder:"",columns:12,disabled:false}]
  208. FormModule.EditPosition = "top-right";
  209. FormModule.Inputs = {
  210. FormInputs: {
  211. type: Module.Types.Object,
  212. defaultValue: {
  213. "email": {
  214. type: "email",
  215. placeholder: "Email",
  216. columns: 12
  217. },
  218. "password": {
  219. type: "password",
  220. placeholder: "Password",
  221. columns: 12
  222. }
  223. }
  224. },
  225. activeButton: {
  226. type: Module.Types.Boolean
  227. },
  228. button: {
  229. type: Module.Types.Text,
  230. defaultValue: "Submit",
  231. class: "empty",
  232. columns: 12
  233. },
  234. withCaptcha: {
  235. type: Module.Types.Boolean
  236. },
  237. CaptchaKey: {
  238. type: Module.Types.Text
  239. }
  240. };
  241. FormModule.Outputs = {
  242. //
  243. formData: {},
  244. onSubmit: {}
  245. };
  246. FormModule.Category = "sxoinas";
  247. var _default = FormModule;
  248. exports.default = _default;