sygnal 2.1.1 → 2.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -114,11 +114,11 @@ Alternatively, you can use any other bundler of your choice (Webpack, Babel, Rol
114
114
  ...,
115
115
  esbuild: {
116
116
  // add the import for Sygnal's JSX and Fragment handler to the top of each .jsx and .tsx page automatically
117
- jsxInject: `import { jsx, Fragment } from 'sygnal/jsx'`
117
+ jsxInject: `import { jsx, Fragment } from 'sygnal/jsx'`,
118
118
  // tell the transpiler to use Sygnal's 'jsx' funtion to render JSX elements
119
119
  jsxFactory: `jsx`,
120
120
  // tell the transpiler to use Sygnal's 'Fragment' funtion to render JSX fragments (<>...</>)
121
- jsxFragment: 'Fragment',
121
+ jsxFragment: 'Fragment'
122
122
  },
123
123
  }
124
124
  ```
@@ -26,10 +26,21 @@ function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Sy
26
26
  function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
27
27
 
28
28
  function collection(component, stateLense) {
29
- var combineList = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : ['DOM'];
30
- var globalList = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : ['EVENTS'];
31
- var stateSourceName = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : 'STATE';
29
+ var opts = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
30
+ var _opts$combineList = opts.combineList,
31
+ combineList = _opts$combineList === void 0 ? ['DOM'] : _opts$combineList,
32
+ _opts$globalList = opts.globalList,
33
+ globalList = _opts$globalList === void 0 ? ['EVENTS'] : _opts$globalList,
34
+ _opts$stateSourceName = opts.stateSourceName,
35
+ stateSourceName = _opts$stateSourceName === void 0 ? 'STATE' : _opts$stateSourceName,
36
+ _opts$domSourceName = opts.domSourceName,
37
+ domSourceName = _opts$domSourceName === void 0 ? 'DOM' : _opts$domSourceName,
38
+ _opts$container = opts.container,
39
+ container = _opts$container === void 0 ? 'div' : _opts$container,
40
+ _opts$containerClass = opts.containerClass,
41
+ containerClass = _opts$containerClass === void 0 ? '' : _opts$containerClass;
32
42
  return function (sources) {
43
+ var key = Date.now();
33
44
  var collectionOpts = {
34
45
  item: component,
35
46
  itemKey: function itemKey(state, ind) {
@@ -46,7 +57,27 @@ function collection(component, stateLense) {
46
57
  stream = _ref2[1];
47
58
 
48
59
  if (combineList.includes(name)) {
49
- acc[name] = instances.pickCombine(name);
60
+ var combined = instances.pickCombine(name);
61
+
62
+ if (name === domSourceName && container) {
63
+ acc.DOM = combined.map(function (children) {
64
+ return {
65
+ sel: container,
66
+ data: {
67
+ props: {
68
+ className: containerClass
69
+ }
70
+ },
71
+ children: children,
72
+ key: key,
73
+ text: undefined,
74
+ elm: undefined
75
+ };
76
+ });
77
+ } else {
78
+ console.warn('Collections without wrapping containers will fail in unpredictable ways when used inside JSX fragments');
79
+ acc[name] = combined;
80
+ }
50
81
  } else {
51
82
  acc[name] = instances.pickMerge(name);
52
83
  }
package/dist/component.js CHANGED
@@ -10,6 +10,8 @@ var _isolate = _interopRequireDefault(require("@cycle/isolate"));
10
10
 
11
11
  var _collection = _interopRequireDefault(require("./collection.js"));
12
12
 
13
+ var _switchable = _interopRequireDefault(require("./switchable.js"));
14
+
13
15
  var _state2 = require("@cycle/state");
14
16
 
15
17
  var _xstream = _interopRequireWildcard(require("xstream"));
@@ -788,9 +790,42 @@ var Component = /*#__PURE__*/function () {
788
790
  }
789
791
  };
790
792
  sink$ = (0, _collection["default"])(_factory, lense)(sources);
791
- } else if (isSwitchable) {} else {
793
+ } else if (isSwitchable) {
792
794
  var _objectSpread3;
793
795
 
796
+ var stateField = data.props.state;
797
+
798
+ if (typeof stateField === 'string') {
799
+ var _stream = _this8.sources[_this8.stateSourceName].stream;
800
+ propState = new _state2.StateSource(_stream.map(function (val) {
801
+ if (_typeof(val) !== 'object') {
802
+ console.error("Switchable Error: Invalid or undefined state");
803
+ return;
804
+ }
805
+
806
+ var newVal = val[stateField];
807
+
808
+ if (typeof newVal === 'undefined') {
809
+ console.warn("Specified state field '".concat(stateField, "' for switchable element not found"));
810
+ return;
811
+ }
812
+
813
+ return newVal;
814
+ }));
815
+ } else {
816
+ propState = _this8.sources[_this8.stateSourceName];
817
+ }
818
+
819
+ var switchableComponents = data.props.of;
820
+
821
+ var _sources = _objectSpread(_objectSpread({}, _this8.sources), {}, (_objectSpread3 = {}, _defineProperty(_objectSpread3, _this8.stateSourceName, propState), _defineProperty(_objectSpread3, "props$", props$), _defineProperty(_objectSpread3, "children$", children$), _objectSpread3));
822
+
823
+ sink$ = (0, _switchable["default"])(switchableComponents, props$.map(function (props) {
824
+ return props.current;
825
+ }))(_sources);
826
+ } else {
827
+ var _objectSpread4;
828
+
794
829
  var _lense = function _lense(props) {
795
830
  var state = props.state;
796
831
  if (typeof state === 'undefined') return props;
@@ -804,9 +839,9 @@ var Component = /*#__PURE__*/function () {
804
839
 
805
840
  propState = new _state2.StateSource(props$.map(_lense));
806
841
 
807
- var _sources = _objectSpread(_objectSpread({}, _this8.sources), {}, (_objectSpread3 = {}, _defineProperty(_objectSpread3, _this8.stateSourceName, propState), _defineProperty(_objectSpread3, "props$", props$), _defineProperty(_objectSpread3, "children$", children$), _objectSpread3));
842
+ var _sources2 = _objectSpread(_objectSpread({}, _this8.sources), {}, (_objectSpread4 = {}, _defineProperty(_objectSpread4, _this8.stateSourceName, propState), _defineProperty(_objectSpread4, "props$", props$), _defineProperty(_objectSpread4, "children$", children$), _objectSpread4));
808
843
 
809
- sink$ = factory(_sources);
844
+ sink$ = factory(_sources2);
810
845
  }
811
846
 
812
847
  var originalDOMSink = sink$[_this8.DOMSourceName];
@@ -1080,7 +1115,7 @@ function getComponents(currentElement, componentNames) {
1080
1115
  var sel = currentElement.sel;
1081
1116
  var isCollection = sel && sel.toLowerCase() === 'collection';
1082
1117
  var isSwitchable = sel && sel.toLowerCase() === 'switchable';
1083
- var isComponent = sel && ['collection', 'swtichable', 'sygnal-factory'].concat(_toConsumableArray(componentNames)).includes(currentElement.sel) || typeof ((_currentElement$data2 = currentElement.data) === null || _currentElement$data2 === void 0 ? void 0 : (_currentElement$data3 = _currentElement$data2.props) === null || _currentElement$data3 === void 0 ? void 0 : _currentElement$data3.sygnalFactory) === 'function';
1118
+ var isComponent = sel && ['collection', 'switchable', 'sygnal-factory'].concat(_toConsumableArray(componentNames)).includes(currentElement.sel) || typeof ((_currentElement$data2 = currentElement.data) === null || _currentElement$data2 === void 0 ? void 0 : (_currentElement$data3 = _currentElement$data2.props) === null || _currentElement$data3 === void 0 ? void 0 : _currentElement$data3.sygnalFactory) === 'function';
1084
1119
  var props = currentElement.data && currentElement.data.props || {};
1085
1120
  var children = currentElement.children || [];
1086
1121
  var found = {};
@@ -1094,20 +1129,17 @@ function getComponents(currentElement, componentNames) {
1094
1129
  if (!componentNames.includes(props.of)) throw new Error("Specified component for collection not found: ".concat(props.of));
1095
1130
  if (!props["for"] || !(typeof props["for"] === 'string' || Array.isArray(props["for"]))) console.warn("No valid array found in the 'value' property of collection ".concat(props.of, ": no collection components will be created"));
1096
1131
  currentElement.data.isCollection = true;
1097
- currentElement.data.component = props.component; // currentElement.data.componentArray = props.value
1098
1132
  } else if (isSwitchable) {
1099
- if (!props.components) throw new Error("Switchable element missing required 'components' property");
1100
- if (_typeof(props.components) !== 'object') throw new Error("Invalid 'components' property of switchable element: found ".concat(_typeof(props.components), " requires object mapping names to component factories"));
1101
- var switchableComponents = Object.values(props.components);
1133
+ if (!props.of) throw new Error("Switchable element missing required 'of' property");
1134
+ if (_typeof(props.of) !== 'object') throw new Error("Invalid 'components' property of switchable element: found ".concat(_typeof(props.of), " requires object mapping names to component factories"));
1135
+ var switchableComponents = Object.values(props.of);
1102
1136
  if (!switchableComponents.every(function (comp) {
1103
1137
  return typeof comp === 'function';
1104
- })) throw new Error("One or more invalid component factories for switchable element is not a valid component factory");
1105
- if (!props.current || typeof props.current !== 'string') throw new Error("Missing or invalid 'current' property for switchable element: found ".concat(_typeof(props.current), " requires string"));
1106
- var switchableComponentNames = Object.keys(props.components);
1138
+ })) throw new Error("One or more components provided to switchable element is not a valid component factory");
1139
+ if (!props.current || typeof props.current !== 'string' && typeof props.current !== 'function') throw new Error("Missing or invalid 'current' property for switchable element: found '".concat(_typeof(props.current), "' requires string or function"));
1140
+ var switchableComponentNames = Object.keys(props.of);
1107
1141
  if (!switchableComponentNames.includes(props.current)) throw new Error("Component '".concat(props.current, "' not found in switchable element"));
1108
1142
  currentElement.data.isSwitchable = true;
1109
- currentElement.data.components = props.components;
1110
- currentElement.data.currentComponent = props.current;
1111
1143
  } else {}
1112
1144
 
1113
1145
  found[id] = currentElement;
@@ -1138,7 +1170,7 @@ function injectComponents(currentElement, components, componentNames) {
1138
1170
  if ((_currentElement$data4 = currentElement.data) !== null && _currentElement$data4 !== void 0 && _currentElement$data4.componentsInjected) return currentElement;
1139
1171
  if (!isNestedElement && currentElement.data) currentElement.data.componentsInjected = true;
1140
1172
  var sel = currentElement.sel || 'NO SELECTOR';
1141
- var isComponent = ['collection', 'swtichable', 'sygnal-factory'].concat(_toConsumableArray(componentNames)).includes(sel) || typeof ((_currentElement$data5 = currentElement.data) === null || _currentElement$data5 === void 0 ? void 0 : (_currentElement$data6 = _currentElement$data5.props) === null || _currentElement$data6 === void 0 ? void 0 : _currentElement$data6.sygnalFactory) === 'function';
1173
+ var isComponent = ['collection', 'switchable', 'sygnal-factory'].concat(_toConsumableArray(componentNames)).includes(sel) || typeof ((_currentElement$data5 = currentElement.data) === null || _currentElement$data5 === void 0 ? void 0 : (_currentElement$data6 = _currentElement$data5.props) === null || _currentElement$data6 === void 0 ? void 0 : _currentElement$data6.sygnalFactory) === 'function';
1142
1174
  var isCollection = currentElement === null || currentElement === void 0 ? void 0 : (_currentElement$data7 = currentElement.data) === null || _currentElement$data7 === void 0 ? void 0 : _currentElement$data7.isCollection;
1143
1175
  var isSwitchable = currentElement === null || currentElement === void 0 ? void 0 : (_currentElement$data8 = currentElement.data) === null || _currentElement$data8 === void 0 ? void 0 : _currentElement$data8.isSwitchable;
1144
1176
  var props = currentElement.data && currentElement.data.props || {};
@@ -1153,6 +1185,8 @@ function injectComponents(currentElement, components, componentNames) {
1153
1185
  delete currentElement.elm;
1154
1186
  currentElement.children = _component;
1155
1187
  return currentElement;
1188
+ } else if (isSwitchable) {
1189
+ return _component;
1156
1190
  } else {
1157
1191
  return _component;
1158
1192
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "sygnal",
3
- "version": "2.1.1",
3
+ "version": "2.2.0",
4
4
  "description": "An intuitive framework for building fast and small components or applications based on Cycle.js",
5
5
  "main": "./dist/index.js",
6
6
  "exports": {
Binary file
package/sygnal-2.1.0.tgz DELETED
Binary file