cpk-ui 0.0.7 → 0.1.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.
@@ -137,14 +137,22 @@ export function Button({ testID, type = 'solid', color = 'primary', size = 'medi
137
137
  type,
138
138
  loading,
139
139
  ]);
140
+ function resolveStyle(style) {
141
+ if (Array.isArray(style)) {
142
+ return style.find((s) => !!s);
143
+ }
144
+ return style || undefined;
145
+ }
146
+ const viewStyle = resolveStyle(compositeStyles.container);
147
+ const textStyle = resolveStyle(compositeStyles.text);
140
148
  const ChildView = (_jsxs(_Fragment, { children: [cloneElemWithDefaultColors({
141
149
  element: startElement,
142
- backgroundColor: compositeStyles.container?.[0]?.backgroundColor,
143
- color: compositeStyles.text?.[0]?.color,
150
+ backgroundColor: viewStyle?.backgroundColor,
151
+ color: textStyle?.color,
144
152
  }), !text || typeof text === 'string' ? (_jsx(Text, { style: compositeStyles.text, children: text })) : (text), cloneElemWithDefaultColors({
145
153
  element: endElement,
146
- backgroundColor: compositeStyles.container?.[0]?.backgroundColor,
147
- color: compositeStyles.text?.[0]?.color,
154
+ backgroundColor: viewStyle?.backgroundColor,
155
+ color: textStyle?.color,
148
156
  })] }));
