What would be a good FormMessage implementation strategy for Rich Texts? #3006
Unanswered
codesfromshad
asked this question in
Q&A
Replies: 1 comment 1 reply
-
I think I just poopified const FormMessage = React.forwardRef<
HTMLParagraphElement,
React.HTMLAttributes<HTMLParagraphElement>
>(({ className, children, ...props }, ref) => {
const { error, formMessageId } = useFormField();
const body = error && !children
? DOMPurify.sanitize(String(error?.message))
.replace(/`([^`]+)`/g, "<kbd>$1</kbd>")
: children;
if (!body) {
return null;
}
if (children) {
return (
<p
ref={ref}
id={formMessageId}
className={cn(
"text-[0.8rem] font-medium text-destructive",
className
)}
{...props}
>
{children}
</p>
);
}
return (
<p
ref={ref}
id={formMessageId}
className={cn(
"[&_kbd]:rounded [&_kbd]:border [&_kbd]:border-border [&_kbd]:bg-muted [&_kbd]:px-1",
"text-[0.8rem] font-medium text-destructive",
className
)}
{...props}
dangerouslySetInnerHTML={{ __html: body as string | TrustedHTML }}
/>
);
});
FormMessage.displayName = "FormMessage"; |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
First of all,
shadcn-ui
is just amazing. However, I am a bit confuzzled as I am failing to come up with a goodFormMessage
implementation strategy. I am usingreact-hook-form
andnext-intl
, as well aszod
in my project. For example, in myvalidation.ts
file I have this set-up:t()
is the function that gets passed invalidation.ts
and it takes the translation key (refer to this). However,message
inctx.addIssue
can bestring | undefined
. Unfortunately, it cannot takestring | React.ReactElement
like<ErrorMessage />
component ofreact-hook-form
. Any idea on how I can passReact.ReactElement
sot.rich()
can also work?Beta Was this translation helpful? Give feedback.
All reactions