/** * @format */ import { Button, FormLabel, Input, Select, Box, FormControl, HStack, InputGroup, InputRightAddon, Heading, InputLeftAddon, Text, Flex } from '@chakra-ui/react' import {AnyFunction} from '@chakra-ui/utils' import React, {ReactNode, useEffect, useRef, useState} from 'react' import {FormInputDataIntf, KeyValue, SelectBoxDataIntf} from '../types/types' import { getPseudoRandomId, removeLetters, removeLettersLeaveDots, sqmToSqft } from '../common/utils' export interface ButtonWithIconProps { /** * Is this the principal call to action on the page? */ variant?: 'primary' | 'outline' | 'menu' | 'ghost' /** * How large should the button be? */ size?: 'xs' | 'sm' | 'md' | 'lg' /** * Show loading icon */ isLoading?: boolean /** * Makes the button enabled or disabled */ isDisabled?: boolean /** * Optional click handler */ clickHandler?: () => void /** * The icon object. This can be an image element or SVG element of type HTMLOrSVGImageElement */ icon: React.ReactElement /** * Position of the icon with respect to the text */ iconPosition?: 'left' | 'right' /** * Any other chakra prop for the button */ props?: KeyValue children: ReactNode } interface ButtonProps { /** * Is this the principal call to action on the page? */ variant?: 'primary' | 'outline' | 'menu' | 'ghost' /** * How large should the button be? */ size?: 'xs' | 'sm' | 'md' | 'lg' /** * Show loading icon */ isLoading?: boolean /** * Makes the button enabled or disabled */ isDisabled?: boolean /** * Optional click handler */ clickHandler?: () => void /** * Any other chakra prop for the button */ props?: KeyValue /** * Content of the button */ children: ReactNode } export const PGButton = ({ variant = 'outline', children, size = 'md', clickHandler, isLoading = false, isDisabled = false, props }: ButtonProps) => ( ) export const ButtonWithIcon = ({ iconPosition = 'left', icon, variant, children, clickHandler, isLoading = false, isDisabled = false, size = 'md', props }: ButtonWithIconProps) => ( ) export const TextBox = ({ value, props, changeHandler }: { value: string props: FormInputDataIntf changeHandler: any isNumber?: boolean }) => { const id = getPseudoRandomId() return ( {props.title && {props.title}} ) } export const CurrencyBox = ({ value, props, changeHandler }: { value: string props: FormInputDataIntf changeHandler: any }) => { const id = getPseudoRandomId() return ( {props.title && {props.title}} ) } export const LandArea = ({ sqftValue, sqmValue, props, changeHandler }: { sqftValue: string sqmValue: string props: FormInputDataIntf changeHandler: any }) => { const id = getPseudoRandomId() type supportedLandAreaTypes = 'sqft' | 'sqm' | undefined const [currentUnit, setCurrenUnit] = useState( props.defaultLandAreaType ) const sqftRef = useRef(null) const sqmRef = useRef(null) return ( {props.title && {props.title}} {currentUnit === 'sqft' && ( ) => { changeHandler(e) }} /> )} {currentUnit === 'sqm' && ( ) => { changeHandler(e) }} /> )} { setCurrenUnit(currentUnit === 'sqft' ? 'sqm' : 'sqft') }} children={currentUnit} /> ) } export const SelectBox = ({ props, changeHandler }: { props: FormInputDataIntf changeHandler: any }) => { const id = getPseudoRandomId() return ( {props.title !== null && ( {props.title} )} ) } export const FormComponent = ({ inputData, submitHandler, cancelHandler }: // formState { inputData: Array submitHandler: AnyFunction cancelHandler?: AnyFunction // formState: KeyValue }) => { const [formData, setFormData] = useState([]) const addNormalFormData = (obj: FormInputDataIntf) => { const appendValue: any = [] if ( obj.type !== 'submit' && obj.type !== 'cancel' && obj.type !== 'heading' ) { if (obj.type === 'area') { appendValue[`${obj.name}_sqft`] = '' appendValue[`${obj.name}_sqm`] = '' } else { appendValue[obj.name] = obj.defaultValue || '' } } setFormData((prev: any) => ({ ...prev, ...appendValue })) } useEffect(() => { inputData.forEach((obj: any) => { if (obj.type !== 'row') { addNormalFormData(obj) } else if (obj.type === 'row') { obj.columns.forEach((col: FormInputDataIntf) => { addNormalFormData(col) }) } }) }, []) const moreFilterFieldsOnChange = (e: React.FormEvent) => { const targetElm = e.target as HTMLInputElement const {name, value} = targetElm setFormData((prev: any) => ({ ...prev, [name]: value })) } const mapElements = (_obj: FormInputDataIntf, rowObj?: any) => { const obj = _obj if (rowObj && rowObj.required) { if (!obj.htmlValidations) { obj.htmlValidations = {required: true} } } const id = `${getPseudoRandomId()}_${_obj.name}` return ( {obj.type === 'heading' && ( {obj.title} )} {obj.type === 'text' && ( )} {obj.type === 'number' && ( )} {obj.type === 'currency' && ( )} {obj.type === 'area' && ( )} {obj.type === 'selectBox' && ( )} {obj.type === 'cancel' && ( )} {obj.type === 'submit' && ( )} ) } return ( <> {Object.keys(formData).length && (
) => { submitHandler(e, formData) e.preventDefault() }} > {inputData.map((obj: any) => ( {/* Single column layout */} {obj.type !== 'row' && mapElements(obj)} {/* Dual column layout */} {obj.type === 'row' && ( <> {obj.rowTitle}{' '} {obj.required && ( * )} {obj.columns.map((colObj: FormInputDataIntf) => mapElements(colObj, obj) )} )} ))}
)} ) } export const WrapperBox = ({children}: {children: ReactNode}) => ( {children} )