From 90b89024729519311e77481f98bef2cf60eb11df Mon Sep 17 00:00:00 2001 From: imikulec Date: Tue, 4 Apr 2023 15:45:54 +0200 Subject: [PATCH] #97 Add valueTransform prop to InputField --- libs/formik-elements/src/InputField.tsx | 23 ++++++++++++++++--- .../formik-elements/InputField.stories.tsx | 13 +++++++++++ 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/libs/formik-elements/src/InputField.tsx b/libs/formik-elements/src/InputField.tsx index 8dc09d2..49e71a2 100644 --- a/libs/formik-elements/src/InputField.tsx +++ b/libs/formik-elements/src/InputField.tsx @@ -17,7 +17,7 @@ import * as React from "react"; -import { useField, useFormikContext } from "formik"; +import { useField } from "formik"; import { Input, InputProps } from "@tiller-ds/form-elements"; import { ComponentTokens } from "@tiller-ds/theme"; @@ -42,6 +42,12 @@ export type InputFieldProps = { * Determines whether the components auto trim whitespace after typing */ autoTrim?: boolean; + + /** + * Function or a string that determines how the input value should be transformed when the user types into the input field. + * In case it's a function, it will be called every time the input value changes. The function can then modify the value as needed and return the modified value. + */ + valueTransform?: "uppercase" | "lowercase" | "capitalize" | ((value: string) => void); } & Omit & InputTokensProps; @@ -49,15 +55,26 @@ type InputTokensProps = { tokens?: ComponentTokens<"Input">; }; -export default function InputField({ name, autoTrim = true, ...props }: InputFieldProps) { +export default function InputField({ name, autoTrim = true, valueTransform, ...props }: InputFieldProps) { const [field, meta, helpers] = useField(name); const shouldValidate = useShouldValidate(); const initialError = useFormikBypass(name); const onChange = (e) => { - helpers.setValue(e.target.value, shouldValidate); + let transformedValue = e.target.value; + if (typeof valueTransform === "function") { + transformedValue = valueTransform(transformedValue); + } else if (valueTransform === "uppercase") { + transformedValue = transformedValue.toUpperCase(); + } else if (valueTransform === "lowercase") { + transformedValue = transformedValue.toLowerCase(); + } else if (valueTransform === "capitalize") { + transformedValue = transformedValue.charAt(0).toUpperCase() + transformedValue.slice(1); + } + helpers.setValue(transformedValue, shouldValidate); initialError.current = undefined; }; + const onReset = () => helpers.setValue("", shouldValidate); const onBlur = () => { diff --git a/storybook/src/formik-elements/InputField.stories.tsx b/storybook/src/formik-elements/InputField.stories.tsx index 4904bfb..0a09c86 100644 --- a/storybook/src/formik-elements/InputField.stories.tsx +++ b/storybook/src/formik-elements/InputField.stories.tsx @@ -81,6 +81,11 @@ export default { label: { name: "Label", control: "text" }, help: { name: "Help", control: "text" }, placeholder: { name: "Placeholder", control: "text" }, + valueTransform: { + name: "Transform Value", + options: ["uppercase", "lowercase", "capitalize"], + control: { type: "radio" }, + }, tooltipText: { name: "Tooltip Text (on hover)", control: "text" }, tooltipToggle: { name: "Toggle Tooltip (on/off)", control: { type: "boolean" } }, tooltipIcon: { name: "Tooltip Icon", control: { type: "select", options: iconTypes } }, @@ -117,6 +122,7 @@ export const InputFieldFactory = ({ className, useTokens, tokens, + valueTransform, }) => ( ); @@ -145,6 +152,7 @@ InputFieldFactory.args = { label: "Test Label", help: "", placeholder: "Test placeholder", + valueTransform: "lowercase", tooltipToggle: false, tooltipText: "Test tooltip content", tooltipIcon: "info", @@ -175,6 +183,10 @@ export const WithoutLabel = () => ; export const WithValue = () => } />; +export const WithTransformedValue = () => ( + } valueTransform="uppercase" /> +); + export const Disabled = () => } disabled={true} />; export const WithPlaceholder = (args, context) => ( @@ -270,6 +282,7 @@ const HideControls = { WithLabel.argTypes = HideControls; WithoutLabel.argTypes = HideControls; WithValue.argTypes = HideControls; +WithTransformedValue.argTypes = HideControls; Disabled.argTypes = HideControls; WithPlaceholder.argTypes = HideControls; WithHelp.argTypes = HideControls;