149
157
  return (_jsx(TouchableHighlight, { activeOpacity: activeOpacity, delayPressIn: 30, disabled: innerDisabled || loading || !onPress, hitSlop: hitSlop, onPress: onPress, ref: Platform.select({
150
158
  web: ref,
@@ -285,12 +285,24 @@ export function Button({
285
285
  ],
286
286
  );
287
287
 
288
+ function resolveStyle<T>(
289
+ style: StyleProp<T>
290
+ ): T | undefined {
291
+ if (Array.isArray(style)) {
292
+ return style.find((s): s is T => !!s);
293
+ }
294
+ return style as T || undefined;
295
+ }
296
+
297
+ const viewStyle = resolveStyle<ViewStyle>(compositeStyles.container);
298
+ const textStyle = resolveStyle<TextStyle>(compositeStyles.text);
299
+
288
300
  const ChildView = (
289
301
  <>
290
302
  {cloneElemWithDefaultColors({
291
303
  element: startElement,
292
- backgroundColor: compositeStyles.container?.[0]?.backgroundColor,
293
- color: compositeStyles.text?.[0]?.color,
304
+ backgroundColor: viewStyle?.backgroundColor,
305
+ color: textStyle?.color,
294
306
  })}
295
307
  {!text || typeof text === 'string' ? (
296
308
  <Text style={compositeStyles.text}>{text}</Text>
@@ -299,8 +311,8 @@ export function Button({
299
311
  )}
300
312
  {cloneElemWithDefaultColors({
301
313
  element: endElement,
302
- backgroundColor: compositeStyles.container?.[0]?.backgroundColor,
303
- color: compositeStyles.text?.[0]?.color,
314
+ backgroundColor: viewStyle?.backgroundColor,
315
+ color: textStyle?.color,
304
316
  })}
305
317
  </>
306
318
  );
@@ -3,13 +3,13 @@ import { forwardRef, isValidElement, useCallback, useMemo, useRef, useState, } f
3
3
  import { Platform, Text, TextInput, TouchableWithoutFeedback, View, } from 'react-native';
4
4
  import { useHover } from 'react-native-web-hooks';
5
5
  import { css } from '@emotion/native';
6
- import { useCPK } from '../../../providers';
7
6
  import { Icon } from '../Icon/Icon';
8
7
  import { cloneElemWithDefaultColors } from '../../../utils/guards';
9
8
  import { Global } from '@emotion/react';
9
+ import { useTheme } from '../../../providers/ThemeProvider';
10
10
  export const EditText = forwardRef(({ testID, textInputProps, style, styles, label, error, startElement, endElement, multiline = false, value = '', placeholder, placeholderColor, onChange, onChangeText, onFocus, onBlur, onSubmitEditing, numberOfLines, maxLength, hideCounter = false, autoComplete, autoCapitalize = 'none', secureTextEntry = false, editable = true, direction = 'column', decoration = 'underline', colors = {}, required = false, }, ref) => {
11
11
  EditText.displayName = 'EditText';
12
- const { theme } = useCPK();
12
+ const { theme } = useTheme();
13
13
  const webRef = useRef(null);
14
14
  const [focused, setFocused] = useState(false);
15
15
  const defaultInputRef = useRef(null);
@@ -1,3 +1,5 @@
1
+ import '@testing-library/jest-native/extend-expect';
2
+
1
3
  import React from 'react';
2
4
  import {Text} from 'react-native';
3
5
  import RNWebHooks from 'react-native-web-hooks';
@@ -7,7 +9,7 @@ import {act, fireEvent, render} from '@testing-library/react-native';
7
9
  import {createComponent} from '../../../../test/testUtils';
8
10
  import type {EditTextProps} from './EditText';
9
11
  import {EditText} from './EditText';
10
- import { light } from '../../../utils/colors';
12
+ import {light} from '../../../utils/colors';
11
13
 
12
14
  jest.mock('react-native-web-hooks', () => ({
13
15
  useHover: () => false,
@@ -33,9 +35,7 @@ describe('[EditText]', () => {
33
35
  describe('label', () => {
34
36
  it('renders label text', async () => {
35
37
  testingLib = render(Component({label: 'label text'}));
36
-
37
38
  const label = testingLib.getByText('label text');
38
-
39
39
  expect(label).toBeTruthy();
40
40
  });
41
41
 
@@ -23,10 +23,10 @@ import {
23
23
  import {useHover} from 'react-native-web-hooks';
24
24
  import {css} from '@emotion/native';
25
25
 
26
- import {useCPK} from '../../../providers';
27
26
  import {Icon} from '../Icon/Icon';
28
27
  import {cloneElemWithDefaultColors} from '../../../utils/guards';
29
28
  import {Global} from '@emotion/react';
29
+ import {useTheme} from '../../../providers/ThemeProvider';
30
30
 
31
31
  export type EditTextStyles = {
32
32
  container?: StyleProp<ViewStyle>;
@@ -148,7 +148,7 @@ export const EditText = forwardRef<TextInput, EditTextProps>(
148
148
  ): JSX.Element => {
149
149
  EditText.displayName = 'EditText';
150
150
 
151
- const {theme} = useCPK();
151
+ const {theme} = useTheme();
152
152
  const webRef = useRef<View>(null);
153
153
  const [focused, setFocused] = useState(false);
154
154
  const defaultInputRef = useRef(null);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cpk-ui",
3
- "version": "0.0.7",
3
+ "version": "0.1.0",
4
4
  "main": "index",
5
5
  "react-native": "index",
6
6
  "module": "index",
package/utils/guards.js CHANGED
@@ -1,12 +1,12 @@
1
- import React, { cloneElement } from "react";
2
- import { Button, Image, Pressable, Text, TextInput, TouchableHighlight, TouchableOpacity, TouchableWithoutFeedback, View, } from "react-native";
3
- import { Icon } from "../components/uis/Icon/Icon";
1
+ import React, { cloneElement } from 'react';
2
+ import { Button, Image, Pressable, Text, TextInput, TouchableHighlight, TouchableOpacity, TouchableWithoutFeedback, View, } from 'react-native';
3
+ import { Icon } from '../components/uis/Icon/Icon';
4
4
  const getRootElementStyleType = (element) => {
5
5
  if (React.isValidElement(element)) {
6
6
  if (element.type === Text ||
7
7
  element.type === TextInput ||
8
8
  element.type === Icon) {
9
- return "TextStyle";
9
+ return 'TextStyle';
10
10
  }
11
11
  if (element.type === Image ||
12
12
  element.type === View ||
@@ -15,10 +15,10 @@ const getRootElementStyleType = (element) => {
15
15
  element.type === TouchableOpacity ||
16
16
  element.type === Pressable ||
17
17
  element.type === TouchableWithoutFeedback) {
18
- return "ViewStyle";
18
+ return 'ViewStyle';
19
19
  }
20
20
  }
21
- return "unknown";
21
+ return 'unknown';
22
22
  };
23
23
  /**
24
24
  * This function applies default colors to cloned element.
@@ -30,10 +30,10 @@ export const cloneElemWithDefaultColors = ({ element, color, backgroundColor, st
30
30
  return element
31
31
  ? cloneElement(element, {
32
32
  style: [
33
- getRootElementStyleType(element) === "TextStyle" && {
33
+ getRootElementStyleType(element) === 'TextStyle' && {
34
34
  color,
35
35
  },
36
- getRootElementStyleType(element) === "ViewStyle" && {
36
+ getRootElementStyleType(element) === 'ViewStyle' && {
37
37
  borderColor: color,
38
38
  backgroundColor: backgroundColor,
39
39
  },
package/utils/guards.ts CHANGED
@@ -1,5 +1,5 @@
1
- import React, { cloneElement } from "react";
2
- import type { ViewStyle } from "react-native";
1
+ import React, {cloneElement} from 'react';
2
+ import type {ColorValue, ViewStyle} from 'react-native';
3
3
  import {
4
4
  Button,
5
5
  Image,
@@ -10,19 +10,19 @@ import {
10
10
  TouchableOpacity,
11
11
  TouchableWithoutFeedback,
12
12
  View,
13
- } from "react-native";
14
- import { Icon } from "../components/uis/Icon/Icon";
13
+ } from 'react-native';
14
+ import {Icon} from '../components/uis/Icon/Icon';
15
15
 
16
16
  const getRootElementStyleType = (
17
- element: JSX.Element
18
- ): "TextStyle" | "ViewStyle" | "unknown" => {
17
+ element: JSX.Element,
18
+ ): 'TextStyle' | 'ViewStyle' | 'unknown' => {
19
19
  if (React.isValidElement(element)) {
20
20
  if (
21
21
  element.type === Text ||
22
22
  element.type === TextInput ||
23
23
  element.type === Icon
24
24
  ) {
25
- return "TextStyle";
25
+ return 'TextStyle';
26
26
  }
27
27
 
28
28
  if (
@@ -34,11 +34,11 @@ const getRootElementStyleType = (
34
34
  element.type === Pressable ||
35
35
  element.type === TouchableWithoutFeedback
36
36
  ) {
37
- return "ViewStyle";
37
+ return 'ViewStyle';
38
38
  }
39
39
  }
40
40
 
41
- return "unknown";
41
+ return 'unknown';
42
42
  };
43
43
 
44
44
  type CloneElemColorsParams = {
@@ -51,13 +51,13 @@ type CloneElemColorsParams = {
51
51
  * If not passed, default color will be applied.
52
52
  * Invalid if element is ViewStyle.
53
53
  */
54
- color?: string;
54
+ color?: ColorValue;
55
55
  /**
56
56
  * Background color to be applied.
57
57
  * If not passed, default color will be applied.
58
58
  * Invalid if element is TextStyle.
59
59
  */
60
- backgroundColor?: string;
60
+ backgroundColor?: ColorValue;
61
61
  /**
62
62
  * Extra style to be applied.
63
63
  */
@@ -79,14 +79,14 @@ export const cloneElemWithDefaultColors = ({
79
79
  return element
80
80
  ? cloneElement(element, {
81
81
  style: [
82
- getRootElementStyleType(element) === "TextStyle" && {
82
+ getRootElementStyleType(element) === 'TextStyle' && {
83
83
  color,
84
84
  },
85
- getRootElementStyleType(element) === "ViewStyle" && {
85
+ getRootElementStyleType(element) === 'ViewStyle' && {
86
86
  borderColor: color,
87
87
  backgroundColor: backgroundColor,
88
88
  },
89
- { ...style, ...element.props.style },
89
+ {...style, ...element.props.style},
90
90
  ],
91
91
  })
92
92
  : null;