@lumx/core 3.10.1-alpha.7 → 3.10.1-alpha.8

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/js/utils.ts CHANGED
@@ -1,11 +1,25 @@
1
1
  import classNames from 'classnames';
2
+ // eslint-disable-next-line import/no-extraneous-dependencies
3
+ import React from 'react';
2
4
 
3
- import type React from 'react';
5
+ import isBoolean from 'lodash/isBoolean';
6
+ import isEmpty from 'lodash/isEmpty';
7
+ import kebabCase from 'lodash/kebabCase';
8
+ import noop from 'lodash/noop';
4
9
 
5
- /** Transform camelCase to kebab-case */
6
- function camelToKebabCase(str: string): string {
7
- return str.replace(/([a-z0-9])([A-Z])/g, '$1-$2').toLowerCase();
8
- }
10
+ /**
11
+ * Enhance isEmpty method to also works with numbers.
12
+ *
13
+ * @param value The value to check.
14
+ * @return Whether the input value is empty or != 0.
15
+ */
16
+ const _isEmpty = (value: any) => {
17
+ if (typeof value === 'number') {
18
+ return value === 0;
19
+ }
20
+
21
+ return isEmpty(value);
22
+ };
9
23
 
10
24
  /**
11
25
  * Get the basic CSS class for the given type.
@@ -22,9 +36,9 @@ export function getBasicClass({
22
36
  }: {
23
37
  prefix: string;
24
38
  type: string;
25
- value: string | number | boolean | undefined | null;
39
+ value: string | number | boolean | undefined;
26
40
  }): string {
27
- if (typeof value === 'boolean') {
41
+ if (isBoolean(value)) {
28
42
  if (!value) {
29
43
  // False value should not return a class.
30
44
  return '';
@@ -32,13 +46,13 @@ export function getBasicClass({
32
46
  const booleanPrefixes = ['has', 'is'];
33
47
 
34
48
  if (booleanPrefixes.some((booleanPrefix) => type.toString().startsWith(booleanPrefix))) {
35
- return `${prefix}--${camelToKebabCase(type)}`;
49
+ return `${prefix}--${kebabCase(type)}`;
36
50
  }
37
51
 
38
- return `${prefix}--is-${camelToKebabCase(type)}`;
52
+ return `${prefix}--is-${kebabCase(type)}`;
39
53
  }
40
54
 
41
- return `${prefix}--${camelToKebabCase(type)}-${value}`;
55
+ return `${prefix}--${kebabCase(type)}-${value}`;
42
56
  }
43
57
 
44
58
  /**
@@ -52,22 +66,14 @@ export function getBasicClass({
52
66
  * be used in the classname to represent the value of the given prop.
53
67
  * @return All LumX basic CSS classes.
54
68
  */
55
- export function handleBasicClasses({
56
- prefix,
57
- ...props
58
- }: {
59
- prefix: string;
60
- [prop: string]: string | number | boolean | null | undefined;
61
- }): string {
69
+ export function handleBasicClasses({ prefix, ...props }: { prefix: string; [prop: string]: any }): string {
62
70
  const otherClasses: any = {};
63
- const propKeys = Object.keys(props);
64
- if (propKeys.length > 1) {
65
- for (const prop of propKeys) {
66
- if (props[prop]) {
67
- const className = getBasicClass({ prefix, type: prop, value: props[prop] });
68
- otherClasses[className] = true;
69
- }
70
- }
71
+ if (!isEmpty(props)) {
72
+ Object.keys(props).forEach((prop) => {
73
+ otherClasses[getBasicClass({ prefix, type: prop, value: props[prop] })] = isBoolean(props[prop])
74
+ ? props[prop]
75
+ : !_isEmpty(props[prop]);
76
+ });
71
77
  }
72
78
 
73
79
  return classNames(prefix, otherClasses);
@@ -84,7 +90,7 @@ declare type SwipeDirection = 'none' | 'up' | 'down' | 'left' | 'right';
84
90
  * @param handleSwipe Callback function.
85
91
  * @return Function to remove listeners.
86
92
  */
87
- export function detectSwipe(touchSurface: Element, handleSwipe?: (direction: SwipeDirection) => void) {
93
+ export function detectSwipe(touchSurface: Element, handleSwipe: (direction: SwipeDirection) => void = noop) {
88
94
  let distX: number;
89
95
  let distY: number;
90
96
  let startX: number;
@@ -135,7 +141,7 @@ export function detectSwipe(touchSurface: Element, handleSwipe?: (direction: Swi
135
141
  direction = distY < 0 ? 'up' : 'down';
136
142
  }
137
143
  }
138
- handleSwipe?.(direction);
144
+ handleSwipe(direction);
139
145
  evt.preventDefault();
140
146
  };
141
147
 
@@ -162,8 +168,8 @@ function isPassiveEventAvailable() {
162
168
  supportsPassiveOption = true;
163
169
  },
164
170
  });
165
- window.addEventListener('testPassiveEventSupport', () => {}, opts);
166
- window.removeEventListener('testPassiveEventSupport', () => {}, opts);
171
+ window.addEventListener('testPassiveEventSupport', noop, opts);
172
+ window.removeEventListener('testPassiveEventSupport', noop, opts);
167
173
  } catch (e) {
168
174
  // ignored
169
175
  }