Skip to content

Commit

Permalink
feat(Angular): add standalone components to the @beeq/angular outpu…
Browse files Browse the repository at this point in the history
…t target (#870)

We have added more flexibility over the `dist-custom-elements` target,
allowing using it on each framework-specific configuration target. The
Angular output target has been improved, the value accessors are now
part of the BEEQ Angular module, and no need to import them on the
Angular project side.
Instead of this:

```ts
import {
  BeeQModule,
  BooleanValueAccessor,
  TextValueAccessor,
} from '@beeq/angular';

/** 💡 More Value Accessors will be exported later and should be included as well */
const VALUE_ACCESSORS = [BooleanValueAccessor, TextValueAccessor];

@NgModule({
  declarations: [AppComponent, ...VALUE_ACCESSORS],
  imports: [BeeQModule.forRoot(), BrowserModule, FormsModule, ReactiveFormsModule],
  ....
})
```

we will have only to do this:

```ts
@NgModule({
  declarations: [AppComponent],
  imports: [BeeQModule.forRoot(), BrowserModule, FormsModule, ReactiveFormsModule],
  ....
})
```

Also, we added new value accessors: number, radio, and select, for
components like `bq-input[type="number"]`, `bq-radio-group`, and
`bq-select`.
  • Loading branch information
dgonzalezr authored Feb 24, 2024
1 parent 5a297a9 commit 055209f
Show file tree
Hide file tree
Showing 19 changed files with 166 additions and 35 deletions.
16 changes: 8 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

55 changes: 50 additions & 5 deletions packages/beeq-angular/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,15 +118,11 @@ To enable two-way binding and the use of [ngModel] within BEEQ form components,
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { BrowserModule } from '@angular/platform-browser';
import { BeeQModule, BooleanValueAccessor, TextValueAccessor } from '@beeq/angular';

import { AppComponent } from './app.component';

/** 💡 More Value Accessors will be exported later and should be included as well */
const VALUE_ACCESSORS = [BooleanValueAccessor, TextValueAccessor];

@NgModule({
declarations: [AppComponent, ...VALUE_ACCESSORS],
declarations: [AppComponent],
imports: [BeeQModule.forRoot(), BrowserModule, FormsModule],
providers: [],
bootstrap: [AppComponent],
Expand All @@ -135,6 +131,22 @@ const VALUE_ACCESSORS = [BooleanValueAccessor, TextValueAccessor];
export class AppModule {}
```

> 🙋🏼‍♂️ If you are using `@beeq/angular` v1.0.1 or below, **you also need to import the values accessors**, as shown below:
```ts
...
import { BeeQModule, BooleanValueAccessor, TextValueAccessor } from '@beeq/angular';
...
const VALUE_ACCESSORS = [BooleanValueAccessor, TextValueAccessor];

@NgModule({
declarations: [AppComponent, ...VALUE_ACCESSORS],
imports: [BeeQModule.forRoot(), BrowserModule, FormsModule],
...
})
export class AppModule {}
```

### Usage

```html
Expand Down Expand Up @@ -176,3 +188,36 @@ export class AppComponent {
}
}
```

### Using BEEQ components in Angular standalone

You can also use BEEQ components in Angular standalone. To do so, you will need to import the components from `@beeq/angular/standalone` and use them as you would use any other Angular component.

```ts
import { Component } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { BqButton, BqCard, BqInput } from '@beeq/angular/standalone';

@Component({
selector: 'app-component',
standalone: true,
imports: [BqButton, BqCard, BqInput],
template: `
<bq-card>
<bq-input name="email" [value]="emailValue" (bqChange)="onInputChange($event)">
<label slot="label">Your email</label>
</bq-input>
<bq-button>Subscribe me!</bq-button>
</bq-card>
`,
styles: [],
schemas: [],
})
export class AppComponent2 {
emailValue = 'BEEQ Design System';

onInputChange(event: CustomEvent<{ value: string }>) {
console.log('emailValue', event.detail.value);
}
}
```
2 changes: 1 addition & 1 deletion packages/beeq-angular/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"module": "dist/esm2015/index.js",
"types": "index.d.ts",
"dependencies": {
"@beeq/core": "*",
"@beeq/core": "^1.0.1",
"tslib": "^2.6.2"
},
"peerDependencies": {
Expand Down
6 changes: 3 additions & 3 deletions packages/beeq-angular/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@
"executor": "@nx/angular:package",
"outputs": ["{workspaceRoot}/dist/beeq-angular"],
"options": {
"project": "packages/beeq-angular/ng-package.json"
"project": "{projectRoot}/ng-package.json"
},
"configurations": {
"production": {
"tsConfig": "packages/beeq-angular/tsconfig.lib.prod.json"
"tsConfig": "{projectRoot}/tsconfig.lib.prod.json"
},
"development": {
"tsConfig": "packages/beeq-angular/tsconfig.lib.json"
"tsConfig": "{projectRoot}/tsconfig.lib.json"
}
},
"defaultConfiguration": "production"
Expand Down
32 changes: 27 additions & 5 deletions packages/beeq-angular/src/beeq.module.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,41 @@
import { CommonModule } from '@angular/common';
import { ModuleWithProviders, NgModule } from '@angular/core';
import { CommonModule, DOCUMENT } from '@angular/common';
import { APP_INITIALIZER, ModuleWithProviders, NgModule, NgZone } from '@angular/core';
import { defineCustomElements } from '@beeq/core/dist/loader';

import { DIRECTIVES } from './directives';
import { BooleanValueAccessor } from './directives/boolean-value-accessor';
import { NumericValueAccessor } from './directives/number-value-accessor';
import { RadioValueAccessor } from './directives/radio-value-accessor';
import { SelectValueAccessor } from './directives/select-value-accessor';
import { TextValueAccessor } from './directives/text-value-accessor';

const DECLARATIONS = [
...DIRECTIVES,
// ngModel Accessors
BooleanValueAccessor,
NumericValueAccessor,
RadioValueAccessor,
SelectValueAccessor,
TextValueAccessor,
];

@NgModule({
imports: [CommonModule],
declarations: [...DIRECTIVES],
exports: [...DIRECTIVES],
declarations: DECLARATIONS,
exports: DECLARATIONS,
})
export class BeeQModule {
static forRoot(): ModuleWithProviders<BeeQModule> {
defineCustomElements();
return {
ngModule: BeeQModule,
providers: [
{
provide: APP_INITIALIZER,
useFactory: () => defineCustomElements,
multi: true,
deps: [DOCUMENT, NgZone],
},
],
};
}
}
3 changes: 3 additions & 0 deletions packages/beeq-angular/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
/* DIRECTIVES */
/* -------------------------------------------------------------------------- */
export { BooleanValueAccessor } from './directives/boolean-value-accessor';
export { NumericValueAccessor } from './directives/number-value-accessor';
export { RadioValueAccessor } from './directives/radio-value-accessor';
export { SelectValueAccessor } from './directives/select-value-accessor';
export { TextValueAccessor } from './directives/text-value-accessor';

export * from './directives/components';
Expand Down
5 changes: 5 additions & 0 deletions packages/beeq-angular/standalone/ng-package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"lib": {
"entryFile": "src/index.ts"
}
}
5 changes: 5 additions & 0 deletions packages/beeq-angular/standalone/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/* -------------------------------------------------------------------------- */
/* DIRECTIVES */
/* -------------------------------------------------------------------------- */
// @ts-ignore
export * from './directives/components';
2 changes: 1 addition & 1 deletion packages/beeq-angular/tsconfig.lib.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@
"types": []
},
"exclude": ["src/test-setup.ts", "**/*.spec.ts", "jest.config.ts", "**/*.test.ts"],
"include": ["**/*.ts"]
"include": ["src/index.ts", "standalone/src/index.ts"]
}
3 changes: 2 additions & 1 deletion packages/beeq-angular/tsconfig.lib.prod.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"extends": "./tsconfig.lib.json",
"compilerOptions": {
"declarationMap": false
"declarationMap": false,
"removeComments": false
},
"angularCompilerOptions": {
"annotateForClosureCompiler": true,
Expand Down
2 changes: 1 addition & 1 deletion packages/beeq-react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"module": "./src/index.js",
"types": "./src/index.d.ts",
"dependencies": {
"@beeq/core": "*"
"@beeq/core": "^1.0.1"
},
"peerDependencies": {
"react": ">=18.0.0",
Expand Down
3 changes: 2 additions & 1 deletion packages/beeq-react/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"allowJs": false,
"esModuleInterop": false,
"allowSyntheticDefaultImports": true,
"declaration": true
"declaration": true,
"removeComments": false
},
"files": [],
"include": [],
Expand Down
2 changes: 1 addition & 1 deletion packages/beeq-vue/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"module": "./src/index.js",
"types": "./src/index.d.ts",
"dependencies": {
"@beeq/core": "*",
"@beeq/core": "^1.0.1",
"tslib": "^2.3.0"
},
"peerDependencies": {
Expand Down
1 change: 1 addition & 0 deletions packages/beeq-vue/tsconfig.lib.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "../../dist/out-tsc",
"removeComments": false,
"types": ["node"]
},
"include": ["src/**/*.ts"],
Expand Down
1 change: 1 addition & 0 deletions packages/beeq/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
"{options.outputPath}",
"{projectRoot}/custom-elements.json",
"{workspaceRoot}/packages/beeq-angular/src/directives",
"{workspaceRoot}/packages/beeq-angular/standalone/src/directives",
"{workspaceRoot}/packages/beeq-react/src/react-component-lib",
"{workspaceRoot}/packages/beeq-vue/src/components.ts"
],
Expand Down
4 changes: 2 additions & 2 deletions packages/beeq/src/components/input/bq-input.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ export class BqInput {
this.debounceBqInput?.cancel();

if (!isHTMLElement(ev.target, 'input')) return;
this.value = ev.target.value;
this.value = this.type === 'number' ? Number(ev.target.value) : ev.target.value;

this.debounceBqInput = debounce(() => {
this.bqInput.emit({ value: this.value, el: this.el });
Expand All @@ -258,7 +258,7 @@ export class BqInput {
if (this.disabled) return;

if (!isHTMLElement(ev.target, 'input')) return;
this.value = ev.target.value;
this.value = this.type === 'number' ? Number(ev.target.value) : ev.target.value;

this.bqChange.emit({ value: this.value, el: this.el });
};
Expand Down
29 changes: 28 additions & 1 deletion packages/beeq/src/tools/angular-value-accessor-config.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,41 @@
import { ValueAccessorConfig } from '@stencil/angular-output-target';

/**
* This lets you define which components should be integrated with ngModel (i.e. form components).
* It lets you set what the target prop is (i.e. value), which event will cause the target prop to change.
*/
export const angularValueAccessorBindings: ValueAccessorConfig[] = [
{
// Boolean
elementSelectors: ['bq-checkbox', 'bq-switch'],
event: 'bqChange',
targetAttr: 'checked',
type: 'boolean',
},
{
elementSelectors: ['bq-input', 'bq-radio-group', 'bq-select', 'bq-slider', 'bq-textarea'],
// Number
elementSelectors: ['bq-input[type="number"]', 'bq-slider'],
event: 'bqChange',
targetAttr: 'value',
type: 'number',
},
{
// Radio
elementSelectors: ['bq-radio-group'],
event: 'bqChange',
targetAttr: 'value',
type: 'radio',
},
{
// Select
elementSelectors: ['bq-select'],
event: 'bqChange',
targetAttr: 'value',
type: 'select',
},
{
// Text
elementSelectors: ['bq-input:not[type="number"]', 'bq-textarea'],
event: 'bqChange',
targetAttr: 'value',
type: 'text',
Expand Down
Loading

0 comments on commit 055209f

Please sign in to comment.