Skip to content

Commit a568aa2

Browse files
[DURACOM-347] refactor, add test and doc
1 parent e13ce5f commit a568aa2

9 files changed

Lines changed: 34 additions & 10 deletions

config/config.example.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,11 @@ submission:
237237
- value: default
238238
style: text-muted
239239
icon: fa-circle-xmark
240+
# If set to true avoid setting placeholder for simple fields, where the placeholder would be the same as the label.
241+
# The default is set to true as placeholders that do not provide additional information to the field are to be avoided as they could cause accessibility issue.
242+
# More info on the topic can be found at https://www.deque.com/blog/accessible-forms-the-problem-with-placeholders/
243+
ignorePlaceholderForSimpleFields:
244+
240245

241246
# Fallback language in which the UI will be rendered if the user's browser language is not an active language
242247
fallbackLanguage: en

src/app/shared/form/builder/ds-dynamic-form-ui/ds-dynamic-form-control-container.component.html

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
<div [class.mb-2]="hasLabel || model.type === 'DATE' || (model.type !== 'GROUP' && asBootstrapFormGroup) || getClass('element', 'container').includes('mb-2')"
22
[class.d-none]="model.hidden"
3-
[formGroup]="group"
4-
[ngClass]="[getClass('element', 'container'), getClass('grid', 'container')]">
3+
[formGroup]="group"
4+
[ngClass]="[getClass('element', 'container'), getClass('grid', 'container')]">
55
@if (!isCheckbox && hasLabel && !isDateField) {
66
<label
77
[id]="'label_' + model.id"
88
[for]="id"
99
class="form-label"
1010
[innerHTML]="(model.required && model.label) ? (model.label | translate) + ' *' : (model.label | translate)"
11-
[ngClass]="[getClass('element', 'label'), getClass('grid', 'label')]"></label>
11+
[ngClass]="[getClass('element', 'label'), getClass('grid', 'label')]"></label>
1212
}
1313
<ng-container *ngTemplateOutlet="startTemplate?.templateRef; context: { $implicit: model };"></ng-container>
1414
<!-- Should be *ngIf instead of class d-none, but that breaks the #componentViewContainer reference-->
@@ -20,8 +20,7 @@
2020
</div>
2121

2222
@if (hasHint && (formBuilderService.hasArrayGroupValue(model) || (!model.repeatable && (isRelationship === false || value?.value === null)) || (model.repeatable === true && context?.index === context?.context?.groups?.length - 1)) && (!showErrorMessages || errorMessages.length === 0)) {
23-
<small
24-
class="text-muted ds-hint" [innerHTML]="model.hint | translate" [ngClass]="getClass('element', 'hint')"></small>
23+
<small class="text-muted ds-hint" [innerHTML]="model.hint | translate" [ngClass]="getClass('element', 'hint')"></small>
2524
}
2625
<!-- In case of repeatable fields show empty space for all elements except the first -->
2726
@if (context?.parent?.groups?.length > 1 && (!showErrorMessages || errorMessages.length === 0)) {

src/app/shared/form/builder/ds-dynamic-form-ui/ds-dynamic-form-control-container.component.spec.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,4 +434,12 @@ describe('DsDynamicFormControlContainerComponent test suite', () => {
434434
});
435435
});
436436

437+
it('should not show a label if is a checkbox or a date field', () => {
438+
const checkboxLabel = fixture.debugElement.query(By.css('#label_' + formModel[0].id));
439+
const dsDatePickerLabel = fixture.debugElement.query(By.css('#label_' + formModel[22].id));
440+
441+
expect(checkboxLabel).toBeNull();
442+
expect(dsDatePickerLabel).toBeNull();
443+
});
444+
437445
});

src/app/shared/form/builder/parsers/date-field-parser.spec.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,4 +67,12 @@ describe('DateFieldParser test suite', () => {
6767

6868
expect(fieldModel.value).toEqual(expectedValue);
6969
});
70+
71+
it('should skip setting the placeholder when ignore ignorePlaceholderForSimpleFields is true', () => {
72+
const parser = new DateFieldParser(submissionId, field, initFormValues, parserOptions, translateService);
73+
parser.ignorePlaceholderForSimpleFields = true;
74+
const fieldModel = parser.parse();
75+
76+
expect(fieldModel.placeholder).toBeNull();
77+
});
7078
});

