forked from rajun/ar-test
feat: add custom button size fields
This commit is contained in:
parent
69d91f164d
commit
e3eb3384ed
59
app/components/projects/form/button-custom-fields.tsx
Normal file
59
app/components/projects/form/button-custom-fields.tsx
Normal file
@ -0,0 +1,59 @@
|
||||
import { useFormContext, useWatch } from 'react-hook-form';
|
||||
import { ColorPicker } from '~/components/ui/color-picker';
|
||||
import { Field } from '~/components/ui/field';
|
||||
import { Input } from '~/components/ui/input';
|
||||
import { BUTTON_CUSTOM_LIMITS, BUTTON_TEXT_COLORS } from '~/data/projects/constants';
|
||||
import type { ProjectFormValues } from '~/schemas/projects/project-form';
|
||||
|
||||
type Measurement = keyof typeof BUTTON_CUSTOM_LIMITS;
|
||||
const MEASUREMENTS = Object.keys(BUTTON_CUSTOM_LIMITS) as Measurement[];
|
||||
|
||||
// An empty box is "not set": no width means the button fits its label; anything else must be filled in.
|
||||
// The form also passes stored values through here (a saved empty width is null), not only typed text.
|
||||
const toNumber = (optional: boolean) => (value: unknown) => {
|
||||
if (value === '' || value === null || value === undefined) return optional ? null : Number.NaN;
|
||||
return Number(value);
|
||||
};
|
||||
|
||||
// Exact button measurements for the "Custom" size
|
||||
export function ButtonCustomFields() {
|
||||
const { register, control, setValue, formState } = useFormContext<ProjectFormValues>();
|
||||
const [borderWidth, borderColor] = useWatch({ control, name: ['arPage.buttonCustom.borderWidth', 'arPage.buttonCustom.borderColor'] });
|
||||
const errors = formState.errors.arPage?.buttonCustom;
|
||||
|
||||
return (
|
||||
<div className='flex flex-col gap-4 rounded-control p-3.5 ring-1 ring-inset ring-border-control'>
|
||||
<div className='grid grid-cols-2 gap-x-3 gap-y-4'>
|
||||
{MEASUREMENTS.map((key) => {
|
||||
const { label, min, max } = BUTTON_CUSTOM_LIMITS[key];
|
||||
const optional = key === 'width';
|
||||
return (
|
||||
<Field key={key} label={`${label} (px)`} htmlFor={`button-${key}`} error={errors?.[key]?.message}>
|
||||
<Input
|
||||
id={`button-${key}`}
|
||||
type='number'
|
||||
size='sm'
|
||||
min={min}
|
||||
max={max}
|
||||
placeholder={optional ? 'Fits the label' : undefined}
|
||||
aria-invalid={!!errors?.[key]}
|
||||
{...register(`arPage.buttonCustom.${key}`, { setValueAs: toNumber(optional) })}
|
||||
/>
|
||||
</Field>
|
||||
);
|
||||
})}
|
||||
</div>
|
||||
{borderWidth > 0 && (
|
||||
<Field label='Border colour' htmlFor='button-border-color' error={errors?.borderColor?.message}>
|
||||
<ColorPicker
|
||||
id='button-border-color'
|
||||
label='Border colour'
|
||||
value={borderColor}
|
||||
presets={BUTTON_TEXT_COLORS}
|
||||
onChange={(hex) => setValue('arPage.buttonCustom.borderColor', hex, { shouldDirty: true })}
|
||||
/>
|
||||
</Field>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user