feat: add button style fields to the project form

This commit is contained in:
rajun 2026-10-27 16:28:52 +05:30
parent 189957f5b0
commit c647e18af7

View File

@ -0,0 +1,58 @@
import { useFormContext, useWatch } from 'react-hook-form';
import { Field } from '~/components/ui/field';
import { Input } from '~/components/ui/input';
import { SegmentedControl } from '~/components/ui/segmented-control';
import { ButtonCustomFields } from '~/components/projects/form/button-custom-fields';
import { ColorField } from '~/components/projects/form/color-field';
import { AR_BUTTON_LABEL } from '~/data/ar-viewer/constants';
import { BRAND_COLORS, BUTTON_LABEL_MAX, BUTTON_SIZE_OPTIONS, BUTTON_SIZES, BUTTON_TEXT_COLORS } from '~/data/projects/constants';
import type { ProjectFormValues } from '~/schemas/projects/project-form';
import type { ButtonSize } from '~/types/project';
// The client's "View in your space" button: colour, text colour, label and size
export function ButtonStyleFields() {
const { register, control, setValue, formState } = useFormContext<ProjectFormValues>();
const [size, custom] = useWatch({ control, name: ['arPage.buttonSize', 'arPage.buttonCustom'] });
// Custom starts from the preset that was selected, so the button doesn't jump
const changeSize = (next: ButtonSize) => {
if (next === 'custom' && size !== 'custom') {
const preset = BUTTON_SIZES[size === 'full' ? 'md' : size];
setValue('arPage.buttonCustom', { ...custom, height: preset.height, paddingX: preset.paddingX, radius: preset.height / 2, width: null }, { shouldDirty: true });
}
setValue('arPage.buttonSize', next, { shouldDirty: true });
};
return (
<>
<ColorField name='brandColor' label='Button colour' presets={BRAND_COLORS} />
<ColorField name='buttonTextColor' label='Button text colour' presets={BUTTON_TEXT_COLORS} />
<Field
label='Button label'
htmlFor='project-button-label'
requirement='optional'
hint={`Leave empty to use “${AR_BUTTON_LABEL}”.`}
error={formState.errors.buttonLabel?.message}
>
<Input
id='project-button-label'
placeholder={AR_BUTTON_LABEL}
maxLength={BUTTON_LABEL_MAX}
aria-invalid={!!formState.errors.buttonLabel}
{...register('buttonLabel')}
/>
</Field>
<Field label='Button size'>
<SegmentedControl
label='Button size'
size='sm'
surface='sunken'
value={size}
options={BUTTON_SIZE_OPTIONS}
onChange={changeSize}
/>
</Field>
{size === 'custom' && <ButtonCustomFields />}
</>
);
}