diff options
Diffstat (limited to 'apps/mobile/lib/utils.ts')
| -rw-r--r-- | apps/mobile/lib/utils.ts | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/apps/mobile/lib/utils.ts b/apps/mobile/lib/utils.ts index 88283f01..bfb6c9ed 100644 --- a/apps/mobile/lib/utils.ts +++ b/apps/mobile/lib/utils.ts @@ -5,3 +5,40 @@ import { twMerge } from "tailwind-merge"; export function cn(...inputs: ClassValue[]) { return twMerge(clsx(inputs)); } + +/** + * Merge props conditionally. + * + * @example + * ``` + * <View {...condProps( + * { condition: true, props: { className: "foo" } }, + * { condition: true, props: { style: { margin: "10px" } } }, + * )} /> + * ``` + * results in: + * ``` + * <View className="foo" style={ margin: "10px" } /> + * ``` + * @example + * ``` + * <View style={condProps( + * { condition: true, color: "red" }, + * { condition: true, fontWeight: "bold" } + * )} /> + * ``` + * results in: + * ``` + * <View style={ color: "red", fontWeight: "bold" } /> + * ``` + */ +export function condProps( + ...condProps: { + condition: boolean; + props: Record<string, unknown>; + }[] +): Record<string, unknown> { + return condProps.reduce((acc, { condition, props }) => { + return condition ? { ...acc, ...props } : acc; + }, {}); +} |
