Skip to content

Commit a93575c

Browse files
reshawbengry
authored andcommitted
Add directive support for fab-dropdown (#104)
-Add directives to support i18n for fab-dropdown -Add demo of new directive-supported fab-dropdown to app.component -Minor cleanup of directives for fab-combo-box
1 parent ec65a3e commit a93575c

File tree

8 files changed

+85
-21
lines changed

8 files changed

+85
-21
lines changed

apps/demo/src/app/app.component.html

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,13 @@ <h2>Getting up and running...</h2>
1919
(onBlur)="logEvent('dropdown blur', $event)"
2020
></fab-dropdown>
2121

22+
<fab-dropdown contentStyle="width: 300px;" [placeholder]="'Select one'" [defaultSelectedKey]="'A'" (onChange)="logEvent('dropdown change', $event)">
23+
<options>
24+
<fab-dropdown-option optionKey="A" text="See option A"></fab-dropdown-option>
25+
<fab-dropdown-option optionKey="B" text="See option B"></fab-dropdown-option>
26+
</options>
27+
</fab-dropdown>
28+
2229
<fab-icon iconName="Add" (onClick)="onClickEventHandler($event)" (onMouseOver)="onMouseOverEventHandler($event)">
2330
</fab-icon>
2431

libs/fabric/src/lib/components/combo-box/base-combo-box.component.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import { ComboBoxOptionsDirective } from './directives/combo-box-options.directi
2020

2121
export abstract class FabBaseComboBoxComponent extends ReactWrapperComponent<IComboBoxProps>
2222
implements OnInit, AfterContentInit {
23-
@ContentChild(ComboBoxOptionDirective) readonly optionsDirective?: ComboBoxOptionDirective;
2423

2524
@Input() componentRef?: IComboBoxProps['componentRef'];
2625
@Input() options: IComboBoxProps['options'];

libs/fabric/src/lib/components/combo-box/directives/combo-box-option.directive.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export class ComboBoxOptionDirective {
1414
@Input() optionKey: IComboBoxOption['key'];
1515
@Input() text: IComboBoxOption['text'];
1616
@Input() title?: IComboBoxOption['title'];
17-
@Input() itemType: IComboBoxOption['itemType'];
17+
@Input() itemType?: IComboBoxOption['itemType'];
1818
@Input() index?: IComboBoxOption['index'];
1919
@Input() ariaLabel?: IComboBoxOption['ariaLabel'];
2020
@Input() selected?: IComboBoxOption['selected'];

libs/fabric/src/lib/components/combo-box/directives/combo-box-options.directive.ts

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,16 @@ import { ComboBoxOptionDirective } from "./combo-box-option.directive";
88

99
/**
1010
* Wrapper directive for creating multiple ComboBoxOptions
11+
* Note that if you use this, it will override the imperative [options] binding.
1112
*/
1213
@Directive({ selector: 'fab-combo-box > options' })
1314
export class ComboBoxOptionsDirective {
1415
@ContentChildren(ComboBoxOptionDirective) readonly directiveItems: QueryList<ComboBoxOptionDirective>;
1516

1617
get items() {
17-
return this.directiveItems.map<IComboBoxOption>(directiveItem => {
18-
return {
19-
key: directiveItem.optionKey,
20-
text: directiveItem.text,
21-
title: directiveItem.title,
22-
itemType: directiveItem.itemType,
23-
index: directiveItem.index,
24-
ariaLabel: directiveItem.ariaLabel,
25-
selected: directiveItem.selected,
26-
disabled: directiveItem.disabled,
27-
data: directiveItem.data,
28-
styles: directiveItem.styles,
29-
useAriaLabelAsText: directiveItem.useAriaLabelAsText
30-
}
31-
});
18+
return this.directiveItems.map<IComboBoxOption>(({ optionKey, ...otherDirectiveProps }) => ({
19+
key: optionKey,
20+
...otherDirectiveProps
21+
}));
3222
}
3323
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
import { Directive, Input } from '@angular/core';
5+
import { IDropdownOption } from 'office-ui-fabric-react';
6+
7+
/**
8+
* Wrapper directive for creating a DropdownOption
9+
* @paramName optionKey Binds to React 'key' property.
10+
* Name change necessary because key is a reserved attribute in the wrapper component.
11+
*/
12+
@Directive({ selector: 'fab-dropdown-option' })
13+
export class DropdownOptionDirective {
14+
@Input() optionKey: IDropdownOption['key'];
15+
@Input() text: IDropdownOption['text'];
16+
@Input() title?: IDropdownOption['title'];
17+
@Input() itemType?: IDropdownOption['itemType'];
18+
@Input() index?: IDropdownOption['index'];
19+
@Input() ariaLabel?: IDropdownOption['ariaLabel'];
20+
@Input() selected?: IDropdownOption['selected'];
21+
@Input() disabled?: IDropdownOption['disabled'];
22+
@Input() data?: IDropdownOption['data'];
23+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
import { ContentChildren, Directive, QueryList } from '@angular/core';
5+
6+
import { DropdownOptionDirective } from './dropdown-option.directive';
7+
import { IDropdownOption } from 'office-ui-fabric-react';
8+
9+
/**
10+
* Wrapper directive for creating multiple DropdownOptions
11+
* Note that if you use this, it will override the imperative [options] binding.
12+
*/
13+
@Directive({ selector: 'fab-dropdown > options' })
14+
export class DropdownOptionsDirective {
15+
@ContentChildren(DropdownOptionDirective) readonly directiveItems: QueryList<DropdownOptionDirective>;
16+
17+
get items() {
18+
return this.directiveItems.map<IDropdownOption>(({ optionKey, ...otherDirectiveProps }) => ({
19+
key: optionKey,
20+
...otherDirectiveProps
21+
}));
22+
}
23+
}

libs/fabric/src/lib/components/dropdown/dropdown.component.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,12 @@ import {
1313
ViewChild,
1414
Output,
1515
EventEmitter,
16+
ContentChild,
17+
AfterContentInit,
1618
} from '@angular/core';
1719
import { IDropdownProps, IDropdownOption, IDropdown } from 'office-ui-fabric-react/lib/Dropdown';
1820
import { ISelectableDroppableTextProps, ISelectableOption } from 'office-ui-fabric-react';
21+
import { DropdownOptionsDirective } from './directives/dropdown-options.directive';
1922

2023
@Component({
2124
selector: 'fab-dropdown',
@@ -61,8 +64,9 @@ import { ISelectableDroppableTextProps, ISelectableOption } from 'office-ui-fabr
6164
styles: ['react-renderer'],
6265
changeDetection: ChangeDetectionStrategy.OnPush,
6366
})
64-
export class FabDropdownComponent extends ReactWrapperComponent<IDropdownProps> implements OnInit {
67+
export class FabDropdownComponent extends ReactWrapperComponent<IDropdownProps> implements OnInit, AfterContentInit {
6568
@ViewChild('reactNode') protected reactNodeRef: ElementRef;
69+
@ContentChild(DropdownOptionsDirective) readonly dropdownOptionsDirective?: DropdownOptionsDirective;
6670

6771
@Input() componentRef?: IDropdownProps['componentRef'];
6872
@Input() label?: IDropdownProps['label'];
@@ -135,6 +139,13 @@ export class FabDropdownComponent extends ReactWrapperComponent<IDropdownProps>
135139
this.onRenderCaretDown = this.createRenderPropHandler(this.renderCaretDown);
136140
}
137141

142+
ngAfterContentInit() {
143+
if (this.dropdownOptionsDirective) {
144+
this._initDirective(this.dropdownOptionsDirective);
145+
}
146+
super.ngAfterContentInit();
147+
}
148+
138149
onChangeHandler(event: React.FormEvent<HTMLDivElement>, option?: IDropdownOption, index?: number) {
139150
this.onChange.emit({
140151
event: event && event.nativeEvent,
@@ -146,4 +157,9 @@ export class FabDropdownComponent extends ReactWrapperComponent<IDropdownProps>
146157
onDismissHandler() {
147158
this.onDismiss.emit();
148159
}
160+
161+
private _initDirective(directive: DropdownOptionsDirective) {
162+
this.options = directive.items;
163+
this.markForCheck();
164+
}
149165
}

libs/fabric/src/lib/components/dropdown/dropdown.module.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,19 @@ import { CommonModule } from '@angular/common';
66
import { NgModule, NO_ERRORS_SCHEMA } from '@angular/core';
77
import { Dropdown } from 'office-ui-fabric-react';
88
import { FabDropdownComponent } from './dropdown.component';
9+
import { DropdownOptionDirective } from './directives/dropdown-option.directive';
10+
import { DropdownOptionsDirective } from './directives/dropdown-options.directive';
911

10-
const components = [FabDropdownComponent];
12+
const declarations = [
13+
FabDropdownComponent,
14+
DropdownOptionDirective,
15+
DropdownOptionsDirective
16+
];
1117

1218
@NgModule({
1319
imports: [CommonModule],
14-
declarations: components,
15-
exports: components,
20+
declarations: declarations,
21+
exports: declarations,
1622
schemas: [NO_ERRORS_SCHEMA],
1723
})
1824
export class FabDropdownModule {

0 commit comments

Comments
 (0)