<option value="false" i18n>False</option>
</select>
} @else if (getCustomFieldByID(atom.field)?.data_type === CustomFieldDataType.Select) {
- <ng-select
+ <ng-select #fieldSelects
class="paperless-input-select rounded-end"
[items]="getSelectOptionsForField(atom.field)"
[(ngModel)]="atom.value"
-import { ComponentFixture, TestBed } from '@angular/core/testing'
+import {
+ ComponentFixture,
+ fakeAsync,
+ TestBed,
+ tick,
+} from '@angular/core/testing'
import {
CustomFieldQueriesModel,
CustomFieldsQueryDropdownComponent,
CustomFieldQueryAtom,
CustomFieldQueryElement,
} from 'src/app/utils/custom-field-query-element'
+import { NgSelectModule } from '@ng-select/ng-select'
+import { FormsModule, ReactiveFormsModule } from '@angular/forms'
const customFields = [
{
beforeEach(async () => {
await TestBed.configureTestingModule({
declarations: [CustomFieldsQueryDropdownComponent],
- imports: [NgbDropdownModule, NgxBootstrapIconsModule.pick(allIcons)],
+ imports: [
+ NgbDropdownModule,
+ NgxBootstrapIconsModule.pick(allIcons),
+ NgSelectModule,
+ FormsModule,
+ ReactiveFormsModule,
+ ],
providers: [
provideHttpClient(withInterceptorsFromDi()),
provideHttpClientTesting(),
expect(component.name).toBe('test_title')
})
+ it('should add a default atom on open and focus the select field', fakeAsync(() => {
+ expect(component.selectionModel.queries.length).toBe(0)
+ component.onOpenChange(true)
+ fixture.detectChanges()
+ tick()
+ expect(component.selectionModel.queries.length).toBe(1)
+ expect(window.document.activeElement.tagName).toBe('INPUT')
+ }))
+
describe('CustomFieldQueriesModel', () => {
let model: CustomFieldQueriesModel
Input,
OnDestroy,
Output,
+ QueryList,
ViewChild,
+ ViewChildren,
} from '@angular/core'
import { NgbDropdown } from '@ng-bootstrap/ng-bootstrap'
+import { NgSelectComponent } from '@ng-select/ng-select'
import { Subject, first, takeUntil } from 'rxjs'
import { CustomField, CustomFieldDataType } from 'src/app/data/custom-field'
import {
@ViewChild('dropdown') dropdown: NgbDropdown
+ @ViewChildren(NgSelectComponent) fieldSelects!: QueryList<NgSelectComponent>
+
private _selectionModel: CustomFieldQueriesModel
@Input()
}
public onOpenChange(open: boolean) {
- if (open && this.selectionModel.queries.length === 0) {
- this.selectionModel.addExpression()
+ if (open) {
+ if (this.selectionModel.queries.length === 0) {
+ this.selectionModel.addAtom(
+ new CustomFieldQueryAtom([
+ null,
+ CustomFieldQueryOperator.Exists,
+ 'true',
+ ])
+ )
+ }
+ if (
+ this.selectionModel.queries.length === 1 &&
+ (
+ (this.selectionModel.queries[0] as CustomFieldQueryExpression)
+ ?.value[0] as CustomFieldQueryAtom
+ )?.field === null
+ ) {
+ setTimeout(() => {
+ this.fieldSelects.first?.focus()
+ }, 0)
+ }
}
}
public get isActive(): boolean {
- return (
- (this.selectionModel.queries[0] as CustomFieldQueryExpression)?.value
- ?.length > 0
- )
+ return this.selectionModel.isValid()
}
private getFields() {
CustomFieldQueryLogicalOperator,
CustomFieldQueryOperator,
} from 'src/app/data/custom-field-query'
-import { CustomFieldQueryAtom } from 'src/app/utils/custom-field-query-element'
+import {
+ CustomFieldQueryAtom,
+ CustomFieldQueryExpression,
+} from 'src/app/utils/custom-field-query-element'
const tags: Tag[] = [
{
By.css('button')
)
customFieldToggleButton.triggerEventHandler('click')
+ tick()
fixture.detectChanges()
- const customFieldButtons = customFieldsQueryDropdown.queryAll(
- By.css('button')
- )
- customFieldButtons[1].triggerEventHandler('click')
- fixture.detectChanges()
- const query = component.customFieldQueriesModel
- .queries[0] as CustomFieldQueryAtom
- query.field = custom_fields[0].id
+ const expression = component.customFieldQueriesModel
+ .queries[0] as CustomFieldQueryExpression
+ const atom = expression.value[0] as CustomFieldQueryAtom
+ atom.field = custom_fields[0].id
const fieldSelect: NgSelectComponent = customFieldsQueryDropdown.queryAll(
By.directive(NgSelectComponent)
)[0].componentInstance