import * as React from "react"; import { TextInputProps } from "./TextInput"; import nextId from "react-id-generator"; import { InputAppearance } from "../../shared/types/inputAppearance"; import { flex, flexItem, flush, inputReset, padding } from "../../shared/styles/styleUtils"; import { cx } from "@emotion/css"; import { TextInputWithIconProps } from "./TextInputWithIcon"; import IconPropAdapter from "../../icon/components/IconPropAdapter"; import { getIconAppearanceStyle, inputIconWrapper } from "../../shared/styles/formStyles"; const getInputElementProps = (props: TextInputProps): TextInputProps => { const placeholderId = nextId("textInput-"); // omit props for container and that we override, otherwise pass through // TextInput props to input element const { className, hintContent, inputLabel, errors, id = placeholderId, ...inputElementProps } = props; return { ...inputElementProps, id }; }; const getInputElement = ( additionalClasses: string[] = [], isValid: boolean, describedBy: string, textInputProps: TextInputProps, getInputAppearance: (props: TextInputProps) => string, getInputElementProps: (textInputProps: TextInputProps) => TextInputProps ) => { const { value, showInputLabel, appearance, tooltipContent, type, ...inputElementProps } = getInputElementProps(textInputProps); const textInputAppearance = getInputAppearance(textInputProps); const dataCy = [ "textInput-input", ...(textInputAppearance && textInputAppearance !== InputAppearance.Standard ? [`textInput-input.${textInputAppearance}`] : []) ].join(" "); let { onChange } = inputElementProps; if (onChange == null && value != null) { onChange = Function.prototype as ( event: React.FormEvent ) => void; } const additionalProps = { ...{ ...inputElementProps, onChange, value, type } }; return ( ); }; const getInputAppearance = (props: TextInputProps): string => { return props.disabled ? "disabled" : props.appearance; }; const getIconStartContent = ( props: TextInputWithIconProps, appearance: string ) => { if (!props.iconStart) { return null; } return ( ); }; const getIconEndContent = ( props: TextInputWithIconProps, appearance: string ) => { if (!props.iconEnd) { return null; } return ( ); }; const getId = (props: TextInputProps): string => { if (typeof props.id === "string") { return props.id; } return nextId("textInput-"); }; export { getInputElementProps, getInputElement, getInputAppearance, getIconStartContent, getIconEndContent, getId };