Skip to content
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
4 changes: 4 additions & 0 deletions example/src/Onboarding.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,10 @@ const OnboardingWithProps = ({
// Nigeria
contract_details: 2,
},
NLD: {
// Netherlands
contract_details: 2,
},
NOR: {
// Norway
contract_details: 2,
Expand Down
81 changes: 30 additions & 51 deletions src/components/form/fields/ForcedValueField.tsx
Original file line number Diff line number Diff line change
@@ -1,39 +1,9 @@
import { useFormContext } from 'react-hook-form';
import { sanitizeHtml } from '@/src/lib/utils';
import { useEffect } from 'react';
import { ZendeskTriggerButton } from '@/src/components/shared/zendesk-drawer/ZendeskTriggerButton';

const Description = ({
name,
description,
helpCenter,
}: {
name: string;
description: string;
helpCenter?: {
callToAction: string;
id: number;
url: string;
label: string;
};
}) => {
return (
<span>
<span
className={`text-xs RemoteFlows__ForcedValue__Description__${name}`}
dangerouslySetInnerHTML={{ __html: sanitizeHtml(description) }}
/>
{helpCenter?.callToAction && helpCenter?.id && (
<ZendeskTriggerButton
className='RemoteFlows__ForcedValue__HelpCenterLink'
zendeskId={helpCenter.id}
>
{helpCenter.callToAction}
</ZendeskTriggerButton>
)}
</span>
);
};
import { HelpCenterDataProps } from '@/src/types/fields';
import { BaseFormDescription as Description } from '@/src/components/ui/form';
import { HelpCenter } from '@/src/components/shared/zendesk-drawer/HelpCenter';

export type ForcedValueFieldProps = {
name: string;
Expand All @@ -44,12 +14,7 @@ export type ForcedValueFieldProps = {
description?: string;
};
label: string;
helpCenter?: {
callToAction: string;
id: number;
url: string;
label: string;
};
helpCenter?: HelpCenterDataProps;
};

export function ForcedValueField({
Expand All @@ -61,40 +26,54 @@ export function ForcedValueField({
helpCenter,
}: ForcedValueFieldProps) {
const { setValue } = useFormContext();
const descriptionSanitized = sanitizeHtml(
statement?.description || description,
);
const forcedValueDescription = statement?.description || description;

const titleSanitized = statement?.title
const forcedValueTitle = statement?.title
? sanitizeHtml(statement?.title)
: sanitizeHtml(label);

const titleId = `forced-value-${name}-title`;
const descriptionId = `forced-value-${name}-description`;

useEffect(() => {
setValue(name, value);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);

const isHiddenValue = !descriptionSanitized && !statement?.title;
const isHiddenValue = !forcedValueDescription && !statement?.title;

if (isHiddenValue) {
return null;
}

return (
<div>
{titleSanitized && (
<div
role='group'
aria-labelledby={forcedValueTitle ? titleId : undefined}
aria-describedby={forcedValueDescription ? descriptionId : undefined}
>
{forcedValueTitle && (
<p
id={titleId}
className={`text-sm RemoteFlows__ForcedValue__Title__${name}`}
dangerouslySetInnerHTML={{
__html: titleSanitized,
__html: forcedValueTitle,
}}
/>
)}
<Description
name={name}
description={descriptionSanitized}
helpCenter={helpCenter}
/>
as='span'
id={descriptionId}
className={`text-xs RemoteFlows__ForcedValue__Description__${name}`}
helpCenter={
<HelpCenter
className='RemoteFlows__ForcedValue__HelpCenterLink'
helpCenter={helpCenter}
/>
}
>
{forcedValueDescription}
</Description>
</div>
);
}
5 changes: 3 additions & 2 deletions src/components/shared/zendesk-drawer/HelpCenter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@ import { HelpCenterDataProps } from '@/src/types/fields';

type HelpCenterProps = {
helpCenter?: HelpCenterDataProps;
className?: string;
};

export function HelpCenter({ helpCenter }: HelpCenterProps) {
export function HelpCenter({ helpCenter, className }: HelpCenterProps) {
if (!helpCenter || !helpCenter.id || !helpCenter.callToAction) {
return null;
}

return (
<ZendeskTriggerButton zendeskId={helpCenter.id}>
<ZendeskTriggerButton zendeskId={helpCenter.id} className={className}>
{helpCenter.callToAction}
</ZendeskTriggerButton>
);
Expand Down
26 changes: 18 additions & 8 deletions src/components/ui/form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -131,26 +131,26 @@ const FormControl = React.forwardRef<

FormControl.displayName = 'FormControl';

function FormDescription<T extends React.ElementType = 'p'>({
export function BaseFormDescription<T extends React.ElementType = 'p'>({
helpCenter,
className,
children,
as,
id,
...props
}: React.ComponentProps<'p'> & {
helpCenter?: React.ReactNode;
children?: React.ReactNode | (() => React.ReactNode);
as?: T;
} & Omit<React.ComponentPropsWithoutRef<T>, 'children' | 'className'>) {
const { formDescriptionId } = useFormField();
id?: string;
} & Omit<React.ComponentPropsWithoutRef<T>, 'children' | 'className' | 'id'>) {
const Component = as || 'p';

if (typeof children === 'string') {
return (
<>
<Component
data-slot='form-description'
id={formDescriptionId}
id={id}
className={cn('text-base-color text-xs', className)}
data-sanitized='true'
{...props}
Expand All @@ -161,13 +161,12 @@ function FormDescription<T extends React.ElementType = 'p'>({
</>
);
}

return (
<Component
data-slot='form-description'
id={formDescriptionId}
data-sanitized='false'
id={id}
className={cn('text-base-color text-xs', className)}
data-sanitized='false'
{...props}
>
{typeof children === 'function' ? children() : children}
Expand All @@ -176,6 +175,17 @@ function FormDescription<T extends React.ElementType = 'p'>({
);
}

function FormDescription<T extends React.ElementType = 'p'>({
...props
}: React.ComponentProps<'p'> & {
helpCenter?: React.ReactNode;
children?: React.ReactNode | (() => React.ReactNode);
as?: T;
} & Omit<React.ComponentPropsWithoutRef<T>, 'children' | 'className' | 'id'>) {
const { formDescriptionId } = useFormField();
return <BaseFormDescription id={formDescriptionId} {...props} />;
}

function FormMessage({ className, ...props }: React.ComponentProps<'p'>) {
const { error, formMessageId } = useFormField();
const body = error ? String(error?.message ?? '') : props.children;
Expand Down
Loading