UNPKG

@folo/forms

Version:

> React components combined of @folo/layout & @folo/values

461 lines (387 loc) 21.3 kB
var React = require('react'); var Registry = require('@folo/store/src'); var registry = (function init() { var registry = new Registry(); return registry; })(); function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); } 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; } function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); } function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); } function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; } function _iterableToArrayLimit(arr, i) { if (typeof Symbol === "undefined" || !(Symbol.iterator in Object(arr))) return; var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; } function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; } function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; } var BLUR = "blur"; /** * manage value updates for all cell types * all controlled */ var Core = function Core(_ref) { var Component = _ref.coreComponent, initValue = _ref.initValue, nameRef = _ref.nameRef, groupName = _ref.groupName, valueRef = _ref.valueRef, isInput = _ref.isInput, storeID = _ref.storeID, onBlurProps = _ref.onBlur, onChangeProps = _ref.onChange, children = _ref.children, rest = _objectWithoutProperties(_ref, ["coreComponent", "initValue", "nameRef", "groupName", "valueRef", "isInput", "storeID", "onBlur", "onChange", "children"]); var _React$useState = React.useState(initValue), _React$useState2 = _slicedToArray(_React$useState, 2), localValue = _React$useState2[0], setValue = _React$useState2[1]; React.useEffect(function () { registry.subscribe({ nameRef: nameRef, initValue: initValue, groupName: groupName, storeID: storeID }, setValue); }, [initValue]); function eventHandler(e) { var newValue = e.target[valueRef], type = e.type; if (type === BLUR && typeof onBlurProps === "function") { // trigger onBlur coming form props onBlurProps(e); } else { // update local value while the change is happening // don't notify the global store yet setValue(newValue); if (typeof onChangeProps === "function") { onChangeProps(e); } } if (!isInput || type === BLUR) { // inform the store with the new changes we have here // only when bur or change happens in non-input element registry.updater({ nameRef: nameRef, newValue: newValue, groupName: groupName, storeID: storeID }); } } return /*#__PURE__*/React.createElement(Component, _extends({}, _defineProperty({}, valueRef, localValue), { onChange: eventHandler, onBlur: eventHandler }, rest), children); }; var VALUE = "value"; var CHECKED = "checked"; var TEXT = "text"; var INPUT = "input"; var SELECT = "select"; var LIST = "list"; var CHECKBOX = "checkbox"; var RADIO = "radio"; /** * Gets the cell type * returns booleans type flag. * * @param {string} type * @param {boolean} checked * @param {string} value * @return {{isInput:boolean, valueRef: string, initValue: string||boolean, RecommendedComponent: string }} */ function cellRecognizer(_ref) { var type = _ref.type, checked = _ref.checked, value = _ref.value; // only true when cell is button var isInput = false; // input or select var RecommendedComponent = INPUT; // value ref to the element: value or checked; depends on the type var valueRef = VALUE; // is it boolean or string; depends on the type var initValue = value; if (type === SELECT || type === LIST) { RecommendedComponent = SELECT; } else if (type === CHECKBOX || type === RADIO) { valueRef = CHECKED; initValue = checked; } else { isInput = true; } return { isInput: isInput, valueRef: valueRef, initValue: initValue, RecommendedComponent: RecommendedComponent }; } function _extends$1() { _extends$1 = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends$1.apply(this, arguments); } function _objectWithoutProperties$1(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose$1(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; } function _objectWithoutPropertiesLoose$1(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; } var Field = function Field(_ref) { var component = _ref.component, valueKey = _ref.valueKey, id = _ref.id, _ref$value = _ref.value, value = _ref$value === void 0 ? "" : _ref$value, _ref$checked = _ref.checked, checked = _ref$checked === void 0 ? false : _ref$checked, _ref$type = _ref.type, type = _ref$type === void 0 ? TEXT : _ref$type, _ref$groupName = _ref.groupName, groupName = _ref$groupName === void 0 ? null : _ref$groupName, storeID = _ref.storeID, children = _ref.children, rest = _objectWithoutProperties$1(_ref, ["component", "valueKey", "id", "value", "checked", "type", "groupName", "storeID", "children"]); var _cellRecognizer = cellRecognizer({ type: type, checked: checked, value: value }), valueRef = _cellRecognizer.valueRef, isInput = _cellRecognizer.isInput, initValue = _cellRecognizer.initValue, RecommendedComponent = _cellRecognizer.RecommendedComponent; var nameRef = valueKey || id || "".concat(new Date().getTime()); return /*#__PURE__*/React.createElement(Core, _extends$1({ valueRef: valueRef, initValue: initValue, isInput: isInput, groupName: groupName, nameRef: nameRef, coreComponent: component || RecommendedComponent, type: type, id: id, storeID: storeID }, rest), children); }; function _extends$2() { _extends$2 = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends$2.apply(this, arguments); } function _objectWithoutProperties$2(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose$2(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; } function _objectWithoutPropertiesLoose$2(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; } var Form = function Form(_ref) { var _ref$component = _ref.component, FormComponent = _ref$component === void 0 ? "form" : _ref$component, onSubmitProps = _ref.onSubmit, storeID = _ref.storeID, children = _ref.children, rest = _objectWithoutProperties$2(_ref, ["component", "onSubmit", "storeID", "children"]); React.useEffect(function () { return function cleanup() { registry.clear(storeID); }; }); function onSubmit(e) { e.preventDefault(); if (typeof onSubmitProps === "function") { onSubmitProps(e, registry.getDataByStoreID(storeID)); } } return /*#__PURE__*/React.createElement(FormComponent, _extends$2({ onSubmit: onSubmit }, rest), children); }; function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } 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); } } function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; } var AutoPositionCell = /*#__PURE__*/function () { function AutoPositionCell() { _classCallCheck(this, AutoPositionCell); // store cells number according to its name this.cellPositions = {}; this.biggestRowItem = 0; } /** * Auto set the row number * If we don't have row then take the highest value * depending on biggestRowItem which updated with each grid item * Otherwise set the row do you have and update biggestRowItem * * This helps to assign position value according to highest value * If we start from 10, the next will be 11 and so on. * * @param {object} GridItem - GridItem that should be register and calculated * @param {string} GridItem.key unique key generated in GridItem * @param {number} GridItem.row * @param {number} GridItem.toRow * @return {number} row position */ _createClass(AutoPositionCell, [{ key: "autoPosition", value: function autoPosition(_ref) { var key = _ref.key, row = _ref.row, toRow = _ref.toRow; var parseRow = parseInt(row, 10); var parseToRow = parseInt(toRow, 10); if (parseToRow && parseRow) { this.cellPositions[key] = parseRow; var bigger = parseToRow > parseRow ? parseToRow : parseRow; if (bigger > this.biggestRowItem) { this.biggestRowItem = bigger; } } else if (parseRow) { this.cellPositions[key] = parseRow; if (parseRow > this.biggestRowItem) { this.biggestRowItem = parseRow; } } else { this.biggestRowItem += 1; this.cellPositions[key] = this.biggestRowItem; if (parseToRow > this.biggestRowItem) { this.biggestRowItem = parseToRow; } } return this.cellPositions[key]; } }]); return AutoPositionCell; }(); var positionStore = (function init() { var positionStore = new AutoPositionCell(); return positionStore; })(); var GRID = "grid"; var STRETCH = "stretch"; var CENTER = "center"; var SPACE_BETWEEN = "space-between"; var FR = "1fr"; var AUTO_FIT = "auto-fit"; var AUTO = "auto"; var DEFAULT_GAP = "1em"; var ROW = "row"; var COLUMN = "column"; var DISPLAY_FLEX = "flex"; function _extends$3() { _extends$3 = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends$3.apply(this, arguments); } function _objectWithoutProperties$3(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose$3(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; } function _objectWithoutPropertiesLoose$3(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; } function location(from, to) { if (from !== null && to !== null) { return "".concat(from, " / ").concat(to); } return "".concat(from); } /** * For implicit grid * Takes column and row number * to inform the grid parent of total positions * * It collects numbers report it to Grid */ var GridItem = function GridItem(props) { var _props$component = props.component, CellComponent = _props$component === void 0 ? "div" : _props$component, _props$row = props.row, row = _props$row === void 0 ? null : _props$row, _props$toRow = props.toRow, toRow = _props$toRow === void 0 ? null : _props$toRow, _props$col = props.col, col = _props$col === void 0 ? 0 : _props$col, _props$toCol = props.toCol, toCol = _props$toCol === void 0 ? null : _props$toCol, _props$isCenter = props.isCenter, isCenter = _props$isCenter === void 0 ? false : _props$isCenter, _props$isHorizontal = props.isHorizontal, isHorizontal = _props$isHorizontal === void 0 ? true : _props$isHorizontal, _props$style = props.style; _props$style = _props$style === void 0 ? {} : _props$style; var _props$style$display = _props$style.display, display = _props$style$display === void 0 ? DISPLAY_FLEX : _props$style$display, fDirection = _props$style.flexDirection, aItems = _props$style.alignItems, otherStyle = _objectWithoutProperties$3(_props$style, ["display", "flexDirection", "alignItems"]), id = props.id, children = props.children, rest = _objectWithoutProperties$3(props, ["component", "row", "toRow", "col", "toCol", "isCenter", "isHorizontal", "style", "id", "children"]); var calculatedPosition = positionStore.autoPosition({ key: id || "".concat(new Date().getTime()), row: row, toRow: toRow }); var container = Object.assign({ display: display }, isHorizontal ? { flexDirection: fDirection || ROW, alignItems: aItems || CENTER } : { flexDirection: fDirection || COLUMN, alignItems: aItems || STRETCH }, { justifyContent: isCenter ? CENTER : SPACE_BETWEEN, gridRow: location(calculatedPosition, toRow), gridColumn: location(col, toCol) }, otherStyle); return /*#__PURE__*/React.createElement(CellComponent, _extends$3({ style: container }, rest), children); }; function _extends$4() { _extends$4 = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends$4.apply(this, arguments); } function _objectWithoutProperties$4(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose$4(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; } function _objectWithoutPropertiesLoose$4(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; } /** * call repeat() CSS function * depending on length, min,max * with some enhancements * * * @param {number} length row or column number in grid * @param {string} min minimum unit for row or column * @param {string} max maximum unit for row or column * @return {string} repeat() CSS function represents a repeated fragment of the track list */ function repeat(length, fixed, min, max) { return "repeat(".concat(length, ", ").concat(fixed || "minmax(".concat(min, ", ").concat(max, ")"), ")"); } var Grid = function Grid(props) { var _props$component = props.component, CellComponent = _props$component === void 0 ? "div" : _props$component, _props$col = props.col, col = _props$col === void 0 ? null : _props$col, colWidth = props.colWidth, _props$colMinWidth = props.colMinWidth, colMinWidth = _props$colMinWidth === void 0 ? AUTO : _props$colMinWidth, _props$colMaxWidth = props.colMaxWidth, colMaxWidth = _props$colMaxWidth === void 0 ? FR : _props$colMaxWidth, _props$row = props.row, row = _props$row === void 0 ? null : _props$row, rowWidth = props.rowWidth, _props$rowMinWidth = props.rowMinWidth, rowMinWidth = _props$rowMinWidth === void 0 ? AUTO : _props$rowMinWidth, _props$rowMaxWidth = props.rowMaxWidth, rowMaxWidth = _props$rowMaxWidth === void 0 ? FR : _props$rowMaxWidth, _props$isCenter = props.isCenter, isCenter = _props$isCenter === void 0 ? false : _props$isCenter, _props$style = props.style; _props$style = _props$style === void 0 ? {} : _props$style; var _props$style$display = _props$style.display, display = _props$style$display === void 0 ? GRID : _props$style$display, _props$style$alignIte = _props$style.alignItems, alignItems = _props$style$alignIte === void 0 ? isCenter ? CENTER : STRETCH : _props$style$alignIte, _props$style$justifyC = _props$style.justifyContent, justifyContent = _props$style$justifyC === void 0 ? col || colWidth ? SPACE_BETWEEN : isCenter ? CENTER : STRETCH : _props$style$justifyC, _props$style$gap = _props$style.gap, gap = _props$style$gap === void 0 ? DEFAULT_GAP : _props$style$gap, otherStyles = _objectWithoutProperties$4(_props$style, ["display", "alignItems", "justifyContent", "gap"]), children = props.children, rest = _objectWithoutProperties$4(props, ["component", "col", "colWidth", "colMinWidth", "colMaxWidth", "row", "rowWidth", "rowMinWidth", "rowMaxWidth", "isCenter", "style", "children"]); var style = Object.assign({ display: display }, row ? { gridTemplateRows: repeat(row, rowWidth, rowMinWidth, rowMaxWidth) } : rowWidth && { gridAutoRows: rowWidth }, colWidth && !col ? { gridAutoColumns: colWidth } : { gridTemplateColumns: repeat(col || AUTO_FIT, colWidth, colMinWidth, colMaxWidth) }, { alignItems: alignItems, justifyContent: justifyContent, gap: gap }, otherStyles); return /*#__PURE__*/React.createElement(CellComponent, _extends$4({ style: style }, rest), children); }; exports.Field = Field; exports.Form = Form; exports.Grid = Grid; exports.GridItem = GridItem;