src/app/shared/form/builder/parsers/date-field-parser.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export class DateFieldParser extends FieldParser {
1111

1212
public modelFactory(fieldValue?: FormFieldMetadataValueObject, label?: boolean): any {
1313
let malformedDate = false;
14-
const inputDateModelConfig: DynamicDsDatePickerModelConfig = this.initModel(null, label, true);
14+
const inputDateModelConfig: DynamicDsDateControlModelConfig = this.initModel(null, label, true);
1515
inputDateModelConfig.legend = this.configData.repeatable ? null : this.configData.label;
1616
inputDateModelConfig.disabled = inputDateModelConfig.readOnly;
1717
inputDateModelConfig.toggleIcon = 'fas fa-calendar';

src/app/shared/form/builder/parsers/field-parser.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { DynamicFormControlLayout } from '@ng-dynamic-forms/core';
66
import { TranslateService } from '@ngx-translate/core';
77
import uniqueId from 'lodash/uniqueId';
88

9+
import { environment } from '../../../../../environments/environment';
910
import { SubmissionScopeType } from '../../../../core/submission/submission-scope-type';
1011
import { VocabularyOptions } from '../../../../core/submission/vocabularies/models/vocabulary-options.model';
1112
import { isNgbDateStruct } from '../../../date.util';
@@ -32,7 +33,6 @@ import { VisibilityType } from './../../../../submission/sections/visibility-typ
3233
import { setLayout } from './parser.utils';
3334
import { ParserOptions } from './parser-options';
3435
import { ParserType } from './parser-type';
35-
import { environment } from "../../../../../environments/environment";
3636

3737
export const SUBMISSION_ID: InjectionToken<string> = new InjectionToken<string>('submissionId');
3838
export const CONFIG_DATA: InjectionToken<FormFieldModel> = new InjectionToken<FormFieldModel>('configData');
@@ -54,13 +54,16 @@ export abstract class FieldParser {
5454
*/
5555
protected typeField: string;
5656

57+
ignorePlaceholderForSimpleFields: boolean;
58+
5759
constructor(
5860
@Inject(SUBMISSION_ID) protected submissionId: string,
5961
@Inject(CONFIG_DATA) protected configData: FormFieldModel,
6062
@Inject(INIT_FORM_VALUES) protected initFormValues: any,
6163
@Inject(PARSER_OPTIONS) protected parserOptions: ParserOptions,
6264
protected translate: TranslateService,
6365
) {
66+
this.ignorePlaceholderForSimpleFields = environment.submission.ignorePlaceholderForSimpleFields;
6467
}
6568

6669
public abstract modelFactory(fieldValue?: FormFieldMetadataValueObject, label?: boolean): any;
@@ -307,7 +310,7 @@ export abstract class FieldParser {
307310
if (hint) {
308311
controlModel.hint = this.configData.hints || '&nbsp;';
309312
}
310-
if (!environment.submission.hidePlaceholderForBasicFields) {
313+
if (!this.ignorePlaceholderForSimpleFields) {
311314
controlModel.placeholder = this.configData.label;
312315
}
313316

src/config/default-app-config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ export class DefaultAppConfig implements AppConfig {
254254
],
255255
},
256256
},
257-
hidePlaceholderForBasicFields: true
257+
ignorePlaceholderForSimpleFields: true,
258258
};
259259

260260
// Fallback language in which the UI will be rendered if the user's browser language is not an active language

src/config/submission-config.interface.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,5 @@ export interface SubmissionConfig extends Config {
3636
duplicateDetection: DuplicateDetectionConfig;
3737
typeBind: TypeBindConfig;
3838
icons: IconsConfig;
39-
hidePlaceholderForBasicFields?: boolean;
39+
ignorePlaceholderForSimpleFields?: boolean;
4040
}

src/environments/environment.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,7 @@ export const environment: BuildConfig = {
195195
],
196196
},
197197
},
198+
ignorePlaceholderForSimpleFields: false,
198199
},
199200

200201
// NOTE: will log all redux actions and transfers in console

0 commit comments

Comments
 (0)