@jobber/components-native 0.22.0 → 0.23.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.
@@ -0,0 +1,16 @@
1
+ import React, { useState } from "react";
2
+ import { View } from "react-native";
3
+ import { BaseSwitch } from "./components/BaseSwitch";
4
+ import { styles } from "./Switch.styles";
5
+ import { Text } from "../Text";
6
+ export function Switch(props) {
7
+ const switchProps = Object.assign(Object.assign({}, props), { accessibilityLabel: props.accessibilityLabel || props.label });
8
+ const [labelWidth, setLabelWidth] = useState();
9
+ return (React.createElement(View, { style: styles.container },
10
+ React.createElement(View, { style: styles.row },
11
+ props.label && (React.createElement(View, { style: styles.label, onLayout: event => setLabelWidth(event.nativeEvent.layout.width), testID: "switch-label-view" },
12
+ React.createElement(Text, { variation: props.disabled ? "disabled" : "base" }, props.label))),
13
+ React.createElement(BaseSwitch, Object.assign({}, switchProps))),
14
+ props.description && (React.createElement(View, { style: [styles.description, { maxWidth: labelWidth }], testID: "switch-description-view" },
15
+ React.createElement(Text, { level: "textSupporting", variation: "subdued" }, props.description)))));
16
+ }
@@ -0,0 +1,20 @@
1
+ import { StyleSheet } from "react-native";
2
+ import { tokens } from "../utils/design";
3
+ export const styles = StyleSheet.create({
4
+ container: {
5
+ marginTop: tokens["space-base"],
6
+ marginBottom: tokens["space-base"],
7
+ },
8
+ row: {
9
+ flexDirection: "row",
10
+ width: "100%",
11
+ },
12
+ label: {
13
+ flex: 1,
14
+ justifyContent: "center",
15
+ marginRight: tokens["space-small"],
16
+ },
17
+ description: {
18
+ marginTop: tokens["space-smaller"],
19
+ },
20
+ });
@@ -0,0 +1,54 @@
1
+ import React from "react";
2
+ import { Platform } from "react-native";
3
+ import { Switch } from "react-native-gesture-handler";
4
+ import { useFormController } from "../../../hooks";
5
+ import { tokens } from "../../../utils/design";
6
+ export function BaseSwitch({ value, defaultValue, onValueChange, disabled = false, accessibilityLabel, name, }) {
7
+ const { field } = useFormController({
8
+ name,
9
+ value: value !== null && value !== void 0 ? value : defaultValue,
10
+ });
11
+ const internalValue = value !== null && value !== void 0 ? value : field.value;
12
+ function getThumbColor() {
13
+ if (Platform.OS === "android") {
14
+ if (disabled) {
15
+ return tokens["color-disabled"];
16
+ }
17
+ else if (internalValue) {
18
+ return tokens["color-interactive"];
19
+ }
20
+ else {
21
+ return tokens["color-surface--background"];
22
+ }
23
+ }
24
+ return undefined; //use default iOS
25
+ }
26
+ function getTrackColors() {
27
+ if (Platform.OS === "android") {
28
+ return {
29
+ true: disabled
30
+ ? tokens["color-disabled--secondary"]
31
+ : tokens["color-green--lighter"],
32
+ false: disabled
33
+ ? tokens["color-disabled--secondary"]
34
+ : tokens["color-disabled"],
35
+ };
36
+ }
37
+ else {
38
+ //iOS
39
+ return {
40
+ true: tokens["color-interactive"],
41
+ false: tokens["color-surface--background"],
42
+ };
43
+ }
44
+ }
45
+ return (React.createElement(Switch, { value: internalValue, onValueChange: (val) => {
46
+ if (!disabled) {
47
+ onValueChange === null || onValueChange === void 0 ? void 0 : onValueChange(val);
48
+ field.onChange(val);
49
+ }
50
+ }, disabled: disabled, thumbColor: getThumbColor(), trackColor: getTrackColors(), ios_backgroundColor: tokens["color-surface--background"], accessibilityLabel: accessibilityLabel, accessibilityRole: "switch", accessibilityState: {
51
+ disabled: disabled,
52
+ checked: internalValue,
53
+ } }));
54
+ }
@@ -0,0 +1 @@
1
+ export { BaseSwitch } from "./BaseSwitch";
@@ -0,0 +1 @@
1
+ export { Switch } from "./Switch";
package/dist/src/index.js CHANGED
@@ -18,5 +18,6 @@ export * from "./InputPressable";
18
18
  export * from "./InputText";
19
19
  export * from "./ProgressBar";
20
20
  export * from "./StatusLabel";
21
+ export * from "./Switch";
21
22
  export * from "./Text";
22
23
  export * from "./Typography";