@applicaster/zapp-react-native-ui-components 16.0.0-rc.34 → 16.0.0-rc.35

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,13 +1,16 @@
1
- import React from "react";
2
- import { TouchableOpacity, ViewStyle } from "react-native";
1
+ import React, { useCallback } from "react";
2
+ import { Pressable, ViewStyle } from "react-native";
3
+ import { PressedStateContext } from "../contexts/PressedStateContext";
3
4
 
4
5
  type Props = {
5
6
  children?: React.ReactNode;
6
7
  style?: ViewStyle | ViewStyle[];
8
+ pressedStyle?: ViewStyle | ViewStyle[];
7
9
  testID?: string;
8
10
  accessibilityLabel?: string;
9
11
  accessibilityHint?: string;
10
12
  onPress?: () => void;
13
+ testOnly_pressed?: boolean;
11
14
  };
12
15
 
13
16
  export function PressableView({
@@ -16,19 +19,36 @@ export function PressableView({
16
19
  testID,
17
20
  accessibilityLabel,
18
21
  accessibilityHint,
22
+ pressedStyle,
19
23
  onPress,
24
+ testOnly_pressed = false,
20
25
  }: Props) {
21
26
  return (
22
- <TouchableOpacity
23
- activeOpacity={1}
27
+ <Pressable
28
+ testOnly_pressed={testOnly_pressed}
24
29
  onPress={onPress}
25
30
  testID={testID}
26
31
  accessibilityLabel={accessibilityLabel}
27
32
  accessibilityHint={accessibilityHint}
28
33
  accessible={!!(testID || accessibilityLabel)}
29
- style={style}
34
+ style={useCallback(
35
+ ({ pressed }) => [
36
+ style,
37
+ pressed && {
38
+ opacity: 1,
39
+ },
40
+ pressed && pressedStyle,
41
+ ],
42
+ [style, pressedStyle]
43
+ )}
30
44
  >
31
- {children}
32
- </TouchableOpacity>
45
+ {({ pressed }) => {
46
+ return (
47
+ <PressedStateContext.Provider value={pressed}>
48
+ {children}
49
+ </PressedStateContext.Provider>
50
+ );
51
+ }}
52
+ </Pressable>
33
53
  );
34
54
  }
@@ -83,6 +83,10 @@ export const useTextLabel = ({ label, entry }): string => {
83
83
  return prepareHebrewText(label, isRTL);
84
84
  };
85
85
 
86
+ /**
87
+ * Uses `otherProps.state` ("focused" | "default") to select between `focusedStyles` and `normalStyles`.
88
+ * For mobile action buttons, map `usePressedState()` (boolean) to this state value (e.g. pressed ? "focused" : "default").
89
+ */
86
90
  export const withFocusedStyles = ({ style, otherProps }) => {
87
91
  const state = R.path(["state"], otherProps);
88
92
 
@@ -1,6 +1,6 @@
1
- import React from "react";
2
- import { StyleSheet, View, ViewStyle } from "react-native";
1
+ import React, { useCallback } from "react";
3
2
  import { PressableView } from "../../PressableView";
3
+ import { usePressedState } from "../../../hooks";
4
4
 
5
5
  import { ActionButtonController } from "../../ActionButtonsCore/components";
6
6
 
@@ -16,28 +16,142 @@ type ChildElementProps = {
16
16
  entry?: any;
17
17
  };
18
18
 
19
- const styles = StyleSheet.create({
20
- pressableView: {
21
- flexDirection: "row",
22
- overflow: "hidden",
23
- },
24
- maxWidthFull: {
25
- width: "100%",
26
- },
27
- innerWrapper: {
28
- flexDirection: "row",
29
- flexGrow: 0,
30
- flexShrink: 1,
31
- overflow: "hidden",
32
- },
33
- });
34
-
35
19
  const isValidElement = (
36
20
  child: React.ReactNode
37
21
  ): child is React.ReactElement<ChildElementProps> =>
38
22
  React.isValidElement(child);
39
23
 
40
- export const ActionButton = ({ style, children, action, entry, ...props }) => {
24
+ type ActionButtonContentProps = {
25
+ style: any;
26
+ children: React.ReactNode;
27
+ action: any;
28
+ focusedStyles: any;
29
+ entry: any;
30
+ props: Record<string, any>;
31
+ actionContext: any;
32
+ actionState: any;
33
+ isActive: boolean;
34
+ onPress: () => any;
35
+ };
36
+
37
+ const ActionButtonContent = ({
38
+ children,
39
+ action,
40
+ entry,
41
+ actionContext,
42
+ actionState,
43
+ isActive,
44
+ }: ActionButtonContentProps) => {
45
+ const isPressed = usePressedState();
46
+
47
+ const supportsEntryState =
48
+ typeof actionContext?.initialEntryState === "function";
49
+
50
+ const shouldRenderAsset = Boolean(
51
+ supportsEntryState ? actionState?.mobileButtonAssets : false
52
+ );
53
+
54
+ const shouldRenderLabel = Boolean(
55
+ supportsEntryState && resolveLabelText(actionState?.label)
56
+ );
57
+
58
+ const _cloneChildrenWithState = useCallback(
59
+ (nodes?: React.ReactNode): React.ReactNode => {
60
+ return React.Children.map(nodes, (child) => {
61
+ if (!isValidElement(child)) {
62
+ return child;
63
+ }
64
+
65
+ const role = child.props.mobileActionRole;
66
+
67
+ const nextChildren = _cloneChildrenWithState(child.props.children);
68
+
69
+ // Prevents of the asset rendering when the action doesn't provide asset for current entry state
70
+ if (role === "asset" && !shouldRenderAsset) {
71
+ return null;
72
+ }
73
+
74
+ // Prevents of the label rendering when the action doesn't provide label for current entry state.
75
+ // Also prevents of the label container when it doesn't have children to avoid unnecessary empty space.
76
+ if (role === "label" && !shouldRenderLabel) {
77
+ return null;
78
+ }
79
+
80
+ // Prevents of the label container rendering when it doesn't have children to avoid unnecessary empty space.
81
+ if (
82
+ role === "label_container" &&
83
+ React.Children.count(nextChildren) === 0
84
+ ) {
85
+ return null;
86
+ }
87
+
88
+ const nextProps: Partial<ChildElementProps> = {};
89
+
90
+ // Inject asset or uri to Asset component with role asset
91
+ if (role === "asset") {
92
+ const resolvedAsset = selectByAssetFlavour(
93
+ actionState,
94
+ action.flavour,
95
+ isActive
96
+ );
97
+
98
+ if (typeof resolvedAsset === "function") {
99
+ const AssetComponent = resolvedAsset as CellActionAssetComponent;
100
+
101
+ nextProps.asset = (
102
+ <AssetComponent
103
+ flavour={action.flavour || "flavour_1"}
104
+ width={action.width}
105
+ height={action.height}
106
+ />
107
+ );
108
+ } else {
109
+ nextProps.uri = resolvedAsset;
110
+ }
111
+ }
112
+
113
+ // Inject state and entry to Text component with role label
114
+ // Use isPressed state for mobile buttons to set focused state on mobile
115
+ if (role === "label") {
116
+ nextProps.state = isActive || isPressed ? "focused" : "default";
117
+ nextProps.entry = entry;
118
+ }
119
+
120
+ if (nextChildren !== child.props.children) {
121
+ nextProps.children = nextChildren;
122
+ }
123
+
124
+ return React.cloneElement(child, nextProps);
125
+ });
126
+ },
127
+ [
128
+ shouldRenderAsset,
129
+ shouldRenderLabel,
130
+ actionState,
131
+ action.flavour,
132
+ action.width,
133
+ action.height,
134
+ isActive,
135
+ isPressed,
136
+ entry,
137
+ ]
138
+ );
139
+
140
+ if (!shouldRenderAsset && !shouldRenderLabel) {
141
+ return null;
142
+ }
143
+
144
+ return _cloneChildrenWithState(children);
145
+ };
146
+
147
+ export const ActionButton = ({
148
+ style,
149
+ children,
150
+ action,
151
+ focusedStyles,
152
+ entry,
153
+ ...props
154
+ }) => {
41
155
  return (
42
156
  <ActionButtonController
43
157
  action={action}
@@ -48,126 +162,28 @@ export const ActionButton = ({ style, children, action, entry, ...props }) => {
48
162
  );
49
163
  }}
50
164
  >
51
- {({ actionContext, actionState, isActive, onPress }) => {
52
- const supportsEntryState =
53
- typeof actionContext?.initialEntryState === "function";
54
-
55
- const shouldRenderAsset = Boolean(
56
- supportsEntryState ? actionState?.mobileButtonAssets : false
57
- );
58
-
59
- const shouldRenderLabel = Boolean(
60
- supportsEntryState && resolveLabelText(actionState?.label)
61
- );
62
-
63
- const _cloneChildrenWithState = (
64
- nodes?: React.ReactNode
65
- ): React.ReactNode => {
66
- return React.Children.map(nodes, (child) => {
67
- if (!isValidElement(child)) {
68
- return child;
69
- }
70
-
71
- const role = child.props.mobileActionRole;
72
-
73
- const nextChildren = _cloneChildrenWithState(child.props.children);
74
-
75
- // Prevents of the asset rendering when the action doesn't provide asset for current entry state
76
- if (role === "asset" && !shouldRenderAsset) {
77
- return null;
78
- }
79
-
80
- // Prevents of the label rendering when the action doesn't provide label for current entry state.
81
- // Also prevents of the label container when it doesn't have children to avoid unnecessary empty space.
82
- if (role === "label" && !shouldRenderLabel) {
83
- return null;
84
- }
85
-
86
- // Prevents of the label container rendering when it doesn't have children to avoid unnecessary empty space.
87
- if (
88
- role === "label_container" &&
89
- React.Children.count(nextChildren) === 0
90
- ) {
91
- return null;
92
- }
93
-
94
- const nextProps: Partial<ChildElementProps> = {};
95
-
96
- // Inject asset or uri to Asset component with role asset
97
- if (role === "asset") {
98
- const resolvedAsset = selectByAssetFlavour(
99
- actionState,
100
- action.flavour,
101
- isActive
102
- );
103
-
104
- if (typeof resolvedAsset === "function") {
105
- const AssetComponent =
106
- resolvedAsset as CellActionAssetComponent;
107
-
108
- nextProps.asset = (
109
- <AssetComponent
110
- flavour={action.flavour || "flavour_1"}
111
- width={action.width}
112
- height={action.height}
113
- />
114
- );
115
- } else {
116
- nextProps.uri = resolvedAsset;
117
- }
118
- }
119
-
120
- // Inject state and entry to Text component with role label
121
- if (role === "label") {
122
- nextProps.state = isActive ? "focused" : "default";
123
- nextProps.entry = entry;
124
- }
125
-
126
- if (nextChildren !== child.props.children) {
127
- nextProps.children = nextChildren;
128
- }
129
-
130
- return React.cloneElement(child, nextProps);
131
- });
132
- };
133
-
134
- if (!shouldRenderAsset && !shouldRenderLabel) {
135
- return null;
136
- }
137
-
138
- return (
139
- <PressableView
140
- testID={props.testID || `${entry?.id}`}
141
- style={[
142
- isActive
143
- ? {
144
- ...(style as ViewStyle),
145
- ...(props.focusedStyles as ViewStyle),
146
- }
147
- : (style as ViewStyle),
148
- styles.pressableView,
149
- ]}
150
- onPress={onPress}
151
- accessibilityLabel={props.accessibilityLabel || `${entry?.id}`}
152
- accessibilityHint={props.accessibilityHint}
165
+ {(renderProps) => (
166
+ <PressableView
167
+ testOnly_pressed={props.testOnly_pressed}
168
+ testID={props.testID || `${entry?.id}`}
169
+ style={style}
170
+ pressedStyle={focusedStyles}
171
+ onPress={renderProps.onPress}
172
+ accessibilityLabel={props.accessibilityLabel || `${entry?.id}`}
173
+ accessibilityHint={props.accessibilityHint}
174
+ >
175
+ <ActionButtonContent
176
+ style={style}
177
+ action={action}
178
+ focusedStyles={focusedStyles}
179
+ entry={entry}
180
+ props={props}
181
+ {...renderProps}
153
182
  >
154
- <View
155
- // eslint-disable-next-line react-native/no-inline-styles
156
- style={{
157
- width: props.displayMode === "fill" ? "100%" : undefined,
158
- maxWidth: (style as ViewStyle).maxWidth,
159
- ...((style as ViewStyle).maxWidth
160
- ? styles.maxWidthFull
161
- : undefined),
162
- }}
163
- >
164
- <View style={[styles.innerWrapper, props.alignment]}>
165
- {_cloneChildrenWithState(children)}
166
- </View>
167
- </View>
168
- </PressableView>
169
- );
170
- }}
183
+ {children}
184
+ </ActionButtonContent>
185
+ </PressableView>
186
+ )}
171
187
  </ActionButtonController>
172
188
  );
173
189
  };
@@ -29,7 +29,6 @@ export const TextLabelsContainer = ({
29
29
  context: actionIdentifier,
30
30
  name: "label_1",
31
31
  },
32
- state: "default",
33
32
  mobileActionRole: "label",
34
33
  testID: testID ? `${testID}-label` : undefined,
35
34
  ...extraProps,
@@ -1,5 +1,5 @@
1
1
  import React from "react";
2
- import { Text as RNText, TouchableOpacity, View } from "react-native";
2
+ import { Pressable, Text as RNText, View } from "react-native";
3
3
  import { render, fireEvent } from "@testing-library/react-native";
4
4
  import { useActions } from "@applicaster/zapp-react-native-utils/reactHooks/actions";
5
5
 
@@ -8,6 +8,11 @@ import { elementMapper } from "../../../../elementMapper";
8
8
  import { defaultComponents } from "../../../index";
9
9
  import { MobileActionButtons } from "..";
10
10
  import { AssetComponent } from "../AssetComponent";
11
+ import { usePressedState } from "../../../../hooks/usePressedState";
12
+
13
+ jest.mock("../../../../hooks/usePressedState", () => ({
14
+ usePressedState: jest.fn(),
15
+ }));
11
16
 
12
17
  jest.mock("@applicaster/zapp-react-native-utils/reactHooks/actions", () => ({
13
18
  useActions: jest.fn(),
@@ -136,6 +141,7 @@ const baseNode = {
136
141
  borderColor: "rgba(4,4,4,1)",
137
142
  },
138
143
  testID: "mobile-action-button",
144
+ testOnly_pressed: false,
139
145
  },
140
146
  elements: [
141
147
  {
@@ -190,14 +196,20 @@ const baseNode = {
190
196
  describe("MobileActionButton", () => {
191
197
  beforeEach(() => {
192
198
  jest.clearAllMocks();
199
+ (usePressedState as jest.Mock).mockReturnValue(false);
193
200
  });
194
201
 
195
- const renderNode = (node = baseNode) =>
196
- render(
202
+ const renderNode = ({ node = baseNode, pressed = false } = {}) => {
203
+ if (pressed) {
204
+ node.props.testOnly_pressed = true;
205
+ }
206
+
207
+ return render(
197
208
  <React.Fragment>
198
209
  {elementMapper(defaultComponents)(node as never)}
199
210
  </React.Fragment>
200
211
  );
212
+ };
201
213
 
202
214
  it("renders nested image and text children through elementMapper", () => {
203
215
  mockUseActions.mockReturnValue(buildActionContext());
@@ -210,12 +222,13 @@ describe("MobileActionButton", () => {
210
222
  });
211
223
 
212
224
  it("applies focused styles when action entry state is active", () => {
225
+ (usePressedState as jest.Mock).mockReturnValue(true);
213
226
  mockUseActions.mockReturnValue(buildActionContext({ active: true }));
214
227
 
215
- const { getByTestId, getByText } = renderNode();
228
+ const { getByTestId, getByText } = renderNode({ pressed: true });
216
229
 
217
230
  expect(
218
- getByTestId("mobile-action-button").props.style.backgroundColor
231
+ getByTestId("mobile-action-button").props.style[2].backgroundColor
219
232
  ).toBe("rgba(3,3,3,1)");
220
233
 
221
234
  expect(getByText("Play").props.style).toEqual(
@@ -253,6 +266,8 @@ describe("MobileActionButton", () => {
253
266
  })
254
267
  );
255
268
 
269
+ node.props.testOnly_pressed = false;
270
+
256
271
  const { UNSAFE_getAllByType, UNSAFE_getByType, getByTestId, getByText } =
257
272
  render(
258
273
  <React.Fragment>
@@ -260,14 +275,14 @@ describe("MobileActionButton", () => {
260
275
  </React.Fragment>
261
276
  );
262
277
 
263
- const pressable = UNSAFE_getByType(TouchableOpacity);
278
+ const pressable = UNSAFE_getByType(Pressable);
264
279
  const label = getByText("Play");
265
280
  const asset = getByTestId("mobile_action_button_1-asset");
266
281
 
267
282
  expect(pressable.props.testID).toBe("mobile_action_button_1");
268
283
  expect(pressable.props.accessibilityLabel).toBe("entry-1");
269
284
 
270
- expect(pressable.props.style[0]).toMatchObject({
285
+ expect(pressable.props.style({ pressed: false })[0]).toMatchObject({
271
286
  backgroundColor: "rgba(1,1,1,1)",
272
287
  borderColor: "rgba(2,2,2,1)",
273
288
  borderWidth: 1,
@@ -278,15 +293,11 @@ describe("MobileActionButton", () => {
278
293
  height: 24,
279
294
  });
280
295
 
281
- expect(label.props.style).toEqual(
282
- expect.arrayContaining([
283
- expect.objectContaining({
284
- color: "rgba(10,10,10,1)",
285
- fontSize: 15,
286
- lineHeight: 24,
287
- }),
288
- ])
289
- );
296
+ expect(label.props.style[0]).toMatchObject({
297
+ color: "rgba(10,10,10,1)",
298
+ fontSize: 15,
299
+ lineHeight: 24,
300
+ });
290
301
 
291
302
  expect(UNSAFE_getAllByType(View).length).toBeGreaterThan(0);
292
303
  expect(UNSAFE_getAllByType(RNText).length).toBeGreaterThan(0);
@@ -324,7 +335,7 @@ describe("MobileActionButton", () => {
324
335
  buildActionContext({ mobileButtonAssets: MockAssetComponent })
325
336
  );
326
337
 
327
- const { getByTestId } = renderNode(nodeWithFlavour1);
338
+ const { getByTestId } = renderNode({ node: nodeWithFlavour1 });
328
339
 
329
340
  expect(MockAssetComponent).toHaveBeenCalled();
330
341
  expect(getByTestId("mobile-action-button-asset")).toBeTruthy();
@@ -339,7 +350,7 @@ describe("MobileActionButton", () => {
339
350
  buildActionContext({ mobileButtonAssets: MockAssetComponent })
340
351
  );
341
352
 
342
- renderNode(nodeWithFlavour1);
353
+ renderNode({ node: nodeWithFlavour1 });
343
354
 
344
355
  expect(MockAssetComponent).toHaveBeenCalledWith(
345
356
  expect.objectContaining({ flavour: "flavour_1" }),
@@ -356,7 +367,7 @@ describe("MobileActionButton", () => {
356
367
  buildActionContext({ mobileButtonAssets: MockAssetComponent })
357
368
  );
358
369
 
359
- renderNode(nodeWithFlavour2);
370
+ renderNode({ node: nodeWithFlavour2 });
360
371
 
361
372
  expect(MockAssetComponent).toHaveBeenCalledWith(
362
373
  expect.objectContaining({ flavour: "flavour_2" }),
@@ -369,7 +380,7 @@ describe("MobileActionButton", () => {
369
380
  buildActionContext({ mobileButtonAssets: undefined })
370
381
  );
371
382
 
372
- const { queryByTestId } = renderNode(nodeWithFlavour1);
383
+ const { queryByTestId } = renderNode({ node: nodeWithFlavour1 });
373
384
 
374
385
  expect(queryByTestId("mobile-action-button-asset")).toBeNull();
375
386
  });
@@ -385,7 +396,7 @@ describe("MobileActionButton", () => {
385
396
  })
386
397
  );
387
398
 
388
- const { getByTestId } = renderNode(nodeWithFlavour1);
399
+ const { getByTestId } = renderNode({ node: nodeWithFlavour1 });
389
400
 
390
401
  expect(getByTestId("mobile-action-button-asset")).toBeTruthy();
391
402
  });
@@ -0,0 +1,3 @@
1
+ import React from "react";
2
+
3
+ export const PressedStateContext = React.createContext<boolean>(false);
@@ -1 +1,3 @@
1
1
  export { useAsyncRendering } from "./useAsyncRendering";
2
+
3
+ export { usePressedState } from "./usePressedState";
@@ -0,0 +1,4 @@
1
+ import React from "react";
2
+ import { PressedStateContext } from "../contexts/PressedStateContext";
3
+
4
+ export const usePressedState = () => React.useContext(PressedStateContext);