-
Notifications
You must be signed in to change notification settings - Fork 13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(4746): add password input components #4753
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
'use client'; | ||
|
||
import { zodResolver } from '@hookform/resolvers/zod'; | ||
import { Button } from '@mantine/core'; | ||
import { FormProvider, useForm } from 'react-hook-form'; | ||
import { z } from 'zod'; | ||
import HookFormPasswordInput from '@/components/generic/input/HookFormPasswordInput'; | ||
import { GlobalRole } from '@/constants'; | ||
import createClientPage from '@/core/client-page'; | ||
|
||
const validationSchema = z.object({ | ||
username: z.string().min(1).max(100), | ||
password: z.string().min(1).max(100), | ||
}); | ||
|
||
const Page = createClientPage({ | ||
roles: [GlobalRole.User], | ||
}); | ||
export default Page(() => { | ||
const methods = useForm({ | ||
resolver: zodResolver(validationSchema), | ||
defaultValues: {}, | ||
}); | ||
|
||
return ( | ||
<> | ||
<h1 className="font-bold text-2xl mt-4 mb-5">Password Input</h1> | ||
<FormProvider {...methods}> | ||
<form onSubmit={methods.handleSubmit(console.log)} autoComplete="off"> | ||
<div className="grid grid-cols-1 md:grid-cols-3 md:gap-4 md:py-2"> | ||
<HookFormPasswordInput | ||
label="Username" | ||
name="username" | ||
placeholder="Enter username..." | ||
required | ||
classNames={{ wrapper: 'col-span-1' }} | ||
/> | ||
<HookFormPasswordInput | ||
label="Password" | ||
name="password" | ||
placeholder="Enter password..." | ||
required | ||
classNames={{ wrapper: 'col-span-1' }} | ||
/> | ||
</div> | ||
|
||
<Button variant="success" type="submit" className="mt-1"> | ||
Submit | ||
</Button> | ||
</form> | ||
</FormProvider> | ||
</> | ||
); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
'use client'; | ||
|
||
import { PasswordInput, PasswordInputProps } from '@mantine/core'; | ||
import _kebabCase from 'lodash-es/kebabCase'; | ||
import { InputHTMLAttributes } from 'react'; | ||
import { cn } from '@/utils/js'; | ||
import Label from '../Label'; | ||
|
||
interface InputProps extends InputHTMLAttributes<HTMLInputElement> {} | ||
type OtherProps = Omit<InputProps, 'size'>; | ||
|
||
const inputClasslist = [ | ||
'block', | ||
'border-0', | ||
'disabled:bg-slate-50', | ||
'disabled:text-slate-500', | ||
'disabled:cursor-not-allowed', | ||
'focus:ring-2', | ||
'focus:ring-indigo-600', | ||
'invalid:ring-pink-600', | ||
'invalid:text-pink-600', | ||
'placeholder:text-gray-400', | ||
'py-2', | ||
'ring-1', | ||
'ring-gray-300', | ||
'ring-inset', | ||
'rounded-md', | ||
'shadow-sm', | ||
'sm:leading-6', | ||
'sm:text-sm', | ||
'text-gray-900', | ||
'w-full', | ||
]; | ||
|
||
const inputClass = inputClasslist.join(' '); | ||
|
||
export interface FormPasswordInputProps extends PasswordInputProps { | ||
id?: string; | ||
name: string; | ||
label?: string; | ||
inputProps?: OtherProps; | ||
classNames?: { | ||
wrapper?: string; | ||
label?: string; | ||
input?: string; | ||
}; | ||
copyable?: boolean; | ||
onCopy?: () => void; | ||
info?: string; | ||
} | ||
|
||
export default function FormPasswordInput({ | ||
id, | ||
name, | ||
label, | ||
type = 'text', | ||
classNames, | ||
required, | ||
disabled, | ||
inputProps = {}, | ||
copyable = false, | ||
onCopy, | ||
info, | ||
...others | ||
}: FormPasswordInputProps) { | ||
if (!id) id = _kebabCase(name); | ||
|
||
return ( | ||
<div className={cn('text-input', classNames?.wrapper)}> | ||
{label && ( | ||
<Label | ||
htmlFor={id} | ||
className={classNames?.label} | ||
required={required} | ||
copyable={copyable} | ||
onCopy={onCopy} | ||
info={info} | ||
> | ||
{label} | ||
</Label> | ||
)} | ||
<PasswordInput | ||
id={id} | ||
name={name} | ||
disabled={disabled} | ||
autoComplete="off" | ||
{...inputProps} | ||
{...others} | ||
variant="unstyled" | ||
classNames={{ input: 'overflow-visible', innerInput: cn(inputClass, classNames?.input) }} | ||
/> | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
'use client'; | ||
|
||
import _get from 'lodash-es/get'; | ||
import { FieldValues, RegisterOptions, Path, useFormContext } from 'react-hook-form'; | ||
import { cn } from '@/utils/js'; | ||
import FormError from '../FormError'; | ||
import FormPasswordInput, { FormPasswordInputProps } from './FormPasswordInput'; | ||
|
||
export default function HookFormPasswordInput<T extends FieldValues>({ | ||
id, | ||
name, | ||
options, | ||
label, | ||
error, | ||
classNames, | ||
disabled, | ||
...others | ||
}: Omit<FormPasswordInputProps, 'name' | 'inputProps'> & { | ||
name: Path<T>; | ||
options?: RegisterOptions<T, Path<T>> | undefined; | ||
error?: string; | ||
}) { | ||
const { | ||
register, | ||
formState: { errors }, | ||
} = useFormContext<T>(); | ||
|
||
const formError = _get(errors, name); | ||
const _error = error ?? (formError && String(formError?.message)); | ||
const showError = !!formError && !disabled; | ||
|
||
return ( | ||
<div className={classNames?.wrapper}> | ||
<FormPasswordInput | ||
id={id} | ||
name={name} | ||
label={label} | ||
disabled={disabled} | ||
{...others} | ||
classNames={{ | ||
label: cn(classNames?.label, { | ||
'text-pink-600': showError, | ||
}), | ||
input: cn(classNames?.input, { | ||
'ring-pink-600 text-pink-600': showError, | ||
}), | ||
}} | ||
inputProps={register(name, options)} | ||
/> | ||
{showError && <FormError field={name} className="mt-1" message={_error} />} | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it possible to avoid using
console.log
directly inhandleSubmit
and instead pass a proper callback function?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@golebu2020 do you mean something like
(formData) => console.log(formData)
?