-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This type can be used on an array type of the json schema to have multiple checkboxes. Co-Authored-by: Bertrand Zuchuat <bertrand.zuchuat@rero.ch>
- Loading branch information
1 parent
7dd3454
commit 098a9b0
Showing
3 changed files
with
143 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
projects/rero/ng-core/src/lib/record/editor/type/multicheckbox.component.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
/* | ||
* RERO angular core | ||
* Copyright (C) 2022 RERO | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as published by | ||
* the Free Software Foundation, version 3 of the License. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
import { Component } from '@angular/core'; | ||
import { FieldType, FormlyFieldConfig } from '@ngx-formly/core'; | ||
|
||
@Component({ | ||
selector: 'ng-core-editor-formly-field-multicheckbox', | ||
template: ` | ||
<div> | ||
<div | ||
*ngFor="let option of to.options | formlySelectOptions:field | async; let i = index;" | ||
[ngClass]="{ | ||
'form-check': to.style === 'stacked', | ||
'form-check-inline': to.style === 'inline' | ||
}" | ||
> | ||
<input | ||
type="checkbox" | ||
class="form-check-input" | ||
[id]="id + '_' + i" | ||
[value]="option.value" | ||
[checked]="isChecked(option)" | ||
[disabled]="formControl.disabled || option.disabled" | ||
(change)="onChange(option.value, $event.target.checked)" | ||
> | ||
<label | ||
class="form-check-label" | ||
[for]="id + '_' + i" | ||
[ngClass]="{ | ||
'text-dark': (!formControl.disabled && !option.disabled), | ||
'text-secondary': (formControl.disabled || option.disabled) | ||
}" | ||
>{{ option.label }}</label> | ||
</div> | ||
</div> | ||
`, | ||
}) | ||
export class MulticheckboxComponent extends FieldType { | ||
/** Default options */ | ||
defaultOptions: Partial<FormlyFieldConfig> = { | ||
templateOptions: { | ||
options: [], | ||
style: 'stacked' // 'stacked' | 'inline' | ||
} | ||
}; | ||
|
||
/** | ||
* Adds or removes the value of a checkbox in the list of values. | ||
* @param value - checkbox value | ||
* @param checked - True if checked | ||
*/ | ||
onChange(value: any, checked: boolean): void { | ||
this.formControl.markAsDirty(); | ||
const formValue = (this.formControl.value || []); | ||
if (checked) { | ||
formValue.push(value); | ||
} else { | ||
formValue.splice(formValue.indexOf(value), 1); | ||
} | ||
this.formControl.patchValue(formValue); | ||
this.formControl.markAsTouched(); | ||
} | ||
|
||
/** | ||
* Activates the checkbox if the value exists in the list. | ||
* @param option - current ckeckbox | ||
* @return True if the current checkbox value exists in the list of values | ||
*/ | ||
isChecked(option: any): boolean { | ||
const value = this.formControl.value; | ||
|
||
return value.includes(option.value); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters