Skip to content
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

🪹 Remove current prop from form counter #221

Merged
merged 1 commit into from
Mar 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 3 additions & 12 deletions src/app/waves/[waveId]/applications/create/steps/mainDetails.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -133,10 +133,7 @@ export function MainDetails() {
</FormControl>
<FormMessages>
<FormMessage />
<FormCounter
current={field.value.length}
limit={FORM_FIELD_PARAMS.name.max}
/>
<FormCounter limit={FORM_FIELD_PARAMS.name.max} />
</FormMessages>
</FormItem>
)}
Expand All @@ -156,10 +153,7 @@ export function MainDetails() {
</FormControl>
<FormMessages>
<FormMessage />
<FormCounter
current={field.value.length}
limit={FORM_FIELD_PARAMS.entityName.max}
/>
<FormCounter limit={FORM_FIELD_PARAMS.entityName.max} />
</FormMessages>
</FormItem>
)}
Expand Down Expand Up @@ -221,10 +215,7 @@ export function MainDetails() {
</FormControl>
<FormMessages>
<FormMessage />
<FormCounter
current={field.value.length}
limit={FORM_FIELD_PARAMS.summary.max}
/>
<FormCounter limit={FORM_FIELD_PARAMS.summary.max} />
</FormMessages>
</FormItem>
)}
Expand Down
10 changes: 2 additions & 8 deletions src/app/waves/create/steps/mainDetails.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,7 @@ export function MainDetails() {
<FormControl>
<Input {...field} />
</FormControl>
<FormCounter
current={field.value.length}
limit={FORM_FIELD_PARAMS[field.name].max}
/>
<FormCounter limit={FORM_FIELD_PARAMS[field.name].max} />
<FormMessage />
</FormItem>
)}
Expand All @@ -72,10 +69,7 @@ export function MainDetails() {
<FormControl>
<Textarea {...field} />
</FormControl>
<FormCounter
current={field.value.length}
limit={FORM_FIELD_PARAMS[field.name].max}
/>
<FormCounter limit={FORM_FIELD_PARAMS[field.name].max} />
<FormMessage />
</FormItem>
)}
Expand Down
13 changes: 9 additions & 4 deletions src/components/ui/form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
Controller,
FormProvider,
useFormContext,
useWatch,
type ControllerProps,
type FieldPath,
type FieldValues,
Expand Down Expand Up @@ -230,18 +231,22 @@ FormMessage.displayName = "FormMessage";

interface FormCounterProps
extends Pick<HTMLAttributes<HTMLParagraphElement>, "className"> {
current: number;
limit: number;
}

const FormCounter = ({ current, limit, className }: FormCounterProps) => {
const FormCounter = ({ limit, className }: FormCounterProps) => {
const { name } = useFormField();
const value = useWatch({ name });

const currentLength = value.length;

return (
<p
className={cn("group ml-auto mr-4 w-fit text-xs opacity-60", className)}
data-overlimit={current > limit}
data-overlimit={currentLength > limit}
>
<span className="transition-colors group-data-[overlimit=true]:text-destructive">
{current}
{currentLength}
</span>
/{limit}
</p>
Expand Down
Loading