import { styled } from '../tokens/stitches.config' import { useState } from 'react' import Checkbox from './Checkbox' import { HStack, VStack } from './LayoutPrimitives' import { Label } from '@radix-ui/react-dropdown-menu' interface FormInputPropsOption { label: string value: string } export interface FormInputProps { name: string label: string value?: any onChange?: (value: any) => void type?: string placeholder?: string disabled?: boolean hidden?: boolean required?: boolean css?: any options?: string[] | FormInputPropsOption[] min?: any } export const FormInput = styled('input', { border: 'none', width: '100%', bg: 'transparent', fontSize: '16px', fontFamily: 'inter', fontWeight: 'normal', lineHeight: '1.35', borderRadius: '5px', textIndent: '8px', marginBottom: '2px', height: '38px', pl: '10px', color: '$grayTextContrast', '&:focus': { outline: 'none', }, '@mdDown': { pl: '5px', }, }) export const FormLabel = styled('label', { fontSize: '16px', color: '$omnivoreGray', '&.required:after': { content: ' *', color: 'red', }, }) export const BorderedFormInput = styled(FormInput, { height: '40px', margin: '0', padding: '4px 11px', color: 'rgba(0,0,0,.88)', fontSize: '14px', lineHeight: '1.6', listStyle: 'none', width: '100%', minWidth: '0', backgroundColor: '#fff', borderWidth: '1px', borderStyle: 'solid', borderColor: '#d9d9d9', borderRadius: '6px', transition: 'all .2s', '&:focus': { border: '1px solid transparent', outline: '2px solid $omnivoreCtaYellow', }, }) export function GeneralFormInput(props: FormInputProps): JSX.Element { const [input, setInput] = useState(props) if (props.type === 'checkbox') { const StyledLabel = styled(Label, { color: '$grayTextContrast', fontSize: 13, padding: '5px 10px', cursor: 'default', }) return ( {input.options?.map((option, index) => { if (typeof option === 'string') { return ( { input.value[index] = arg setInput(input) props.onChange && props.onChange( (input.options as string[]).filter( (_, i) => input.value[i] ) ) }} > {option} ) } else { return ( { input.value[index] = arg setInput(input) props.onChange && props.onChange( (input.options as FormInputPropsOption[]) .filter((_, i) => input.value[i]) .map((option) => option.value) ) }} > {option.label} ) } })} ) } else if (props.type === 'select') { return ( ) } else { return ( { if (input.onChange) { setInput({ ...input, value: event.target.value }) input.onChange(event.target.value) } }} disabled={input.disabled} hidden={input.hidden} required={input.required} css={{ border: '1px solid $textNonessential', borderRadius: '5px', width: '100%', bg: 'transparent', fontSize: '16px', textIndent: '8px', marginBottom: '2px', height: '38px', color: '$grayTextContrast', '&:focus': { border: '1px solid transparent', outline: '2px solid $omnivoreCtaYellow', }, }} name={input.name} min={input.min} /> ) } }