import { DatePicker } from 'components/ui/DatePicker'
import dayjs from 'dayjs'
import { useEffect, useState } from 'react'
import ReactMarkdown from 'react-markdown'
import { Button, IconEye, IconEyeOff, Input, InputNumber, Listbox, Toggle } from 'ui'
import { BASE_PATH } from 'lib/constants'
import { Enum } from './AuthProvidersForm.types'
interface FormFieldProps {
name: string
properties: any
formValues: any
disabled?: boolean
}
function formatDate(date: Date): string {
return dayjs(date).format('dddd, MMMM D, YYYY HH:mm:ss Z')
}
const FormField = ({ name, properties, formValues, disabled = false }: FormFieldProps) => {
const [hidden, setHidden] = useState(!!properties.isSecret)
const [dateAsText, setDateAsText] = useState(
formValues[name] ? formatDate(new Date(formValues[name])) : ''
)
useEffect(() => {
if (properties.show && properties.show.key && !formValues[properties.show.key]) {
formValues[name] = ''
setDateAsText('')
}
}, [properties.show && properties.show.key && !formValues[properties.show.key]])
if (properties.show) {
if (properties.show.matches) {
if (!properties.show.matches.includes(formValues[properties.show.key])) {
return null
}
} else if (!formValues[properties.show.key]) {
return null
}
}
switch (properties.type) {
case 'datetime':
return (
{properties.descriptionOptional}
) : null
}
descriptionText={
properties.description ? (
{properties.description}
) : null
}
actions={
{
if (date && date.to) {
formValues[name] = date.to
setDateAsText(formatDate(new Date(date.to)))
} else {
setDateAsText('')
formValues[name] = ''
}
}}
>
Pick
}
/>
)
case 'string':
return (
{properties.descriptionOptional}
) : null
}
descriptionText={
properties.description ? (
{properties.description}
) : null
}
actions={
!!properties.isSecret ? (
: }
type="default"
onClick={() => setHidden(!hidden)}
/>
) : (
{properties.units ? (
{properties.units}
) : null}
)
}
/>
)
case 'number':
return (
{properties.descriptionOptional}
) : null
}
descriptionText={
properties.description ? (
{properties.description}
) : null
}
actions={
{properties.units ? (
{properties.units}
) : null}
}
/>
)
case 'boolean':
return (
{properties.description}
) : null
}
/>
)
case 'select':
return (
{properties.description}
) : null
}
defaultValue={properties.enum[0]}
>
{properties.enum.map((option: Enum) => {
return (
(
)}
>
{option.label}
)
})}
)
default:
break
}
return <>>
}
export default FormField