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

Is there a simple way to disable ok button in a modal #3743

Closed
MaoScut opened this issue Jul 6, 2019 · 5 comments
Closed

Is there a simple way to disable ok button in a modal #3743

MaoScut opened this issue Jul 6, 2019 · 5 comments

Comments

@MaoScut
Copy link

MaoScut commented Jul 6, 2019

What problem does this feature solve?

disable a ok button is quite common, but it is too exhausted to use nzFooter.

What does the proposed API look like?

currently, if I want to disable a ok button when a formGroup is invalid, the code will be like below.

@Injectable({
  providedIn: 'root',
})
export class MyService {
  tplModal: NzModalRef;
  constructor(
    private modalService: NzModalService,
    private fb: FormBuilder,
    public entityService: EntityService,
  ) {
  }

  perform() {
    const formGroup = this.fb.group({
      name: ['n', Validators.required],
    });
    // declaration 'entityService', 'modal' only for nzFooter[0].onClick!
    const entityService = this.entityService;
    const modal: NzModalRef = this.tplModal = this.modalService.create({
      nzTitle: 'create',
      nzContent: SimpleComponent,
      nzComponentParams: {
        formGroup,
      },
      nzFooter: [{
        type: 'primary',
        label: 'submit',
        disabled: (instance) => instance.formGroup.invalid,
        onClick(instance) {
          return entityService.add(instance.formGroup.value).toPromise().then(
            // manually call destroy!
            () => modal.destroy(),
          );
        },
      }, {
        // config cancel button manually!
      }],
    });
  }
}

but if ModalOptionsForService.nzOkDisabled can accept a function takes contentComponentInstance as arg, the 'perform' method above can be simplify.

  perform() {
    const formGroup = this.fb.group({
      name: ['n', Validators.required],
    });
    this.tplModal = this.modalService.create({
      nzTitle: 'create',
      nzContent: SimpleComponent,
      nzComponentParams: {
        formGroup,
      },
      nzOnOk: (instance) => this.entityService.add(instance.formGroup.value).toPromise(),
      nzOkDisabled: (instance) => instance.formGroup.invalid,
    });
  }
```<!-- generated by ng-zorro-issue-helper. DO NOT REMOVE -->
@MaoScut
Copy link
Author

MaoScut commented Jul 6, 2019

I read the soure code nz-modal.service.ts. The method 'getInstance' of NzModalRef can feat my requirement. But it is not mentioned in the official doc.
use this method, the code becomes as below.

  perform() {
    const formGroup = this.fb.group({
      name: ['n', Validators.required],
    });
    this.tplModal = this.modalService.create({
      nzTitle: 'create',
      nzContent: SimpleComponent,
      nzComponentParams: {
        formGroup,
      },
      nzOnOk: (instance) => this.entityService.add(instance.formGroup.value).toPromise(),
    });
    formGroup.statusChanges.subscribe((state) => {
      this.tplModal.getInstance().nzOkDisabled = state === 'INVALID';
    });
  }

Is this api safe/stable to use?

@krokofant
Copy link
Contributor

krokofant commented Mar 19, 2020

The getInstance api does not seem stable to use as it it now removed in the beta 😕

Alternatives might be NzModalRef.updateConfig({})

@vthinkxie
Copy link
Member

The getInstance api does not seem stable to use as it it now removed in the beta 😕

Alternatives might be NzModalRef.updateConfig({})

cc @hsuanxyz

@hsuanxyz
Copy link
Member

hsuanxyz commented Mar 24, 2020

@krokofant getInstance will be unstable when using template mode(if closed), we'll public the updateConfig method, it works in template mode and service mode.

hsuanxyz added a commit to hsuanxyz/ng-zorro-antd that referenced this issue May 26, 2020
hsuanxyz added a commit to hsuanxyz/ng-zorro-antd that referenced this issue May 26, 2020
hsuanxyz added a commit to hsuanxyz/ng-zorro-antd that referenced this issue Jun 16, 2020
hsuanxyz added a commit to hsuanxyz/ng-zorro-antd that referenced this issue Aug 5, 2020
…5332)

* fix(module:modal): some cases there is no changes detected

close NG-ZORRO#5328, close NG-ZORRO#5287, close NG-ZORRO#5259

* docs(module:modal): update the API docs

close NG-ZORRO#3743
@daiyang123456
Copy link

updateConfig

but,it is invalid

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants