import { useState, useEffect, HTMLAttributes } from "react";
import "./FormField.css";
import { getCN, randomId, clickDetective } from "../../components/utils";
export interface IFormField extends HTMLAttributes {
status?: `default` | `error` | `valid`;
hovered?: `default` | `input`;
before?: React.ReactNode;
after?: React.ReactNode;
top?: React.ReactNode;
bottom?: React.ReactNode;
onActive?: (status?: boolean) => void;
formClassName?: string | undefined;
}
function FormField({
children,
before,
after,
top,
status = `default`,
bottom,
hovered = `default`,
onActive,
formClassName,
...props
}: IFormField) {
const [props_id, setPropsId] = useState(randomId(props.id, `formItem`));
const [is_mobile_hover, setMobileHover] = useState(false);
const [is_hover, setHover] = useState(false);
const [is_active, setActive] = useState(false);
const onMouseHover = (e: any) => {
if (!is_mobile_hover) {
if (e.type == "mouseover") {
setHover(true);
if (props.onMouseOver) props.onMouseOver(e);
} else if (e.type == "mouseout") {
setHover(false);
if (props.onMouseOut) props.onMouseOut(e);
}
}
};
const onBlur = (e: any) => {
setHover(false);
setActive(false);
if (onActive) onActive(false);
};
const onFocus = (e: any) => {
setActive(true);
if (onActive) onActive(true);
};
const onTouchStart = (e: any) => {
setMobileHover(true);
if (props.onTouchStart) props.onTouchStart(e);
};
return (
{top &&
{top}
}
{before &&
{before}
}
{children}
{after &&
{after}
}
{bottom &&
{bottom}
}
);
}
export default FormField;