@zuzjs/ui 0.3.4 → 0.3.6

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,241 +0,0 @@
1
- import Hashids from "hashids"
2
- import { nanoid } from "nanoid"
3
- import StyleCache from './css'
4
- import Cookies from 'js-cookie'
5
- import axios from 'axios'
6
- import {
7
- cssColors,
8
- cssProps, cssPropsDirect, cssPropsIgnore, cssPropsVals
9
- } from "./styles"
10
-
11
- const _Hashids = new Hashids('', 4)
12
- const _Alphabets = "#abcdefghijklmnopqrstuvwxyz0123456789"
13
-
14
- export const setCookie = (key : string, value : any, expiry? : number, host? : string) => Cookies.set(key, value, { expires: expiry || 7, domain: host || window.location.host })
15
-
16
- export const getCookie = (key : string) => key == `` ? Cookies.get() : Cookies.get(key) || null;
17
-
18
- export const removeCookie = (key : string) => Cookies.remove(key)
19
-
20
- export const uuid = (len=11) => nanoid(len)
21
-
22
- export const makeCSSValue = (k: any, v: any, o: any) => {
23
- let ignore = cssPropsIgnore.indexOf(o) == -1
24
- if(k in cssPropsDirect && ignore == true){
25
- return cssPropsDirect[k].indexOf(`__VALUE__`) > - 1 ?
26
- cssPropsDirect[k].replaceAll(`__VALUE__`, `${v}${"number" == typeof v ? `px` : ``}`)
27
- : cssPropsDirect[k];
28
- }
29
- let unit = "number" == typeof v && ignore ? `px` : ``;
30
- if(cssColors.indexOf(v) > -1){
31
- v = `var(--colors-${v.replaceAll(`.`, `-`)})`;
32
- unit = ``;
33
- }else if(v in cssPropsVals){
34
- v = cssPropsVals[v];
35
- }
36
- return `${k}:${v}${unit};`;
37
- }
38
-
39
- export const cleanProps = (props: {}, del=true) : {} => {
40
- const ignore = [`as`, `hover`, `bref`, `tag`,`onSubmit`,`method`,`should`,`options`,`required`]
41
- let items = { ...props }
42
- Object.keys(props).map(k => {
43
- if(k in cssProps || ignore.indexOf(k) > -1){
44
- if(del)
45
- delete items[k]
46
- else
47
- items[k] = undefined
48
- }
49
- })
50
- return items
51
- }
52
-
53
- export const buildCSS = (k : any, v : any) => {
54
- if(k in cssProps){
55
- return makeCSSValue(cssProps[k], v, k)
56
- }
57
- return ``
58
- }
59
-
60
- const _withStyle = (props, pseudo=``) : string => {
61
- if(!props) return ``
62
- let cx = [props.className || props.as || ``]
63
- const propRegexp = /\#|px|\(|\)|-|\s/g
64
- Object.keys(props).map(k => {
65
- if(k != 'children' && k in cssProps){
66
- const _css = buildCSS(k, props[k]).toString().replace(/;|:|\s/g, "")
67
- let _indices = 0
68
- for(let i = 0; i < _css.length; i++){ _indices += _Alphabets.indexOf(_css.charAt(i)) }
69
- let _cp = k.substring(0, 1);
70
- if(cssProps[k].indexOf("-") > -1){
71
- _cp = "";
72
- cssProps[k].split("-").map(c => _cp += c.substring(0, 1))
73
- }
74
- if(props[k].toString().indexOf("-") > -1){
75
- props[k].toString().split("-").map(c => _cp += c.substring(0, 1))
76
- }
77
- const _id = `${_cp}${_Hashids.encode(cssProps[k].length + _indices + (isNaN(parseInt(props[k])) ? 0 : parseInt(props[k])))}`
78
- // const _id = `${_cp}${cssProps[k]}-${props[k]}`
79
- cx.push(_id)
80
- StyleCache[_id + pseudo] = buildCSS(k, props[k])
81
- }
82
- })
83
- return cx.join(" ").trim()
84
- }
85
-
86
- export const withStyle = ( props ) : string => {
87
- const style = [_withStyle(props)]
88
- if("hover" in props){
89
- style.push(_withStyle(props.hover, `:hover`))
90
- }
91
- if("before" in props){
92
- style.push(_withStyle(props.hover, `:before`))
93
- }
94
- if("after" in props){
95
- style.push(_withStyle(props.hover, `:after`))
96
- }
97
- return style.join(style.length > 1 ? " " : "")
98
- }
99
-
100
- export const getStylesheet = () => Object.keys(StyleCache).map(ck => StyleCache[ck] != "" ? `.${ck}{${StyleCache[ck]}}` : `__`).filter(x => x != `__`)
101
-
102
- export const isEmail = (e : string) => {
103
- let reg = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/ ;
104
- return reg.test(e);
105
- }
106
-
107
- export const buildFormData = form => {
108
- if (!form || form.nodeName !== "FORM") {
109
- return;
110
- }
111
- var i, j, q = [];
112
- for (i = form.elements.length - 1; i >= 0; i = i - 1) {
113
- if (form.elements[i].name === "") {
114
- continue;
115
- }
116
- switch (form.elements[i].nodeName) {
117
- case 'INPUT':
118
- switch (form.elements[i].type) {
119
- case 'text':
120
- case 'hidden':
121
- case 'password':
122
- case 'button':
123
- case 'reset':
124
- case 'submit':
125
- q.push({ [form.elements[i].name] : encodeURIComponent(form.elements[i].value) });
126
- break;
127
- case 'checkbox':
128
- case 'radio':
129
- if (form.elements[i].checked) {
130
- q.push({ [form.elements[i].name] : encodeURIComponent(form.elements[i].value) });
131
- }
132
- break;
133
- case 'file':
134
- break;
135
- }
136
- break;
137
- case 'TEXTAREA':
138
- q.push({ [form.elements[i].name] : encodeURIComponent(form.elements[i].value) });
139
- break;
140
- case 'SELECT':
141
- switch (form.elements[i].type) {
142
- case 'select-one':
143
- q.push({ [form.elements[i].name] : encodeURIComponent(form.elements[i].value) });
144
- break;
145
- case 'select-multiple':
146
- for (j = form.elements[i].options.length - 1; j >= 0; j = j - 1) {
147
- if (form.elements[i].options[j].selected) {
148
- q.push({ [form.elements[i].name] : encodeURIComponent(form.elements[i].options[j].value) });
149
- }
150
- }
151
- break;
152
- }
153
- break;
154
- case 'BUTTON':
155
- switch (form.elements[i].type) {
156
- case 'reset':
157
- case 'submit':
158
- case 'button':
159
- q.push({ [form.elements[i].name] : encodeURIComponent(form.elements[i].value) });
160
- break;
161
- }
162
- break;
163
- }
164
- }
165
- return q //return q.join("&");
166
- }
167
-
168
- export const withRest = async (uri : string, data : object, timeout : number = 60, fd : object = null, progress? : Function, bearer : string = `__ha`) => {
169
- var Bearer = getCookie(bearer) || `${uuid(8)}^${uuid(8)}`;
170
- var isWindow = typeof window !== 'undefined'
171
- var cancelToken = null
172
- if(isWindow){
173
- window['__grabToken'] = axios.CancelToken.source();
174
- cancelToken = window['__grabToken'].token
175
- }
176
- if(fd){
177
- return new Promise((resolve, reject) => {
178
- axios({
179
- method: "post",
180
- url: uri,
181
- data: buildFormData(data),
182
- timeout: 1000 * timeout,
183
- cancelToken: cancelToken,
184
- headers: {
185
- 'Content-Type': 'multipart/form-data',
186
- 'Authorization': `Bearer ${Bearer}`
187
- },
188
- onUploadProgress: ev => {
189
- //TODO: Add progress
190
- }
191
- })
192
- .then(resp => {
193
- if(resp.data && "kind" in resp.data){
194
- resolve(resp.data)
195
- }else{
196
- reject(resp.data)
197
- }
198
- })
199
- .catch(err => reject(err));
200
- })
201
- }
202
- return new Promise((resolve, reject) => {
203
- axios.post(
204
- uri,
205
- {
206
- ...Cookies.get(),
207
- ...data,
208
- __poken: new Date().getTime() / 1000
209
- },
210
- {
211
- timeout: 1000 * timeout,
212
- headers: {
213
- 'Content-Type': 'application/json',
214
- 'Authorization': `Bearer ${Bearer}`
215
- },
216
- cancelToken: cancelToken
217
- }
218
- )
219
- .then(resp => {
220
- if(resp.data && "kind" in resp.data){
221
- resolve(resp.data)
222
- }else{
223
- reject(resp.data)
224
- }
225
- })
226
- .catch(err => reject(err));
227
- })
228
- }
229
-
230
- export const formatBytes = bytes => {
231
- var sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB'];
232
- if (bytes == 0) return '0 Byte';
233
- var i = Math.floor(Math.log(bytes) / Math.log(1024)),
234
- nx = bytes / Math.pow(1024, i);
235
- return nx.toFixed(2) + ' ' + sizes[i];
236
- }
237
-
238
- export const formatSeconds = (n: number): string => {
239
- let d = new Date(n * 1000).toISOString().slice(11, 19)
240
- return d.indexOf("00:") > -1 ? d.replace("00:", "") : d
241
- }
@@ -1,379 +0,0 @@
1
- const cssProps : { [key: string] : any } = {
2
- "ac": "align-content",
3
- "alignContent": "align-content",
4
-
5
- "aic": "aic",
6
- "ais": "ais",
7
- "aie": "aie",
8
- "nous": "nous",
9
- "nope": "nope",
10
- "ai": "align-items",
11
- "alignItems": "align-items",
12
-
13
- "ass": "ass",
14
- "asc": "asc",
15
- "ase": "ase",
16
- "alignSelf": "align-self",
17
- "all": "all",
18
- "animation": "animation",
19
- "animationDelay": "animation-delay",
20
- "animationDirection": "animation-direction",
21
- "animationDuration": "animation-duration",
22
- "animationFillMode": "animation-fill-mode",
23
- "animationIterationCount": "animation-iteration-count",
24
- "animationName": "animation-name",
25
- "animationPlayState": "animation-play-state",
26
- "animationTimingFunction": "animation-timing-function",
27
- "backfaceVisibility": "backface-visibility",
28
- "backgroundAttachment": "background-attachment",
29
- "backgroundBlendMode": "background-blend-mode",
30
- "backgroundClip": "background-clip",
31
-
32
- "bg": "background",
33
- "background": "background",
34
- "bgc": "background-color",
35
- "bgColor": "background-color",
36
- "backgroundColor": "background-color",
37
-
38
- "backgroundImage": "background-image",
39
- "backgroundOrigin": "background-origin",
40
- "backgroundPosition": "background-position",
41
- "backgroundRepeat": "background-repeat",
42
- "backgroundSize": "background-size",
43
- "border": "border",
44
- "borderBottom": "border-bottom",
45
- "borderBottomColor": "border-bottom-color",
46
- "borderBottomStyle": "border-bottom-style",
47
- "borderBottomWidth": "border-bottom-width",
48
- "borderCollapse": "border-collapse",
49
- "borderColor": "border-color",
50
- "borderImage": "border-image",
51
- "borderImageOutset": "border-image-outset",
52
- "borderImageRepeat": "border-image-repeat",
53
- "borderImageSlice": "border-image-slice",
54
- "borderImageSource": "border-image-source",
55
- "borderImageWidth": "border-image-width",
56
- "borderLeft": "border-left",
57
- "borderLeftColor": "border-left-color",
58
- "borderLeftStyle": "border-left-style",
59
- "borderLeftWidth": "border-left-width",
60
-
61
- //Radius
62
- "r": "border-radius",
63
- "br": "border-radius",
64
- "borderRadius": "border-radius",
65
- "brtl": "border-top-left-radius",
66
- "borderTopLeftRadius": "border-top-left-radius",
67
- "brtr": "border-top-right-radius",
68
- "borderTopRightRadius": "border-top-right-radius",
69
- "brbl": "border-bottom-left-radius",
70
- "borderBottomLeftRadius": "border-bottom-left-radius",
71
- "brbr": "border-bottom-right-radius",
72
- "borderBottomRightRadius": "border-bottom-right-radius",
73
-
74
- "borderRight": "border-right",
75
- "borderRightColor": "border-right-color",
76
- "borderRightStyle": "border-right-style",
77
- "borderRightWidth": "border-right-width",
78
- "borderSpacing": "border-spacing",
79
- "borderStyle": "border-style",
80
- "borderTop": "border-top",
81
- "borderTopColor": "border-top-color",
82
-
83
- "borderTopStyle": "border-top-style",
84
- "borderTopWidth": "border-top-width",
85
- "borderWidth": "border-width",
86
- "bottom": "bottom",
87
- "boxDecorationBreak": "box-decoration-break",
88
- "boxShadow": "box-shadow",
89
- "boxSizing": "box-sizing",
90
- "captionSide": "caption-side",
91
- "caretColor": "caret-color",
92
- "@charset": "@charset",
93
- "clear": "clear",
94
- "clip": "clip",
95
- "clipPath": "clip-path",
96
- "color": "color",
97
- "columnCount": "column-count",
98
- "columnFill": "column-fill",
99
- "columnGap": "column-gap",
100
- "colGap": "column-gap",
101
- "columnRule": "column-rule",
102
- "columnRuleColor": "column-rule-color",
103
- "columnRuleStyle": "column-rule-style",
104
- "columnRuleWidth": "column-rule-width",
105
- "columnSpan": "column-span",
106
- "columnWidth": "column-width",
107
- "colW": "column-width",
108
- "columns": "columns",
109
- "content": "content",
110
- "counterIncrement": "counter-increment",
111
- "counterReset": "counter-reset",
112
- "cursor": "cursor",
113
- "pointer": "pointer",
114
- "direction": "direction",
115
- "display": "display",
116
- "emptyCells": "empty-cells",
117
- "filter": "filter",
118
- "flex": "flex",
119
- "flexBasis": "flex-basis",
120
- "dir": "flex-direction",
121
- "flexDirection": "flex-direction",
122
- "flexFlow": "flex-flow",
123
- "flexGrow": "flex-grow",
124
- "flexShrink": "flex-shrink",
125
- "flexWrap": "flex-wrap",
126
- "float": "float",
127
- "font": "font",
128
- "fontFamily": "font-family",
129
- "fontKerning": "font-kerning",
130
- "size": "font-size",
131
- "fontSize": "font-size",
132
- "fontSizeAdjust": "font-size-adjust",
133
- "fontStretch": "font-stretch",
134
- "fontStyle": "font-style",
135
- "fontVariant": "font-variant",
136
- "bold": "bold",
137
- "b900": "b900",
138
- "b800": "b800",
139
- "b700": "b700",
140
- "fontWeight": "font-weight",
141
- "gap" : "gap",
142
- "grid": "grid",
143
- "gridArea": "grid-area",
144
- "gridAutoColumns": "grid-auto-columns",
145
- "gridAutoFlow": "grid-auto-flow",
146
- "gridAutoRows": "grid-auto-rows",
147
- "gridColumn": "grid-column",
148
- "gridColumnEnd": "grid-column-end",
149
- "gridColumnGap": "grid-column-gap",
150
- "gridColumnStart": "grid-column-start",
151
- "gridGap": "grid-gap",
152
- "gridRow": "grid-row",
153
- "gridRowEnd": "grid-row-end",
154
- "gridRowGap": "grid-row-gap",
155
- "gridRowStart": "grid-row-start",
156
- "gridTemplate": "grid-template",
157
- "gridTemplateAreas": "grid-template-areas",
158
- "gridTemplateColumns": "grid-template-columns",
159
- "gridTemplateRows": "grid-template-rows",
160
- "hangingPunctuation": "hanging-punctuation",
161
- "hyphens": "hyphens",
162
- "isolation": "isolation",
163
- "jcc": "jcc",
164
- "jcs": "jcs",
165
- "jce": "jce",
166
- "jc": "justify-content",
167
- "justifyContent": "justify-content",
168
- "left": "left",
169
- "letterSpacing": "letter-spacing",
170
- "lineHeight": "line-height",
171
- "listStyle": "list-style",
172
- "listStyleImage": "list-style-image",
173
- "listStylePosition": "list-style-position",
174
- "listStyleType": "list-style-type",
175
- "aspectRatio": "aspect-ratio",
176
- //Margin
177
- "m": "margin",
178
- "margin": "margin",
179
- "mb": "margin-bottom",
180
- "marginBottom": "margin-bottom",
181
- "ml": "margin-left",
182
- "marginLeft": "margin-left",
183
- "mr": "margin-right",
184
- "marginRight": "margin-right",
185
- "mt": "margin-top",
186
- "marginTop": "margin-top",
187
-
188
-
189
- //Height
190
- "h": "height",
191
- "height": "height",
192
- "minH": "min-height",
193
- "minHeight": "min-height",
194
- "maxH": "max-height",
195
- "maxHeight": "max-height",
196
-
197
- //Width
198
- "w": "width",
199
- "width": "width",
200
- "minW": "min-width",
201
- "minWidth": "min-width",
202
- "maxW": "max-width",
203
- "maxWidth": "max-width",
204
-
205
- "mixBlendMode": "mix-blend-mode",
206
- "objectFit": "object-fit",
207
- "objectPosition": "object-position",
208
- "opacity": "opacity",
209
- "order": "order",
210
- "outline": "outline",
211
- "outlineColor": "outline-color",
212
- "outlineOffset": "outline-offset",
213
- "outlineStyle": "outline-style",
214
- "outlineWidth": "outline-width",
215
- "overflow": "overflow",
216
- "overflowX": "overflow-x",
217
- "overflowY": "overflow-y",
218
-
219
-
220
- "p": "padding",
221
- "padding": "padding",
222
- "pb": "padding-bottom",
223
- "paddingBottom": "padding-bottom",
224
- "pl": "padding-left",
225
- "paddingLeft": "padding-left",
226
- "pr": "padding-right",
227
- "paddingRight": "padding-right",
228
- "pt": "padding-top",
229
- "paddingTop": "padding-top",
230
-
231
- "pageBreakAfter": "page-break-after",
232
- "pageBreakBefore": "page-break-before",
233
- "pageBreakInside": "page-break-inside",
234
- "perspective": "perspective",
235
- "perspectiveOrigin": "perspective-origin",
236
- "pointerEvents": "pointer-events",
237
-
238
- "rel":"rel",
239
- "abs":"abs",
240
- "fixed":"fixed",
241
- "sticky":"sticky",
242
- "pos": "position",
243
- "position": "position",
244
-
245
- "quotes": "quotes",
246
- "resize": "resize",
247
- "right": "right",
248
- "scrollBehavior": "scroll-behavior",
249
- "tabSize": "tab-size",
250
- "tableLayout": "table-layout",
251
-
252
- "align" : "text-align",
253
- "textAlign": "text-align",
254
- "textAlignLast": "text-align-last",
255
-
256
- "tdh": "tdh", //text-decoration: underline on hover
257
- "td": "text-decoration",
258
- "textDecoration": "text-decoration",
259
- "textDecorationColor": "text-decoration-color",
260
- "textDecorationLine": "text-decoration-line",
261
- "textDecorationStyle": "text-decoration-style",
262
- "textIndent": "text-indent",
263
- "textJustify": "text-justify",
264
- "textOverflow": "text-overflow",
265
- "textShadow": "text-shadow",
266
- "textTransform": "text-transform",
267
- "top": "top",
268
-
269
- "transform": "transform",
270
- "transform(2D)": "transform(2D)",
271
- "transformOrigin(twoValue syntax)": "transform-origin(two-value syntax)",
272
- "transformStyle": "transform-style",
273
- "transition": "transition",
274
- "transitionDelay": "transition-delay",
275
- "transitionDuration": "transition-duration",
276
- "transitionProperty": "transition-property",
277
- "transitionTimingFunction": "transition-timing-function",
278
- "unicodeBidi": "unicode-bidi",
279
- "userSelect": "user-select",
280
- "verticalAlign": "vertical-align",
281
- "visibility": "visibility",
282
- "weight" : "flex",
283
- "whiteSpace": "white-space",
284
- "ws": "white-space",
285
- "wordBreak": "word-break",
286
- "wordSpacing": "word-spacing",
287
- "wrap": "wrap",
288
- "textWrap": "textWrap",
289
- "wordWrap": "word-wrap",
290
- "writingMode": "writing-mode",
291
- "zIndex": "z-index",
292
- "backdropFilter": "backdrop-filter",
293
- "bgFilter": "backdrop-filter",
294
-
295
- //Custom
296
- "anim" : "anim",
297
- "fill" : "fill",
298
- "abc" : "abc",
299
- "fb" : "fb",
300
- "ph" : "ph",
301
- "pv" : "pv",
302
- "mv" : "mv",
303
- "mh" : "mh"
304
- }
305
-
306
- const cssPropsVals : { [key: string] : any } = {
307
- //Colors
308
- "primary" : 'var(--primary-color)',
309
- "c" : "center",
310
- //Flex Directions
311
- "cols" : "column",
312
- "colsr" : "column-reverse",
313
- "rows" : "row",
314
- "rowsr" : "row-reverse",
315
- //Positions
316
- "rel" : "relative",
317
- "abs" : "absolute",
318
- }
319
-
320
- const cssPropsDirect : { [key : string] : any } = {
321
- 'rel' : 'position: relative;',
322
- 'fixed' : 'position: fixed;',
323
- 'abs' : 'position: absolute;',
324
- 'sticky' : 'position: sticky;',
325
- 'flex' : 'display: flex;',
326
- 'fwrap' : 'flex-wrap: wrap;',
327
- 'aic' : 'align-items: center;',
328
- 'ais' : 'align-items: flex-start;',
329
- 'aie' : 'align-items: flex-end;',
330
- 'ass' : 'align-self: flex-start;',
331
- 'asc' : 'align-self: center;',
332
- 'ase' : 'align-self: flex-end;',
333
- 'jcc' : 'justify-content: center;',
334
- 'jcs' : 'justify-content: flex-start;',
335
- 'jce' : 'justify-content: flex-end;',
336
- 'grid' : 'display: grid;',
337
- 'fill' : 'top: 0px;left: 0px;right: 0px;bottom: 0px;',
338
- 'abc' : 'top: 50%;left: 50%;transform: translate(-50%, -50%);',
339
- 'block' : 'display: block;',
340
- 'bold' : "font-weight: bold;",
341
- 'b900' : "font-weight: 900;",
342
- 'b800' : "font-weight: 800;",
343
- 'b700' : "font-weight: 700;",
344
- 'wrap' : "word-wrap: break-word;white-space: nowrap;overflow: hidden;text-overflow: ellipsis;max-width: 99%;",
345
- 'textWrap' : "word-wrap: break-word;white-space: nowrap;overflow: hidden;text-overflow: ellipsis;max-width: 99%;",
346
- 'pointer' : "cursor: pointer;",
347
- 'fb' : 'font-family: var(--primary-font-bold);',
348
- 'ph' : 'padding-left: __VALUE__;padding-right: __VALUE__;',
349
- 'pv' : 'padding-bottom: __VALUE__;padding-top: __VALUE__;',
350
- 'mv' : 'margin-bottom: __VALUE__;margin-top: __VALUE__;',
351
- 'mh' : 'margin-left: __VALUE__;margin-right: __VALUE__;',
352
- 'anim' : 'transition:all __VALUE__s linear 0s;',
353
- 'nous' : 'user-select: none;',
354
- 'nope' : 'pointer-events: none;',
355
- 'tdn' : 'text-decoration: none;',
356
- 'aspectRatio' : 'aspect-ratio: __VALUE__;'
357
- }
358
-
359
- const cssPropsIgnore : string[] = [
360
- 'weight', `opacity`
361
- ]
362
-
363
- const _cssColors : string[] = [ `white`, `black`, `gray`, `red`, `orange`, `yellow`, `green`, `teal`, `blue`, `cyan`, `purple`, `pink`, `linkedin`, `facebook`, `messenger`, `whatsapp`, `twitter`, `telegram` ]
364
- const _cssColorsRange : string[] = [ `50`, `100`, `200`, `300`, `400`, `500`, `600`, `700`, `800`, `900` ]
365
- let cssColors : string[] = []
366
-
367
- if(cssColors.length == 0){
368
- _cssColors.map(c => {
369
- _cssColorsRange.map(r => cssColors.push(`${c}.${r}`));
370
- });
371
- }
372
-
373
- export {
374
- cssPropsDirect,
375
- cssProps,
376
- cssPropsVals,
377
- cssPropsIgnore,
378
- cssColors
379
- }
@@ -1,2 +0,0 @@
1
- export { default as useStore } from './useStore'
2
- export { default as useDispatch } from './useDispatch'
@@ -1,37 +0,0 @@
1
- import { useContext } from 'react'
2
- import AppContext from '../context'
3
-
4
- const useDispatch = (key? : string) => {
5
-
6
- const state = useContext(AppContext)
7
- const dispatch = state['dispatch'];
8
-
9
- const prepareState = (prevState, nextState) => {
10
- const nextKeys = Object.keys(nextState)
11
- nextKeys.map(k => {
12
- if(prevState[k] !== nextState[k]){
13
- prevState[k] = nextState[k]
14
- }
15
- });
16
- return {
17
- ...prevState,
18
- ...nextState
19
- }
20
- }
21
-
22
- if(key){
23
- return (payload = {}) => {
24
-
25
- dispatch({
26
- action: key,
27
- payload: {
28
- [key] : prepareState(state[key], payload)
29
- }
30
- });
31
- }
32
- }
33
-
34
- return dispatch;
35
- }
36
-
37
- export default useDispatch
@@ -1,25 +0,0 @@
1
- import { useContext } from 'react'
2
- import AppContext from '../context'
3
- import { STORE_KEY } from '../context/AppProvider'
4
-
5
- const useStore = (callback? : any, withFilter = true) => {
6
-
7
- let _state = useContext(AppContext)
8
- let state = withFilter ? {} : _state
9
-
10
- if(withFilter){
11
- const filters = ['defaultState', 'theme', 'dispatch', `${STORE_KEY}base`,`${STORE_KEY}forms`]
12
- Object.keys(_state).map(k => {
13
- if(filters.indexOf(k) == -1){
14
- state[k] = _state[k]
15
- }
16
- });
17
- }
18
-
19
- if("function" == typeof callback){
20
- return callback(state)
21
- }
22
- return state
23
- }
24
-
25
- export default useStore