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

[Feature]: 建议升级angular版本至14+,并将诸如TiTipDirective、TiValidationDirective等指令迁移至standalone,以帮助开发者更好地使用hostDirectives特性。 #21

Open
yinhaox opened this issue Nov 5, 2023 · 0 comments

Comments

@yinhaox
Copy link

yinhaox commented Nov 5, 2023

What problem does this feature solve

场景一,通过指令,为表单控件设置默认的提示信息

import {Directive, Optional} from '@angular/core';
import {TiSelectComponent, TiTextComponent, TiValidationDirective} from "@opentiny/ng";

@Directive({
  selector: 'ti-select[formControlName],input[tiText][formControlName]',
  standalone: true,
  host: {
    "[style.width.px]": "240"
  },
  hostDirectives: [
    // TiValidationDirective
  ]
})
export class DefaultDirective {
  constructor(
    @Optional() private tiValidationDirective: TiValidationDirective,
    @Optional() private tiSelect: TiSelectComponent,
    @Optional() private tiInput: TiTextComponent,
  ) {
    if (tiSelect) {
      tiSelect.placeholder = "Please Choose";
    }
    if (tiInput) {
      tiInput.nativeElement.placeholder = "Please Input";
    }
    if (tiValidationDirective) {
      tiValidationDirective.tiValidation = { tip: "Required", tipPosition: "right" };
    }
  }
}

如上指令,目前由于TiValidationDirective是非“standalone”的,为了给tiValidationDirective设置默认值,我需要对每个控件都添加tiValidation。
例如:

<ti-formfield [formGroup]="formGroup">
  <ti-item [label]="'Country'" [required]="true">
    <ti-select country formControlName="country" tiValidation></ti-select>
  </ti-item>
  <ti-item [label]="'Name'" [required]="true">
    <input formControlName="name" tiText tiValidation/>
  </ti-item>
</ti-formfield>

image

What does the proposed API look like

预期给TiValidationDirective迁移至standalone后,可以通过hostDirectives组合,如:

@Directive({
  selector: 'ti-select[formControlName],input[tiText][formControlName]',
  hostDirectives: [{
    directive: TiValidationDirective,
    inputs: ["tiValidation"],
  }]
})
export class DefaultDirective {
  // ...
}

html中就不需要每个控件都指定tiValidation

<ti-formfield [formGroup]="formGroup">
  <ti-item [label]="'Country'" [required]="true">
    <ti-select country formControlName="country"></ti-select>
  </ti-item>
  <ti-item [label]="'Name'" [required]="true">
    <input formControlName="name" tiText/>
  </ti-item>
</ti-formfield>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant