Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

editor: new wrapper toggle-switch #120

Merged
merged 1 commit into from
Jan 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -368,13 +368,16 @@ export class EditorComponent implements OnInit, OnDestroy {
...field.hooks,
afterContentInit: (f: FormlyFieldConfig) => {
const recordType = formOptions.remoteOptions.type;
const query = formOptions.remoteOptions.query ? formOptions.remoteOptions.query : '';
f.templateOptions.options = this.recordService
.getRecords(recordType, '', 1, RecordService.MAX_REST_RESULTS_SIZE)
.getRecords(recordType, query, 1, RecordService.MAX_REST_RESULTS_SIZE)
.pipe(
map(data =>
data.hits.hits.map(record => {
return {
label: record.metadata.name,
label: formOptions.remoteOptions.labelField && formOptions.remoteOptions.labelField in record.metadata
? record.metadata[formOptions.remoteOptions.labelField]
: record.metadata.name,
value: this.apiService.getRefEndpoint(
recordType,
record.metadata.pid
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { Component, OnInit } from '@angular/core';
import { FieldWrapper } from '@ngx-formly/core';
import { isEmpty, removeEmptyValues } from '../utils';

@Component({
selector: 'ng-core-editor-formly-toggle-wrapper',
template: `
<div class='toggle-wrapper'>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not find the styles of toggle-wrapper

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No specific styles are used for toggle-switch.

If bootstrap toggle library is loaded for projec, the display will be a switch (like the screenshoot), otherwise a default checkbox will be displayed.

<div class="custom-control custom-switch">
<input class="custom-control-input" type="checkbox" id="toggle-switch" (change)="toggle($event)" [checked]="tsOptions.enabled">
<label class="custom-control-label" for="toggle-switch">{{ tsOptions.label }}</label>
</div>
<small class="form-text text-muted" *ngIf="tsOptions.description">{{ tsOptions.description }}</small>
<ng-container *ngIf="tsOptions.enabled" #fieldComponent></ng-container>
</div>
`
})
export class ToggleWrapperComponent extends FieldWrapper implements OnInit {

tsOptions = {
label: 'Toggle',
description: null,
enabled: false
};

ngOnInit() {
if (this.to['toggle-switch']) {
this.tsOptions = {...this.tsOptions, ...this.to['toggle-switch']};
}
this.tsOptions.enabled = !isEmpty(removeEmptyValues(this.model));
jma marked this conversation as resolved.
Show resolved Hide resolved
}

toggle(event: any) {
this.tsOptions.enabled = !this.tsOptions.enabled;
if (this.tsOptions.enabled === false) {
this.field.formControl.reset(); // reset all children fields
}
}

}
7 changes: 6 additions & 1 deletion projects/rero/ng-core/src/lib/record/record.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ import { marker as _ } from '@biesbjerg/ngx-translate-extract-marker';
import { SwitchComponent } from './editor/switch/switch.component';
import { MultiSchemaTypeComponent } from './editor/multischema/multischema.component';
import { DatepickerTypeComponent } from './editor/type/datepicker-type.component';
import {ToggleWrapperComponent} from './editor/toggle-wrapper/toggle-wrappers.component';

@NgModule({
declarations: [
Expand All @@ -71,7 +72,8 @@ import { DatepickerTypeComponent } from './editor/type/datepicker-type.component
ObjectTypeComponent,
SwitchComponent,
MultiSchemaTypeComponent,
DatepickerTypeComponent
DatepickerTypeComponent,
ToggleWrapperComponent
],
imports: [
CoreModule,
Expand Down Expand Up @@ -136,6 +138,9 @@ import { DatepickerTypeComponent } from './editor/type/datepicker-type.component
{ name: 'object', component: ObjectTypeComponent },
{ name: 'multischema', component: MultiSchemaTypeComponent },
{ name: 'datepicker', component: DatepickerTypeComponent }
],
wrappers: [
{ name: 'toggle-switch', component: ToggleWrapperComponent }
]
}),
FormlyBootstrapModule
Expand Down