This commit is contained in:
honzapatCZ 2024-10-08 14:11:40 +02:00
parent 1e9f25e61d
commit 407d4c1215

View File

@ -1,7 +1,7 @@
import React from "react"; import React from "react";
import tw, { styled } from "twin.macro"; import tw from "twin.macro";
import { useField as useFormikField, useFormikContext } from "formik"; import { useField as useFormikField, useFormikContext, FieldHookConfig } from "formik";
//const useFormikField = null; //const useFormikField = null;
//const useFormikContext = ()=>{}; //const useFormikContext = ()=>{};
@ -9,37 +9,45 @@ export function useField(props){
let getField = useFormikField; let getField = useFormikField;
const context = useFormikContext(); const context = useFormikContext();
if(!context){ if(!context){
getField = (props)=>{ getField = (propsOrFieldName: FieldHookConfig<any>)=>{
return [props, null, null]; return [props, null, null];
} }
} }
return getField(props); return getField(props);
} }
export default function Input({ label, title, children, className, ...props }) { export default function Input(
props: React.InputHTMLAttributes<HTMLInputElement> & {
label?: React.ComponentPropsWithoutRef<"label">;
title?: string;
children?: React.ReactNode;
className?: string;
}
) {
/*name, id*/ /*name, id*/
props.id = props.id ?? props.name ?? title;
const [field, meta, helpers] = useField(props); const [field, meta, helpers] = useField(props);
return ( return (
<div css={[title && tw`my-1`, tw`relative`]} className={className}> <div css={[props.title && tw`my-1`, tw`relative`]} className={props.className}>
{title && ( {props.title && (
<label <label
{...label} {...props.label}
css={[tw`block text-secondary text-sm font-bold mb-2`, label?.css]} htmlFor={props.id ?? props.name ?? props.title}
css={[tw`block text-secondary text-sm font-bold mb-2`, props.label?.css]}
> >
{/*htmlFor={id ?? name ?? title}*/} {/*htmlFor={id ?? name ?? title}*/}
{title} {props.title}
</label> </label>
)} )}
{/*name={name || title} id={id || name || title}*/} {/*name={name || title} id={id || name || title}*/}
<input <input
id={props.id ?? props.name ?? props.title}
{...props} {...props}
{...field} {...field}
tw="bg-primary appearance-none border-2 border-secondary rounded w-full py-2 px-4 text-primary leading-tight focus:outline-none focus:bg-secondary focus:border-accent transition duration-150 " tw="bg-primary appearance-none border-2 border-secondary rounded w-full py-2 px-4 text-primary leading-tight focus:outline-none focus:bg-secondary focus:border-accent transition duration-150 "
/> />
{children} {props.children}
{meta?.touched && meta.error ? ( {meta?.touched && meta.error ? (
<div tw="text-[#c23c3c]">{meta.error}</div> <div tw="text-[#c23c3c]">{meta.error}</div>
) : null} ) : null}