mirror of
https://github.com/Alfresco/alfresco-ng2-components.git
synced 2026-09-09 18:03:21 +00:00
AAE-50098 Fix unrendered rich text expressions in form submissions (#12169)
This commit is contained in:
@@ -36,6 +36,58 @@ describe('FormFieldModel', () => {
|
||||
expect(model.json).toBe(json);
|
||||
});
|
||||
|
||||
it('should return an isolated authored value snapshot', () => {
|
||||
const authoredValue = { blocks: [{ data: { text: '${field.name}' } }] };
|
||||
const model = new FormFieldModel(new FormModel(), { id: 'richText', type: FormFieldTypes.DISPLAY_RICH_TEXT, value: authoredValue });
|
||||
|
||||
const snapshot = model.authoredValue as typeof authoredValue;
|
||||
snapshot.blocks[0].data.text = 'changed';
|
||||
|
||||
expect((model.authoredValue as typeof authoredValue).blocks[0].data.text).toBe('${field.name}');
|
||||
expect(authoredValue.blocks[0].data.text).toBe('${field.name}');
|
||||
});
|
||||
|
||||
it('should not capture authored values for other field types', () => {
|
||||
const model = new FormFieldModel(new FormModel(), { id: 'json', type: FormFieldTypes.JSON, value: { content: 'value' } });
|
||||
|
||||
expect(model.authoredValue).toBeUndefined();
|
||||
});
|
||||
|
||||
it('should return undefined for authored values that cannot be cloned', () => {
|
||||
const circularValue: { self?: unknown } = {};
|
||||
circularValue.self = circularValue;
|
||||
|
||||
const circularValueModel = new FormFieldModel(new FormModel(), {
|
||||
id: 'circular',
|
||||
type: FormFieldTypes.DISPLAY_RICH_TEXT,
|
||||
value: circularValue
|
||||
});
|
||||
const bigintValueModel = new FormFieldModel(new FormModel(), {
|
||||
id: 'bigint',
|
||||
type: FormFieldTypes.DISPLAY_RICH_TEXT,
|
||||
value: BigInt(1)
|
||||
});
|
||||
|
||||
expect(circularValueModel.authoredValue).toBeUndefined();
|
||||
expect(bigintValueModel.authoredValue).toBeUndefined();
|
||||
});
|
||||
|
||||
it('should preserve authored value when form data overrides the field value', () => {
|
||||
const authoredValue = { blocks: [{ data: { text: '${field.name}' } }] };
|
||||
const savedValue = { blocks: [{ data: { text: 'John' } }] };
|
||||
const form = new FormModel(
|
||||
{
|
||||
fields: [{ id: 'richText', name: 'richText', type: FormFieldTypes.DISPLAY_RICH_TEXT, value: authoredValue }]
|
||||
},
|
||||
{ richText: savedValue }
|
||||
);
|
||||
const model = form.getFieldById('richText');
|
||||
|
||||
expect(model.value).toEqual(savedValue);
|
||||
expect(model.authoredValue).toEqual(authoredValue);
|
||||
expect(model.authoredValue).not.toBe(authoredValue);
|
||||
});
|
||||
|
||||
it('should setup with json config', () => {
|
||||
const json = {
|
||||
fieldType: '<fieldType>',
|
||||
|
||||
@@ -38,12 +38,28 @@ export type FieldOptionType = 'rest' | 'manual' | 'variable';
|
||||
export type FieldSelectionType = 'single' | 'multiple';
|
||||
export type FieldAlignmentType = 'vertical' | 'horizontal';
|
||||
|
||||
const isJsonPrimitive = (value: unknown): value is null | string | number | boolean =>
|
||||
value === null || ['string', 'number', 'boolean'].includes(typeof value);
|
||||
|
||||
const cloneJsonCompatibleValue = (value: unknown): unknown => {
|
||||
if (value === undefined || isJsonPrimitive(value)) {
|
||||
return value;
|
||||
}
|
||||
|
||||
try {
|
||||
return JSON.parse(JSON.stringify(value));
|
||||
} catch {
|
||||
return undefined;
|
||||
}
|
||||
};
|
||||
|
||||
// Maps to FormFieldRepresentation
|
||||
export class FormFieldModel extends FormWidgetModel {
|
||||
private _value: string;
|
||||
private _readOnly: boolean = false;
|
||||
private _isValid: boolean = true;
|
||||
private _required: boolean = false;
|
||||
private readonly _authoredValue: unknown;
|
||||
|
||||
readonly defaultDateFormat: string = 'D-M-YYYY';
|
||||
readonly defaultDateTimeFormat: string = 'D-M-YYYY hh:mm A';
|
||||
@@ -123,6 +139,10 @@ export class FormFieldModel extends FormWidgetModel {
|
||||
}
|
||||
}
|
||||
|
||||
get authoredValue(): unknown {
|
||||
return cloneJsonCompatibleValue(this._authoredValue);
|
||||
}
|
||||
|
||||
get readOnly(): boolean {
|
||||
if (this.form?.readOnly) {
|
||||
return true;
|
||||
@@ -183,6 +203,7 @@ export class FormFieldModel extends FormWidgetModel {
|
||||
|
||||
constructor(form: any, json?: any, parent?: RepeatableSectionModel) {
|
||||
super(form, json);
|
||||
this._authoredValue = json?.type === FormFieldTypes.DISPLAY_RICH_TEXT ? cloneJsonCompatibleValue(json.value) : undefined;
|
||||
if (json) {
|
||||
this.fieldType = json.fieldType;
|
||||
this.id = this.getId(json.id, parent);
|
||||
|
||||
Reference in New Issue
Block a user