fix: prefill fields (#1689)
Users can now selectively choose which properties to pre-fill for each field - from just a label to all available properties.
This commit is contained in:
@@ -118,6 +118,7 @@ const getUpdatedFieldMeta = (field: Field, prefillField?: TFieldMetaPrefillField
|
|||||||
...existingMeta,
|
...existingMeta,
|
||||||
type: 'text',
|
type: 'text',
|
||||||
label: field.label,
|
label: field.label,
|
||||||
|
placeholder: field.placeholder,
|
||||||
text: field.value,
|
text: field.value,
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -134,6 +135,7 @@ const getUpdatedFieldMeta = (field: Field, prefillField?: TFieldMetaPrefillField
|
|||||||
...existingMeta,
|
...existingMeta,
|
||||||
type: 'number',
|
type: 'number',
|
||||||
label: field.label,
|
label: field.label,
|
||||||
|
placeholder: field.placeholder,
|
||||||
value: field.value,
|
value: field.value,
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -190,8 +192,16 @@ const getUpdatedFieldMeta = (field: Field, prefillField?: TFieldMetaPrefillField
|
|||||||
|
|
||||||
const checkboxMeta = result.data;
|
const checkboxMeta = result.data;
|
||||||
|
|
||||||
|
if (!field.value) {
|
||||||
|
throw new AppError(AppErrorCode.INVALID_BODY, {
|
||||||
|
message: `Value is required for CHECKBOX field ${field.id}`,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
const fieldValue = field.value;
|
||||||
|
|
||||||
// Validate that all values exist in the options
|
// Validate that all values exist in the options
|
||||||
for (const value of field.value) {
|
for (const value of fieldValue) {
|
||||||
const valueExists = checkboxMeta.values?.some((option) => option.value === value);
|
const valueExists = checkboxMeta.values?.some((option) => option.value === value);
|
||||||
|
|
||||||
if (!valueExists) {
|
if (!valueExists) {
|
||||||
@@ -203,7 +213,7 @@ const getUpdatedFieldMeta = (field: Field, prefillField?: TFieldMetaPrefillField
|
|||||||
|
|
||||||
const newValues = checkboxMeta.values?.map((option) => ({
|
const newValues = checkboxMeta.values?.map((option) => ({
|
||||||
...option,
|
...option,
|
||||||
checked: field.value.includes(option.value),
|
checked: fieldValue.includes(option.value),
|
||||||
}));
|
}));
|
||||||
|
|
||||||
const meta: TCheckboxFieldMeta = {
|
const meta: TCheckboxFieldMeta = {
|
||||||
|
|||||||
@@ -130,28 +130,30 @@ export const ZFieldMetaPrefillFieldsSchema = z
|
|||||||
z.discriminatedUnion('type', [
|
z.discriminatedUnion('type', [
|
||||||
z.object({
|
z.object({
|
||||||
type: z.literal('text'),
|
type: z.literal('text'),
|
||||||
label: z.string(),
|
label: z.string().optional(),
|
||||||
value: z.string(),
|
placeholder: z.string().optional(),
|
||||||
|
value: z.string().optional(),
|
||||||
}),
|
}),
|
||||||
z.object({
|
z.object({
|
||||||
type: z.literal('number'),
|
type: z.literal('number'),
|
||||||
label: z.string(),
|
label: z.string().optional(),
|
||||||
value: z.string(),
|
placeholder: z.string().optional(),
|
||||||
|
value: z.string().optional(),
|
||||||
}),
|
}),
|
||||||
z.object({
|
z.object({
|
||||||
type: z.literal('radio'),
|
type: z.literal('radio'),
|
||||||
label: z.string(),
|
label: z.string().optional(),
|
||||||
value: z.string(),
|
value: z.string().optional(),
|
||||||
}),
|
}),
|
||||||
z.object({
|
z.object({
|
||||||
type: z.literal('checkbox'),
|
type: z.literal('checkbox'),
|
||||||
label: z.string(),
|
label: z.string().optional(),
|
||||||
value: z.array(z.string()),
|
value: z.array(z.string()).optional(),
|
||||||
}),
|
}),
|
||||||
z.object({
|
z.object({
|
||||||
type: z.literal('dropdown'),
|
type: z.literal('dropdown'),
|
||||||
label: z.string(),
|
label: z.string().optional(),
|
||||||
value: z.string(),
|
value: z.string().optional(),
|
||||||
}),
|
}),
|
||||||
]),
|
]),
|
||||||
);
|
);
|
||||||
|
|||||||
Reference in New Issue
Block a user