From f77a850fad6103b57373260cd77eb02852895304 Mon Sep 17 00:00:00 2001 From: Srirang Kalantri Date: Mon, 23 Feb 2026 12:14:55 +0530 Subject: [PATCH 1/2] feat: improve existing switch component functionality --- .../switch-native/src/Switch.tsx | 92 +++++++++++++------ .../switch-native/src/Switch.xml | 32 ++++--- .../src/__tests__/Switch.spec.tsx | 3 +- .../switch-native/src/assets/checked-dark.svg | 2 +- .../switch-native/src/assets/checked.svg | 2 +- .../switch-native/src/ui/Styles.ts | 2 - .../switch-native/typings/SwitchProps.d.ts | 4 + 7 files changed, 91 insertions(+), 46 deletions(-) diff --git a/packages/pluggableWidgets/switch-native/src/Switch.tsx b/packages/pluggableWidgets/switch-native/src/Switch.tsx index 3a8e4bc97..fdf6ef6b5 100644 --- a/packages/pluggableWidgets/switch-native/src/Switch.tsx +++ b/packages/pluggableWidgets/switch-native/src/Switch.tsx @@ -1,5 +1,5 @@ import { flattenStyles } from "@mendix/piw-native-utils-internal"; -import { ReactElement, useCallback } from "react"; +import React, { ReactElement, useCallback } from "react"; import { View, Text, Switch as SwitchComponent, Platform } from "react-native"; import { executeAction } from "@mendix/piw-utils-internal"; import { extractStyles } from "@mendix/pluggable-widgets-tools"; @@ -10,7 +10,7 @@ import { SwitchStyle, defaultSwitchStyle, CheckBoxInputType } from "./ui/Styles" export type Props = SwitchProps; export function Switch(props: Props): ReactElement { - const { label, labelOrientation, showLabel, name, onChange, booleanAttribute } = props; + const { label, labelOrientation, showLabel, name, onChange, booleanAttribute, labelPosition } = props; const combinedStyles = flattenStyles(defaultSwitchStyle, props.style); const styles = processStyles(combinedStyles); const horizontalOrientation = showLabel && labelOrientation === "horizontal"; @@ -39,36 +39,72 @@ export function Switch(props: Props): ReactElement { const labelValue = label?.status === "available" ? label.value : ""; + const switchElement = ( + + ); + + const labelElement = showLabel ? ( + + {labelValue} + + ) : null; + + const validationMessage = hasValidationMessage ? ( + + {booleanAttribute.validation} + + ) : null; + return ( - {showLabel ? ( - - {labelValue} - - ) : null} - - - {hasValidationMessage ? ( - - {booleanAttribute.validation} - - ) : null} - + {horizontalOrientation ? ( + // Horizontal layout: label and switch in a row, validation message below + <> + + {labelPosition === "right" ? ( + <> + {React.cloneElement(switchElement, { key: "switch" })} + {labelElement && React.cloneElement(labelElement, { key: "label" })} + + ) : ( + <> + {labelElement && React.cloneElement(labelElement, { key: "label" })} + {React.cloneElement(switchElement, { key: "switch" })} + + )} + + {validationMessage} + + ) : ( + // Vertical layout: label, switch, and validation message all in a column + <> + {labelElement && React.cloneElement(labelElement, { key: "label" })} + {React.cloneElement(switchElement, { key: "switch" })} + {validationMessage} + + )} ); } diff --git a/packages/pluggableWidgets/switch-native/src/Switch.xml b/packages/pluggableWidgets/switch-native/src/Switch.xml index 1bffec638..ae69d2e2f 100644 --- a/packages/pluggableWidgets/switch-native/src/Switch.xml +++ b/packages/pluggableWidgets/switch-native/src/Switch.xml @@ -1,7 +1,5 @@ - - + + Switch Toggle a boolean attribute. Display @@ -11,44 +9,52 @@ Attribute - + - + On change - + Show label - + Label - + Label orientation - + Horizontal Vertical + + Label position + The position of the label relative to the switch + + Left + Right + + - + - + - + diff --git a/packages/pluggableWidgets/switch-native/src/__tests__/Switch.spec.tsx b/packages/pluggableWidgets/switch-native/src/__tests__/Switch.spec.tsx index b48a70aa4..a675c075f 100644 --- a/packages/pluggableWidgets/switch-native/src/__tests__/Switch.spec.tsx +++ b/packages/pluggableWidgets/switch-native/src/__tests__/Switch.spec.tsx @@ -13,7 +13,8 @@ const createProps = (props?: Partial): Props => { showLabel: false, booleanAttribute: new EditableValueBuilder().withValue(false).build(), onChange: undefined, - style: [{ ...defaultSwitchStyle, ...style }] + style: [{ ...defaultSwitchStyle, ...style }], + labelPosition: "left" }; return { ...defaultProps, ...props }; diff --git a/packages/pluggableWidgets/switch-native/src/assets/checked-dark.svg b/packages/pluggableWidgets/switch-native/src/assets/checked-dark.svg index bd5e31bb6..d80ba755e 100644 --- a/packages/pluggableWidgets/switch-native/src/assets/checked-dark.svg +++ b/packages/pluggableWidgets/switch-native/src/assets/checked-dark.svg @@ -1,4 +1,4 @@ - + diff --git a/packages/pluggableWidgets/switch-native/src/assets/checked.svg b/packages/pluggableWidgets/switch-native/src/assets/checked.svg index 15afc0cea..6a828d278 100644 --- a/packages/pluggableWidgets/switch-native/src/assets/checked.svg +++ b/packages/pluggableWidgets/switch-native/src/assets/checked.svg @@ -1,4 +1,4 @@ - + diff --git a/packages/pluggableWidgets/switch-native/src/ui/Styles.ts b/packages/pluggableWidgets/switch-native/src/ui/Styles.ts index f97731217..c4af5569e 100644 --- a/packages/pluggableWidgets/switch-native/src/ui/Styles.ts +++ b/packages/pluggableWidgets/switch-native/src/ui/Styles.ts @@ -25,7 +25,6 @@ export interface SwitchStyle extends Style { export const defaultSwitchStyle: SwitchStyle = { container: { // All ViewStyle properties are allowed - paddingVertical: 4, justifyContent: "center" }, containerDisabled: { @@ -50,6 +49,5 @@ export const defaultSwitchStyle: SwitchStyle = { }, validationMessage: { // All TextStyle properties are allowed - alignSelf: "stretch" } }; diff --git a/packages/pluggableWidgets/switch-native/typings/SwitchProps.d.ts b/packages/pluggableWidgets/switch-native/typings/SwitchProps.d.ts index c14598ddd..88130d80c 100644 --- a/packages/pluggableWidgets/switch-native/typings/SwitchProps.d.ts +++ b/packages/pluggableWidgets/switch-native/typings/SwitchProps.d.ts @@ -8,6 +8,8 @@ import { ActionValue, DynamicValue, EditableValue } from "mendix"; export type LabelOrientationEnum = "horizontal" | "vertical"; +export type LabelPositionEnum = "left" | "right"; + export interface SwitchProps