import { useState } from 'react' import { useFormContext } from 'react-hook-form' import { FormControl, FormField } from 'ui' import { Input } from 'ui-patterns/DataInputs/Input' import { FormItemLayout } from 'ui-patterns/form/FormItemLayout/FormItemLayout' import { InlineLinkClassName } from '@/components/ui/InlineLink' import { generateStrongPassword } from '@/lib/project' interface RolePasswordFieldProps { /** Whether leaving the field empty is allowed. Defaults to true. */ isOptional?: boolean /** Whether the role already exists, so it may already have a password. Defaults to false. */ isExistingRole?: boolean layout?: 'horizontal' | 'vertical' } /** * Password field for a role form. Render it inside a `
` whose values include a * `password` string, and only while the role can log in. */ export const RolePasswordField = ({ isOptional = true, isExistingRole = false, layout = 'horizontal', }: RolePasswordFieldProps) => { const { control, setValue } = useFormContext<{ password: string }>() const [isPasswordGenerated, setIsPasswordGenerated] = useState(false) // A role without login can still hold a password, which a new one replaces const hint = isExistingRole ? 'Optional. Setting a password replaces any existing one.' : "Optional. Without a password, the role can't log in using password authentication." return ( ( {isOptional && `${hint} `} . } > { field.onChange(event) setIsPasswordGenerated(false) }} copy={field.value.length > 0} type="password" autoComplete="new-password" placeholder="Type in a password" /> {isPasswordGenerated ? 'Password generated' : ''} )} /> ) }