Skip to content

Commit

Permalink
🪹 Remove current prop from form counter (#221)
Browse files Browse the repository at this point in the history
  • Loading branch information
nezouse authored Mar 22, 2024
1 parent 3bceec3 commit 437d4de
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 24 deletions.
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

0 comments on commit 437d4de

Please sign in to comment.