@cleanweb/oore 1.2.0-alpha.5 → 1.2.0-alpha.7

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,6 +1,9 @@
1
1
  import type { ReactElement, ReactNode } from 'react';
2
2
  import type { IUseSlots, PotentialSlotComponent } from './types';
3
3
  export declare const isElementChild: (child: ReactNode) => child is ReactElement<any, any>;
4
- export declare const getComponentSlotName: (TargetComponent: PotentialSlotComponent) => string | number | symbol | undefined;
4
+ interface IGetSlotName {
5
+ (TargetComponent: PotentialSlotComponent, child?: ReactElement): string | undefined;
6
+ }
7
+ export declare const getComponentSlotName: IGetSlotName;
5
8
  export declare const useSlots: IUseSlots;
6
9
  export type { SlotNamedComponent, SlottedComponent, TSlotsRecord, PotentialSlotComponent, } from './types';
@@ -1,4 +1,13 @@
1
1
  "use strict";
2
+ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
3
+ if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
4
+ if (ar || !(i in from)) {
5
+ if (!ar) ar = Array.prototype.slice.call(from, 0, i);
6
+ ar[i] = from[i];
7
+ }
8
+ }
9
+ return to.concat(ar || Array.prototype.slice.call(from));
10
+ };
2
11
  var __importDefault = (this && this.__importDefault) || function (mod) {
3
12
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
13
  };
@@ -13,7 +22,7 @@ var isElementChild = function (child) {
13
22
  return false;
14
23
  };
15
24
  exports.isElementChild = isElementChild;
16
- var getComponentSlotName = function (TargetComponent) {
25
+ var getComponentSlotName = function (TargetComponent, child) {
17
26
  if (typeof TargetComponent === 'string') {
18
27
  return TargetComponent;
19
28
  }
@@ -23,11 +32,20 @@ var getComponentSlotName = function (TargetComponent) {
23
32
  else if ('displayName' in TargetComponent) {
24
33
  return TargetComponent.displayName;
25
34
  }
35
+ else if (child) {
36
+ var keyTypes = ['string', 'number', 'symbol'];
37
+ var slotName = child.props['data-slot-name'];
38
+ if (keyTypes.includes(typeof slotName)) {
39
+ return slotName;
40
+ }
41
+ else
42
+ return undefined;
43
+ }
26
44
  else
27
45
  return undefined;
28
46
  };
29
47
  exports.getComponentSlotName = getComponentSlotName;
30
- var useSlots = function (children, slotComponents) {
48
+ var useSlots = function (children, slotComponents, _requiredSlots) {
31
49
  var useMemo = react_1.default.useMemo;
32
50
  console.log({ slotComponents: slotComponents });
33
51
  var slotsAliasLookup = useMemo(function () {
@@ -49,6 +67,7 @@ var useSlots = function (children, slotComponents) {
49
67
  var slotNodes = {};
50
68
  var unmatchedChildren = [];
51
69
  var invalidChildren = [];
70
+ var requiredSlots = __spreadArray([], (_requiredSlots !== null && _requiredSlots !== void 0 ? _requiredSlots : []), true);
52
71
  react_1.default.Children.forEach(children, function (child) {
53
72
  if (!child) {
54
73
  invalidChildren.push(child);
@@ -65,17 +84,26 @@ var useSlots = function (children, slotComponents) {
65
84
  return;
66
85
  }
67
86
  var slotAlias = (function () {
68
- var ChildComponent = child.type;
69
- var slotName = (0, exports.getComponentSlotName)(ChildComponent);
87
+ var slotName = (0, exports.getComponentSlotName)(child.type, child);
70
88
  return slotName ? slotsAliasLookup[slotName] : null;
71
89
  })();
72
90
  console.log('match:', { key: slotAlias });
91
+ if (slotAlias && (typeof slotComponents[slotAlias] !== 'string')) {
92
+ if (slotComponents[slotAlias].isRequiredSlot) {
93
+ requiredSlots.push(slotAlias);
94
+ }
95
+ }
73
96
  if (slotAlias)
74
97
  slotNodes[slotAlias] = child;
75
98
  else
76
99
  unmatchedChildren.push(child);
77
100
  });
78
101
  console.log({ unmatchedChildren: unmatchedChildren, invalidChildren: invalidChildren });
102
+ requiredSlots.forEach(function (slotAlias) {
103
+ if (!slotNodes[slotAlias]) {
104
+ (0, errors_1.throwDevError)("Missing required slot \"".concat(String(slotAlias), "\"."));
105
+ }
106
+ });
79
107
  return [slotNodes, unmatchedChildren, invalidChildren];
80
108
  }, [children]);
81
109
  return result;
@@ -3,7 +3,7 @@ export type TComponent = JSXElementConstructor<any>;
3
3
  export type TSlotName = keyof any;
4
4
  export type TSlotAlias = keyof any;
5
5
  export type TSlotsRecord<TKey extends TSlotAlias = TSlotAlias> = {
6
- [Key in TKey]: (string | SlotComponent);
6
+ [Key in TKey]: string | SlotComponent;
7
7
  };
8
8
  export type DisplayNamedComponent<TComponentArg extends TComponent = TComponent, TDisplayNameArg extends string = string> = TComponentArg & {
9
9
  displayName: TDisplayNameArg;
@@ -11,7 +11,9 @@ export type DisplayNamedComponent<TComponentArg extends TComponent = TComponent,
11
11
  export type SlotNamedComponent<TComponentArg extends TComponent = TComponent, TSlotNameArg extends TSlotName = TSlotName> = TComponentArg & {
12
12
  slotName: TSlotNameArg;
13
13
  };
14
- export type SlotComponent<TComponentArg extends TComponent = TComponent> = SlotNamedComponent<TComponentArg> | DisplayNamedComponent<TComponentArg>;
14
+ export type SlotComponent<TComponentArg extends TComponent = TComponent> = (SlotNamedComponent<TComponentArg> | DisplayNamedComponent<TComponentArg>) & {
15
+ isRequiredSlot?: boolean;
16
+ };
15
17
  export type SlottedComponent<TComponentArg extends TComponent = TComponent, TSlotAliasArg extends TSlotAlias = TSlotAlias, TSlotsRecordArg extends TSlotsRecord<TSlotAliasArg> = TSlotsRecord<TSlotAliasArg>> = TComponentArg & {
16
18
  Slots: TSlotsRecordArg;
17
19
  };
@@ -24,6 +26,6 @@ export type TUseSlotsResult<TSlotAliasArg extends TSlotAlias = TSlotAlias> = Rea
24
26
  invalidChildren: any[]
25
27
  ]>;
26
28
  export interface IUseSlots {
27
- <TSlotAliasArg extends TSlotAlias = TSlotAlias>(children: ReactNode, slotComponents: TSlotsRecord<TSlotAliasArg>): TUseSlotsResult<TSlotAliasArg>;
29
+ <TSlotAliasArg extends TSlotAlias = TSlotAlias>(children: ReactNode, slotComponents: TSlotsRecord<TSlotAliasArg>, requiredSlots?: TSlotAliasArg[]): TUseSlotsResult<TSlotAliasArg>;
28
30
  }
29
31
  export type PotentialSlotComponent = string | SlotComponent | TComponent;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cleanweb/oore",
3
- "version": "1.2.0-alpha.5",
3
+ "version": "1.2.0-alpha.7",
4
4
  "description": "A library of helpers for writing cleaner React function components with object-oriented patterns.",
5
5
  "engines": {
6
6
  "node": ">=20"