All files / packages/components/src input.base.tsx

100% Statements 8/8
83.33% Branches 15/18
75% Functions 3/4
100% Lines 8/8

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70                    8942x             10786x 10786x                             292x                               10786x 10786x       10786x             292x                
import type React from "react";
 
import { useBooleanState } from "~app/hooks/use-boolean-state";
import { Button } from "~components/button";
import { Icon } from "~components/icons";
import type { InputHandler, Props } from "~components/input";
import { getErrorState, getMutationLoading } from "~components/utils";
import type { ViewReactNode } from "~components/view";
import { View } from "~components/view";
 
export const usePasswordVisibility = ({
	type,
	endContent,
}: {
	type?: React.ComponentProps<"input">["type"];
	endContent?: ViewReactNode;
}) => {
	const [visible, { switchValue: switchVisible }] = useBooleanState();
	return {
		endContent: (
			<View className="flex-row gap-2">
				{endContent}
				{type === "password" ? (
					<Button variant="light" isIconOnly onPress={switchVisible}>
						<Icon name={visible ? "eye-off" : "eye"} className="size-6" />
					</Button>
				) : null}
			</View>
		),
		type: type === "password" && visible ? undefined : type,
	};
};
 
export const useMutationErrors = ({
	isDisabled,
	color,
	mutation,
	fieldError,
	description,
	continuousMutations,
}: Pick<
	Props,
	| "isDisabled"
	| "color"
	| "mutation"
	| "fieldError"
	| "description"
	| "continuousMutations"
>) => {
	const isMutationLoading = getMutationLoading(mutation);
	const { isWarning, isError, errors } = getErrorState({
		mutation,
		fieldError,
	});
	return {
		isDisabled: continuousMutations ? false : isMutationLoading || isDisabled,
		color: isWarning ? "warning" : isError ? "danger" : color,
		description: errors.join("\n") || description,
	};
};
 
export const emptyInputHandler: InputHandler = {
	focus: () => {
		/* empty */
	},
	blur: () => {
		/* empty */
	},
};