Skip to content

Commit 800a9b5

Browse files
[DURACOM-347] refactor, add test and doc
1 parent 8a2aa46 commit 800a9b5

10 files changed

Lines changed: 36 additions & 12 deletions

config/config.example.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,11 @@ submission:
220220
- value: default
221221
style: text-muted
222222
icon: fa-circle-xmark
223+
# If set to true avoid setting placeholder for simple fields, where the placeholder would be the same as the label.
224+
# 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.
225+
# More info on the topic can be found at https://www.deque.com/blog/accessible-forms-the-problem-with-placeholders/
226+
ignorePlaceholderForSimpleFields:
227+
223228

224229
# Default Language in which the UI will be rendered if the user's browser language is not an active language
225230
defaultLanguage: 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
@@ -382,4 +382,12 @@ describe('DsDynamicFormControlContainerComponent test suite', () => {
382382
expect(testFn(formModel[25])).toEqual(DsDynamicFormGroupComponent);
383383
});
384384

385+
it('should not show a label if is a checkbox or a date field', () => {
386+
const checkboxLabel = fixture.debugElement.query(By.css('#label_' + formModel[0].id));
387+
const dsDatePickerLabel = fixture.debugElement.query(By.css('#label_' + formModel[22].id));
388+
389+
expect(checkboxLabel).toBeNull();
390+
expect(dsDatePickerLabel).toBeNull();
391+
});
392+
385393
});

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,6 @@ import { SubmissionObject } from '../../../../core/submission/models/submission-
9999
import { SubmissionObjectDataService } from '../../../../core/submission/submission-object-data.service';
100100
import { paginatedRelationsToItems } from '../../../../item-page/simple/item-types/shared/item-relationships-utils';
101101
import { SubmissionService } from '../../../../submission/submission.service';
102-
import { BtnDisabledDirective } from '../../../btn-disabled.directive';
103102
import {
104103
hasNoValue,
105104
hasValue,
@@ -122,6 +121,7 @@ import {
122121
} from './existing-metadata-list-element/existing-metadata-list-element.component';
123122
import { ExistingRelationListElementComponent } from './existing-relation-list-element/existing-relation-list-element.component';
124123
import { DYNAMIC_FORM_CONTROL_TYPE_CUSTOM_SWITCH } from './models/custom-switch/custom-switch.model';
124+
import { DYNAMIC_FORM_CONTROL_TYPE_DSDATEPICKER } from './models/date-picker/date-picker.model';
125125
import { DsDynamicLookupRelationModalComponent } from './relation-lookup-modal/dynamic-lookup-relation-modal.component';
126126

127127
@Component({
@@ -139,7 +139,6 @@ import { DsDynamicLookupRelationModalComponent } from './relation-lookup-modal/d
139139
NgbTooltipModule,
140140
NgTemplateOutlet,
141141
ExistingRelationListElementComponent,
142-
BtnDisabledDirective,
143142
],
144143
standalone: true,
145144
})
@@ -301,6 +300,7 @@ export class DsDynamicFormControlContainerComponent extends DynamicFormControlCo
301300
return this.model.type === DYNAMIC_FORM_CONTROL_TYPE_CHECKBOX || this.model.type === DYNAMIC_FORM_CONTROL_TYPE_CUSTOM_SWITCH;
302301
}
303302

303+
304304
get isDateField(): boolean {
305305
return this.model.type === DYNAMIC_FORM_CONTROL_TYPE_DSDATEPICKER;
306306
}

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
@@ -11,6 +11,7 @@ import {
1111
import { TranslateService } from '@ngx-translate/core';
1212
import uniqueId from 'lodash/uniqueId';
1313

14+
import { environment } from '../../../../../environments/environment';
1415
import { SubmissionScopeType } from '../../../../core/submission/submission-scope-type';
1516
import { VocabularyOptions } from '../../../../core/submission/vocabularies/models/vocabulary-options.model';
1617
import { isNgbDateStruct } from '../../../date.util';
@@ -36,7 +37,6 @@ import { VisibilityType } from './../../../../submission/sections/visibility-typ
3637
import { setLayout } from './parser.utils';
3738
import { ParserOptions } from './parser-options';
3839
import { ParserType } from './parser-type';
39-
import { environment } from "../../../../../environments/environment";
4040

4141
export const SUBMISSION_ID: InjectionToken<string> = new InjectionToken<string>('submissionId');
4242
export const CONFIG_DATA: InjectionToken<FormFieldModel> = new InjectionToken<FormFieldModel>('configData');
@@ -58,13 +58,16 @@ export abstract class FieldParser {
5858
*/
5959
protected typeField: string;
6060

61+
ignorePlaceholderForSimpleFields: boolean;
62+
6163
constructor(
6264
@Inject(SUBMISSION_ID) protected submissionId: string,
6365
@Inject(CONFIG_DATA) protected configData: FormFieldModel,
6466
@Inject(INIT_FORM_VALUES) protected initFormValues: any,
6567
@Inject(PARSER_OPTIONS) protected parserOptions: ParserOptions,
6668
protected translate: TranslateService,
6769
) {
70+
this.ignorePlaceholderForSimpleFields = environment.submission.ignorePlaceholderForSimpleFields;
6871
}
6972

7073
public abstract modelFactory(fieldValue?: FormFieldMetadataValueObject, label?: boolean): any;
@@ -311,7 +314,7 @@ export abstract class FieldParser {
311314
if (hint) {
312315
controlModel.hint = this.configData.hints || '&nbsp;';
313316
}
314-
if (!environment.submission.hidePlaceholderForBasicFields) {
317+
if (!this.ignorePlaceholderForSimpleFields) {
315318
controlModel.placeholder = this.configData.label;
316319
}
317320

src/config/default-app-config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ export class DefaultAppConfig implements AppConfig {
250250
],
251251
},
252252
},
253-
hidePlaceholderForBasicFields: true
253+
ignorePlaceholderForSimpleFields: true,
254254
};
255255

256256
// Default 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
@@ -177,6 +177,7 @@ export const environment: BuildConfig = {
177177
],
178178
},
179179
},
180+
ignorePlaceholderForSimpleFields: false,
180181
},
181182

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

0 commit comments

Comments
 (0)