anu-verzum 2.3.0 → 2.3.2

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.
@@ -1,5 +1,5 @@
1
1
  import { Component } from './Component';
2
- import { AnuElement, Props } from '../elements';
2
+ import { AnuChild, Props } from '../elements';
3
3
  type UserActionEvent = {
4
4
  type: string;
5
5
  keyCode?: number | null;
@@ -26,7 +26,7 @@ declare class AnulyticsProvider extends Component<AnulyticsProviderProps> {
26
26
  componentDidMount(): void;
27
27
  componentWillUnmount(): void;
28
28
  handleVisibilityChange(): void;
29
- render(): AnuElement | AnuElement[] | null;
29
+ render(): AnuChild[] | null;
30
30
  }
31
31
  export default AnulyticsProvider;
32
32
  export declare const __testing: {
@@ -5,6 +5,7 @@ Object.defineProperty(exports, "__esModule", {
5
5
  });
6
6
  exports.trackStateChange = exports.trackRouteChange = exports.trackEvent = exports.default = exports.__testing = void 0;
7
7
  var _Component = require("./Component");
8
+ var _elements = require("../elements");
8
9
  var _serverApi = _interopRequireDefault(require("../../server-api/server-api"));
9
10
  function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
10
11
  const EventTypes = {
@@ -206,9 +207,9 @@ class AnulyticsProvider extends _Component.Component {
206
207
  }
207
208
  }
208
209
  render() {
209
- const children = this.props.children;
210
+ const children = (0, _elements.toChildArray)(this.props.children);
210
211
  try {
211
- if (!children || children.length !== 1) {
212
+ if (children.length !== 1) {
212
213
  throw new Error('Provider must have one child element!');
213
214
  }
214
215
  return children;
@@ -1,4 +1,4 @@
1
- import { AnuElement, Props } from '../elements';
1
+ import { AnuNode, Props } from '../elements';
2
2
  export declare abstract class Component<P extends Record<string, any> = Props, S extends Record<string, any> = Record<string, any>> {
3
3
  props: P;
4
4
  state: S;
@@ -7,7 +7,7 @@ export declare abstract class Component<P extends Record<string, any> = Props, S
7
7
  static isAnuComponent: boolean;
8
8
  constructor(props: P, context?: Record<string, any>);
9
9
  setState(partialState?: Partial<S> | ((prevState: S, prevProps: P) => Partial<S>)): void;
10
- abstract render(): AnuElement | AnuElement[] | string | number | boolean | null | undefined;
10
+ abstract render(): AnuNode;
11
11
  componentDidMount(): void;
12
12
  componentDidUpdate(_prevProps: P, _prevState: S): void;
13
13
  componentWillUnmount(): void;
@@ -1,5 +1,5 @@
1
1
  import { Component } from './Component';
2
- import { AnuElement, Props, ComponentConstructor } from '../elements';
2
+ import { AnuChild, Props, ComponentConstructor } from '../elements';
3
3
  type StoreShape = {
4
4
  getState: () => any;
5
5
  dispatch: (action: any) => any;
@@ -14,7 +14,7 @@ export interface ConnectorProviderProps extends Props {
14
14
  declare class Provider extends Component<ConnectorProviderProps> {
15
15
  store: StoreShape;
16
16
  constructor(props: ConnectorProviderProps, context?: Record<string, any>);
17
- render(): AnuElement | AnuElement[] | null;
17
+ render(): AnuChild[] | null;
18
18
  }
19
19
  declare const Connector: {
20
20
  connect: <TState = any, TOwnProps extends Record<string, any> = Record<string, any>, TStateProps extends Record<string, any> = Record<string, any>, TDispatchProps extends Record<string, any> = Record<string, any>>(mapStateToProps?: MapStateToProps<TState, TOwnProps, TStateProps> | null, mapDispatchToProps?: MapDispatchToProps<any, TOwnProps, TDispatchProps> | null) => (WrappedComponent: ComponentConstructor<TStateProps & TDispatchProps & TOwnProps>) => ComponentConstructor;
@@ -40,9 +40,9 @@ class Provider extends _Component.Component {
40
40
  });
41
41
  }
42
42
  render() {
43
- const children = this.props.children;
43
+ const children = (0, _elements.toChildArray)(this.props.children);
44
44
  try {
45
- if (!children || children.length !== 1) {
45
+ if (children.length !== 1) {
46
46
  throw new Error('Provider must have one child element!');
47
47
  }
48
48
  return children;
@@ -6,6 +6,7 @@ Object.defineProperty(exports, "__esModule", {
6
6
  exports.createContext = void 0;
7
7
  var _utils = require("../../misc/utils");
8
8
  var _Component = require("./Component");
9
+ var _elements = require("../elements");
9
10
  const createContext = context => {
10
11
  const defaultContext = {
11
12
  value: context
@@ -37,9 +38,9 @@ const createContext = context => {
37
38
  }
38
39
  }
39
40
  render() {
40
- const children = this.props.children;
41
+ const children = (0, _elements.toChildArray)(this.props.children);
41
42
  try {
42
- if (!children || children.length !== 1) {
43
+ if (children.length !== 1) {
43
44
  throw new Error('Context Component must have exactly one child element!');
44
45
  }
45
46
  return children;
@@ -80,23 +81,21 @@ const createContext = context => {
80
81
  }
81
82
  }
82
83
  render() {
83
- const children = this.props.children;
84
+ const children = (0, _elements.toChildArray)(this.props.children);
84
85
  try {
85
- if (!children || children.length !== 1) {
86
+ if (children.length !== 1) {
86
87
  throw new Error('Context Component must have exactly one child element!');
87
88
  }
88
89
  const provider = resolveProvider(this);
89
90
  this.subscribeTo(provider);
90
91
  const value = provider ? provider.value : defaultContext.value;
91
- const {
92
- type
93
- } = children[0];
92
+ const renderChild = children[0];
94
93
  const childProps = {
95
94
  value,
96
95
  defaultContext
97
96
  };
98
- if (typeof type === 'function') {
99
- return type(childProps);
97
+ if (typeof renderChild === 'function') {
98
+ return renderChild(childProps);
100
99
  } else {
101
100
  throw new Error('Context component child element must be a function!');
102
101
  }
@@ -12,7 +12,7 @@ const defaultFeatures = {};
12
12
  const FeaturesContext = (0, _Context.createContext)(defaultFeatures);
13
13
  const resolveDefault = defaultComponent => {
14
14
  if (defaultComponent && !Array.isArray(defaultComponent) && defaultComponent.type === _Fragment.Fragment) {
15
- return defaultComponent.props.children ?? null;
15
+ return (0, _elements.toChildArray)(defaultComponent.props.children);
16
16
  }
17
17
  return defaultComponent;
18
18
  };
@@ -1,5 +1,5 @@
1
1
  import { Component } from './Component';
2
- import { AnuElement } from '../elements';
2
+ import { AnuChild } from '../elements';
3
3
  export declare class Fragment extends Component {
4
- render(): AnuElement[];
4
+ render(): AnuChild[];
5
5
  }
@@ -5,11 +5,12 @@ Object.defineProperty(exports, "__esModule", {
5
5
  });
6
6
  exports.Fragment = void 0;
7
7
  var _Component = require("./Component");
8
+ var _elements = require("../elements");
8
9
  class Fragment extends _Component.Component {
9
10
  render() {
10
- const children = this.props.children;
11
+ const children = (0, _elements.toChildArray)(this.props.children);
11
12
  try {
12
- if (!children || !children.length) {
13
+ if (!children.length) {
13
14
  throw new Error('Fragment must have at least one child element!');
14
15
  }
15
16
  return children;
@@ -22,3 +22,5 @@ export type AnuElement<P = Props, T extends ElementType = ElementType> = {
22
22
  ref: Ref<any> | null;
23
23
  };
24
24
  export declare const createElement: (type: ElementType, config: Props | null, ...args: any[]) => AnuElement;
25
+ export declare const toChildArray: (children: AnuNode) => AnuChild[];
26
+ export declare const normalizeChildren: (children: AnuNode) => AnuElement[];
@@ -3,7 +3,7 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- exports.createElement = exports.TEXT_ELEMENT = exports.PORTAL_ELEMENT = void 0;
6
+ exports.toChildArray = exports.normalizeChildren = exports.createElement = exports.TEXT_ELEMENT = exports.PORTAL_ELEMENT = void 0;
7
7
  const TEXT_ELEMENT = exports.TEXT_ELEMENT = 'TEXT_ELEMENT';
8
8
  const PORTAL_ELEMENT = exports.PORTAL_ELEMENT = Symbol('portal');
9
9
  const createTextElement = value => ({
@@ -20,13 +20,10 @@ const createElement = (type, config, ...args) => {
20
20
  const props = Object.assign({}, config);
21
21
  delete props.key;
22
22
  delete props.ref;
23
- if (args.length > 0) {
24
- const rawChildren = [].concat(...args);
25
- props.children = rawChildren.filter(c => c !== null && c !== undefined && typeof c !== 'boolean').map(c => typeof c === 'function' ? createElement(c, {
26
- ...(c.props || {})
27
- }) : c instanceof Object ? c : createTextElement(c));
28
- } else {
29
- props.children = props.children ?? [];
23
+ if (args.length === 1) {
24
+ props.children = args[0];
25
+ } else if (args.length > 1) {
26
+ props.children = args;
30
27
  }
31
28
  return {
32
29
  type,
@@ -35,4 +32,20 @@ const createElement = (type, config, ...args) => {
35
32
  ref
36
33
  };
37
34
  };
38
- exports.createElement = createElement;
35
+ exports.createElement = createElement;
36
+ const flattenChildren = (children, acc = []) => {
37
+ if (Array.isArray(children)) {
38
+ for (const child of children) {
39
+ flattenChildren(child, acc);
40
+ }
41
+ } else if (children !== null && children !== undefined && typeof children !== 'boolean') {
42
+ acc.push(children);
43
+ }
44
+ return acc;
45
+ };
46
+ const toChildArray = children => flattenChildren(children);
47
+ exports.toChildArray = toChildArray;
48
+ const normalizeChildren = children => flattenChildren(children).map(c => typeof c === 'function' ? createElement(c, {
49
+ ...(c.props || {})
50
+ }) : c instanceof Object ? c : createTextElement(c));
51
+ exports.normalizeChildren = normalizeChildren;
@@ -204,7 +204,6 @@ const updateClassComponent = wipFiber => {
204
204
  const newChildElements = wipFiber.stateNode.render();
205
205
  reconcileChildrenArray(wipFiber, newChildElements);
206
206
  };
207
- const arrify = val => !val ? [] : Array.isArray(val) ? val : [val];
208
207
  const getTag = element => {
209
208
  if (element.type === _elements.PORTAL_ELEMENT) {
210
209
  return PORTAL;
@@ -218,7 +217,7 @@ const getTag = element => {
218
217
  return CLASS_COMPONENT;
219
218
  };
220
219
  const reconcileChildrenArray = (wipFiber, newChildElements) => {
221
- const elements = arrify(newChildElements);
220
+ const elements = (0, _elements.normalizeChildren)(newChildElements);
222
221
  const oldFiberMap = new Map();
223
222
  let oldFiber = wipFiber.alternate ? wipFiber.alternate.child : undefined;
224
223
  let oldIndex = 0;
package/dist/index.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import Utils from './misc/utils';
2
2
  import ServerAPI from './server-api/server-api';
3
- import { createElement, AnuElement, Props, Ref } from './core/elements';
3
+ import { createElement, AnuElement, AnuNode, Props, Ref } from './core/elements';
4
4
  import { createRef, render, createPortal } from './core/reconciler';
5
5
  import store from './store/store';
6
6
  import { createContext } from './core/components/Context';
@@ -29,7 +29,7 @@ declare const Anu: {
29
29
  };
30
30
  createContext: <T extends Record<string, any> = Record<string, any>>(context: T) => import("./core/components/Context").Context<T>;
31
31
  createElement: (type: import(".").ElementType, config: Props | null, ...args: any[]) => AnuElement;
32
- createPortal: (children: import(".").AnuNode, container: Element) => AnuElement;
32
+ createPortal: (children: AnuNode, container: Element) => AnuElement;
33
33
  createRef: <T = any>() => Ref<T>;
34
34
  Component: typeof Component;
35
35
  Fragment: typeof Fragment;
@@ -118,7 +118,7 @@ declare const Anu: {
118
118
  subscribe: (listener: () => void) => void;
119
119
  unsubscribe: (listener: () => void) => void;
120
120
  };
121
- render(): AnuElement | AnuElement[] | null;
121
+ render(): import(".").AnuChild[] | null;
122
122
  props: import("./core/components/Connector").ConnectorProviderProps;
123
123
  state: Record<string, any>;
124
124
  context: Record<string, any>;
@@ -172,7 +172,7 @@ declare global {
172
172
  interface Element extends AnuElement<any, any> {
173
173
  }
174
174
  interface ElementClass {
175
- render(): AnuElement | AnuElement[] | string | number | boolean | null | undefined;
175
+ render(): AnuNode;
176
176
  }
177
177
  interface ElementAttributesProperty {
178
178
  props: Record<string, unknown>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "anu-verzum",
3
- "version": "2.3.0",
3
+ "version": "2.3.2",
4
4
  "description": "A \"React-like\" UI library that supports JSX syntax, Redux-like state management, array-rendering, i18n, routing and many more.",
5
5
  "keywords": [
6
6
  "anu-verzum",