-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(directives): add generator for directives (#65)
additionally add disabled host directive
- Loading branch information
Showing
33 changed files
with
822 additions
and
317 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
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
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
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
Large diffs are not rendered by default.
Oops, something went wrong.
File renamed without changes.
32 changes: 32 additions & 0 deletions
32
projects/cli/schematics/_utils/apply-file-template.util.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,32 @@ | ||
import { normalize, strings } from '@angular-devkit/core'; | ||
import { | ||
apply, | ||
applyTemplates, | ||
chain, | ||
mergeWith, | ||
move, | ||
Rule, | ||
url, | ||
} from '@angular-devkit/schematics'; | ||
|
||
export function applyFileTemplateUtil( | ||
sources: string[], | ||
path: string, | ||
templatesPath = `./templates` | ||
): Rule[] { | ||
return sources.map(source => { | ||
const rules: Rule[] = [ | ||
applyTemplates({ | ||
name: source, | ||
localeDate: new Date().toLocaleString(), | ||
...strings, | ||
}), | ||
move(normalize(`${path}/${source}`)), | ||
]; | ||
|
||
const componentTemplateSource = apply(url(`./files/${source}`), rules); | ||
const genericTemplates = apply(url(templatesPath), rules); | ||
|
||
return chain([componentTemplateSource, genericTemplates].map(mergeWith)); | ||
}); | ||
} |
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
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
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
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
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
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
4 changes: 2 additions & 2 deletions
4
projects/cli/schematics/components/files/switch/zen-switch.component.html
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
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
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 |
---|---|---|
@@ -1,34 +1,10 @@ | ||
import { normalize, strings } from '@angular-devkit/core'; | ||
import { | ||
apply, | ||
applyTemplates, | ||
chain, | ||
mergeWith, | ||
move, | ||
Rule, | ||
url, | ||
} from '@angular-devkit/schematics'; | ||
import { chain, Rule } from '@angular-devkit/schematics'; | ||
|
||
import { applyFileTemplateUtil } from '../_utils/apply-file-template.util'; | ||
import { ComponentGeneratorSchema } from './components-generator'; | ||
|
||
export function componentGenerator(options: ComponentGeneratorSchema): Rule { | ||
return () => { | ||
const componentsSource = options.components.map(component => { | ||
const rules: Rule[] = [ | ||
applyTemplates({ | ||
name: component, | ||
localeDate: new Date().toLocaleString(), | ||
...strings, | ||
}), | ||
move(normalize(`${options.path}/${component}`)), | ||
]; | ||
|
||
const componentTemplateSource = apply(url(`./files/${component}`), rules); | ||
const genericTemplates = apply(url(`./templates`), rules); | ||
|
||
return chain([componentTemplateSource, genericTemplates].map(mergeWith)); | ||
}); | ||
|
||
return chain(componentsSource); | ||
return chain(applyFileTemplateUtil(options.components, options.path)); | ||
}; | ||
} |
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,4 @@ | ||
export interface DirectiveGeneratorSchema { | ||
directives: 'disabled'[]; | ||
path: string; | ||
} |
34 changes: 34 additions & 0 deletions
34
projects/cli/schematics/directives/files/disabled/disabled.directive.spec.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,34 @@ | ||
import { Component } from '@angular/core'; | ||
import { ComponentFixture, TestBed } from '@angular/core/testing'; | ||
|
||
import { ZenDisabledDirective } from './disabled.directive'; | ||
import { ZenHostDirective } from './index'; | ||
|
||
@Component({ | ||
template: ``, | ||
hostDirectives: [ZenHostDirective], | ||
standalone: true, | ||
}) | ||
class ZenDisabledComponent {} | ||
|
||
describe('ZenDisabledDirective', () => { | ||
let component: ZenDisabledComponent; | ||
let directive: ZenDisabledDirective; | ||
|
||
beforeEach(async () => { | ||
await TestBed.configureTestingModule({ | ||
imports: [ZenDisabledComponent], | ||
}).compileComponents(); | ||
|
||
const fixture: ComponentFixture<ZenDisabledComponent> = | ||
TestBed.createComponent(ZenDisabledComponent); | ||
component = fixture.componentInstance; | ||
directive = fixture.debugElement.injector.get(ZenDisabledDirective); | ||
fixture.detectChanges(); | ||
}); | ||
|
||
it('should create', () => { | ||
expect(component).toBeTruthy(); | ||
expect(directive).toBeTruthy(); | ||
}); | ||
}); |
43 changes: 43 additions & 0 deletions
43
projects/cli/schematics/directives/files/disabled/disabled.directive.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,43 @@ | ||
import { computed, Directive, HostBinding, model } from '@angular/core'; | ||
|
||
/** | ||
* @example | ||
* <button zenDisabled> ... </button> | ||
* | ||
* @export | ||
* @class ZenDisabledDirective | ||
* | ||
* @license BSD-2-Clause | ||
* @author Konrad Stępień | ||
* @see {https://github.com/Kordrad/ng-zen GitHub Repository} | ||
* */ | ||
@Directive({ | ||
selector: '[zenDisabled]', | ||
standalone: true, | ||
}) | ||
export class ZenDisabledDirective { | ||
/** Model for the disabled state of the checkbox. */ | ||
readonly disabled = model<boolean | 'true' | 'false' | ''>(false, { | ||
alias: 'zenDisabled', | ||
}); | ||
|
||
/** @ignore */ | ||
readonly disabledBoolean = computed(() => | ||
[true, 'true', ''].includes(this.disabled()) | ||
); | ||
|
||
@HostBinding('disabled') | ||
get disabledAttr(): string | null | boolean { | ||
return this.disabledBoolean() || null; | ||
} | ||
|
||
@HostBinding('attr.aria-disabled') | ||
get ariaDisabledAttr(): boolean { | ||
return this.disabledBoolean(); | ||
} | ||
|
||
@HostBinding('class.zen-disabled') | ||
get disabledClass(): boolean { | ||
return this.disabledBoolean(); | ||
} | ||
} |
Oops, something went wrong.