Skip to content

Commit

Permalink
feat(dynamic-form): adiciona escolha entre po-switch ou po-checkbox
Browse files Browse the repository at this point in the history
O usuário pode escolher entre um `po-checkbox` ou um `po-switch`
para exibição dentro de um `po-dynamic-form`.

Fixes #1420, DTHFUI-5286
  • Loading branch information
wsteixeira authored and alinelariguet committed Nov 17, 2023
1 parent cbb93e8 commit 1b91e50
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
/**
* @usedBy PoDynamicFormComponent
*
* @description
*
* Enum para definição do tipo de componente a ser renderizado.
*/
export enum ForceBooleanComponentEnum {
/** Força a renderização de um po-switch */
switch = 'switch',

/** Força a renderização de um po-checkbox */
checkbox = 'checkbox'
}

/**
* @usedBy PoDynamicFormComponent
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { PoLookupAdvancedFilter } from '../../po-field/po-lookup/interfaces/po-l
import { PoLookupColumn } from '../../po-field/po-lookup/interfaces/po-lookup-column.interface';
import { PoMultiselectOption } from '../../po-field/po-multiselect/po-multiselect-option.interface';
import { PoSelectOption } from '../../po-field/po-select/po-select-option.interface';
import { ForceOptionComponentEnum } from '../po-dynamic-field-force-component.enum';
import { ForceBooleanComponentEnum, ForceOptionComponentEnum } from '../po-dynamic-field-force-component.enum';

import { PoDynamicField } from '../po-dynamic-field.interface';

Expand Down Expand Up @@ -507,6 +507,15 @@ export interface PoDynamicFormField extends PoDynamicField {
* `url + ?page=1&pageSize=20&name=Tony%20Stark,Peter%20Parker,Gohan`
*/
advancedFilters?: Array<PoLookupAdvancedFilter>;

/**
* Valores aceitos:
* - ForceBooleanComponentEnum.switch
* - ForceBooleanComponentEnum.checkbox
*
*/
forceBooleanComponentType?: ForceBooleanComponentEnum;

/**
* pode ser utilizada em conjunto com a propriedade `options` forçando o componente a renderizar um `po-select` ou `po-radio-group`.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import * as PoDynamicUtil from '../../po-dynamic.util';
import { PoDynamicFieldType } from '../../po-dynamic-field-type.enum';
import { PoDynamicFormFieldsBaseComponent } from './po-dynamic-form-fields-base.component';
import { PoDynamicFormField } from '../po-dynamic-form-field.interface';
import { ForceOptionComponentEnum } from '../../po-dynamic-field-force-component.enum';
import { ForceBooleanComponentEnum, ForceOptionComponentEnum } from '../../po-dynamic-field-force-component.enum';

describe('PoDynamicFormFieldsBaseComponent:', () => {
let component: PoDynamicFormFieldsBaseComponent;
Expand Down Expand Up @@ -629,6 +629,16 @@ describe('PoDynamicFormFieldsBaseComponent:', () => {
expect(component['getComponentControl'](field)).toBe(expectedValue);
});

it('should return checkbox if `forceBooleanComponentType` is checkbox', () => {
const expectedValue = ForceBooleanComponentEnum.checkbox;
const field: PoDynamicFormField = {
property: 'test',
forceBooleanComponentType: ForceBooleanComponentEnum.checkbox
};

expect(component['getComponentControl'](field)).toBe(expectedValue);
});

it('should return select if `forceOptionsComponentType` is select', () => {
const expectedValue = ForceOptionComponentEnum.select;
const field: PoDynamicFormField = {
Expand All @@ -650,10 +660,10 @@ describe('PoDynamicFormFieldsBaseComponent:', () => {
]
};

spyOn(component, <any>'verifyforceOptionComponent').and.callThrough();
spyOn(component, <any>'verifyForceOptionComponent').and.callThrough();

expect(component['getComponentControl'](field)).toBe(expectedValue);
expect(component['verifyforceOptionComponent']).toHaveBeenCalled();
expect(component['verifyForceOptionComponent']).toHaveBeenCalled();
});

it('shouldn`t return select if `forceOptionsComponentType` is select but optionsMulti is true', () => {
Expand Down Expand Up @@ -682,10 +692,10 @@ describe('PoDynamicFormFieldsBaseComponent:', () => {
]
};

spyOn(component, <any>'verifyforceOptionComponent').and.callThrough();
spyOn(component, <any>'verifyForceOptionComponent').and.callThrough();

expect(component['getComponentControl'](field)).toBe(expectedValue);
expect(component['verifyforceOptionComponent']).toHaveBeenCalled();
expect(component['verifyForceOptionComponent']).toHaveBeenCalled();
});

it('should return `upload` if type is `upload` and has a `url`', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,13 @@ export class PoDynamicFormFieldsBaseComponent {
private getComponentControl(field: PoDynamicFormField = <any>{}) {
const type = field && field.type ? field.type.toLocaleLowerCase() : 'string';

const forceOptionComponent = this.verifyforceOptionComponent(field);
const { forceBooleanComponentType } = field;
const forceOptionComponent = this.verifyForceOptionComponent(field);

if (forceBooleanComponentType) {
return forceBooleanComponentType;
}

if (forceOptionComponent) {
const { forceOptionsComponentType } = field;
return forceOptionsComponentType;
Expand Down Expand Up @@ -257,7 +263,7 @@ export class PoDynamicFormFieldsBaseComponent {
return url && type === 'upload';
}

private verifyforceOptionComponent(field: PoDynamicFormField) {
private verifyForceOptionComponent(field: PoDynamicFormField) {
const { optionsMulti, optionsService, forceOptionsComponentType } = field;

if (forceOptionsComponentType && !optionsMulti && !optionsService) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,20 @@
>
</po-switch>

<po-checkbox
#component
*ngIf="compareTo(field.control, 'checkbox')"
[name]="field.property"
[(ngModel)]="value[field.property]"
[ngClass]="field.componentClass"
[p-auto-focus]="field.focus"
[p-disabled]="isDisabled(field)"
[p-label]="field.label"
[p-size]="field.size"
(p-change)="onChangeField(field)"
>
</po-checkbox>

<po-combo
#component
*ngIf="compareTo(field.control, 'combo')"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
>
</po-dynamic-form>

<br />

<div class="po-row">
<po-button
class="po-md-3"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import {
PoDynamicFormField,
PoDynamicFormFieldChanged,
PoDynamicFormValidation,
PoNotificationService
PoNotificationService,
ForceBooleanComponentEnum
} from '@po-ui/ng-components';
import { PoDynamicFormRegisterService } from './sample-po-dynamic-form-register.service';

Expand Down Expand Up @@ -178,6 +179,13 @@ export class SamplePoDynamicFormRegisterComponent implements OnInit {
],
optionsMulti: true
},
{
property: 'agree',
gridColumns: 12,
label: 'Do you agree?',
type: 'boolean',
forceBooleanComponentType: ForceBooleanComponentEnum.checkbox
},
{
property: 'image',
type: 'upload',
Expand Down

0 comments on commit 1b91e50

Please sign in to comment.