ng-zorro-antd-checkbox.mjs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. import * as i1 from '@angular/cdk/a11y';
  2. import { FocusMonitor } from '@angular/cdk/a11y';
  3. import * as i2 from '@angular/cdk/bidi';
  4. import { Directionality } from '@angular/cdk/bidi';
  5. import * as i0 from '@angular/core';
  6. import { EventEmitter, Output, ViewEncapsulation, ChangeDetectionStrategy, Component, InjectionToken, inject, effect, forwardRef, booleanAttribute, Input, ViewChild, input, signal, linkedSignal, computed, ElementRef, DestroyRef, afterNextRender, untracked, NgModule } from '@angular/core';
  7. import { toSignal, takeUntilDestroyed } from '@angular/core/rxjs-interop';
  8. import * as i3 from '@angular/forms';
  9. import { FormsModule, NG_VALUE_ACCESSOR } from '@angular/forms';
  10. import { Subject } from 'rxjs';
  11. import { takeUntil } from 'rxjs/operators';
  12. import { NzFormStatusService } from 'ng-zorro-antd/core/form';
  13. import { fromEventOutsideAngular } from 'ng-zorro-antd/core/util';
  14. /**
  15. * Use of this source code is governed by an MIT-style license that can be
  16. * found in the LICENSE file at https://github.com/NG-ZORRO/ng-zorro-antd/blob/master/LICENSE
  17. */
  18. /**
  19. * @deprecated Deprecated in v19.0.0. It is recommended to use `<nz-checkbox-group>`.
  20. */
  21. class NzCheckboxWrapperComponent {
  22. nzOnChange = new EventEmitter();
  23. checkboxList = [];
  24. addCheckbox(value) {
  25. this.checkboxList.push(value);
  26. }
  27. removeCheckbox(value) {
  28. this.checkboxList.splice(this.checkboxList.indexOf(value), 1);
  29. }
  30. onChange() {
  31. const listOfCheckedValue = this.checkboxList.filter(item => item.nzChecked).map(item => item.nzValue);
  32. this.nzOnChange.emit(listOfCheckedValue);
  33. }
  34. static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.2", ngImport: i0, type: NzCheckboxWrapperComponent, deps: [], target: i0.ɵɵFactoryTarget.Component });
  35. static ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "14.0.0", version: "19.2.2", type: NzCheckboxWrapperComponent, isStandalone: true, selector: "nz-checkbox-wrapper", outputs: { nzOnChange: "nzOnChange" }, host: { classAttribute: "ant-checkbox-group" }, exportAs: ["nzCheckboxWrapper"], ngImport: i0, template: `<ng-content></ng-content>`, isInline: true, changeDetection: i0.ChangeDetectionStrategy.OnPush, encapsulation: i0.ViewEncapsulation.None });
  36. }
  37. i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.2", ngImport: i0, type: NzCheckboxWrapperComponent, decorators: [{
  38. type: Component,
  39. args: [{
  40. selector: 'nz-checkbox-wrapper',
  41. exportAs: 'nzCheckboxWrapper',
  42. template: `<ng-content></ng-content>`,
  43. preserveWhitespaces: false,
  44. changeDetection: ChangeDetectionStrategy.OnPush,
  45. encapsulation: ViewEncapsulation.None,
  46. host: {
  47. class: 'ant-checkbox-group'
  48. }
  49. }]
  50. }], propDecorators: { nzOnChange: [{
  51. type: Output
  52. }] } });
  53. /**
  54. * Use of this source code is governed by an MIT-style license that can be
  55. * found in the LICENSE file at https://github.com/NG-ZORRO/ng-zorro-antd/blob/master/LICENSE
  56. */
  57. const NZ_CHECKBOX_GROUP = new InjectionToken('NZ_CHECKBOX_GROUP');
  58. class NzCheckboxComponent {
  59. ngZone;
  60. elementRef;
  61. cdr;
  62. focusMonitor;
  63. directionality;
  64. dir = 'ltr';
  65. destroy$ = new Subject();
  66. isNzDisableFirstChange = true;
  67. onChange = () => { };
  68. onTouched = () => { };
  69. inputElement;
  70. nzCheckedChange = new EventEmitter();
  71. nzValue = null;
  72. nzAutoFocus = false;
  73. nzDisabled = false;
  74. nzIndeterminate = false;
  75. nzChecked = false;
  76. nzId = null;
  77. nzName = null;
  78. innerCheckedChange(checked) {
  79. if (!this.nzDisabled && !this.checkboxGroupComponent?.finalDisabled()) {
  80. this.setValue(checked);
  81. this.nzCheckboxWrapperComponent?.onChange();
  82. this.checkboxGroupComponent?.onCheckedChange(this.nzValue, checked);
  83. }
  84. }
  85. writeValue(value) {
  86. this.nzChecked = value;
  87. this.cdr.markForCheck();
  88. }
  89. registerOnChange(fn) {
  90. this.onChange = fn;
  91. }
  92. registerOnTouched(fn) {
  93. this.onTouched = fn;
  94. }
  95. setDisabledState(disabled) {
  96. this.nzDisabled = (this.isNzDisableFirstChange && this.nzDisabled) || disabled;
  97. this.isNzDisableFirstChange = false;
  98. this.cdr.markForCheck();
  99. }
  100. focus() {
  101. this.focusMonitor.focusVia(this.inputElement, 'keyboard');
  102. }
  103. blur() {
  104. this.inputElement.nativeElement.blur();
  105. }
  106. /** @deprecated */
  107. nzCheckboxWrapperComponent = inject(NzCheckboxWrapperComponent, { optional: true });
  108. checkboxGroupComponent = inject(NZ_CHECKBOX_GROUP, { optional: true });
  109. nzFormStatusService = inject(NzFormStatusService, { optional: true });
  110. constructor(ngZone, elementRef, cdr, focusMonitor, directionality) {
  111. this.ngZone = ngZone;
  112. this.elementRef = elementRef;
  113. this.cdr = cdr;
  114. this.focusMonitor = focusMonitor;
  115. this.directionality = directionality;
  116. if (this.checkboxGroupComponent) {
  117. effect(() => {
  118. const values = this.checkboxGroupComponent.value() || [];
  119. this.setValue(values.includes(this.nzValue));
  120. this.cdr.markForCheck();
  121. });
  122. }
  123. }
  124. ngOnInit() {
  125. this.focusMonitor
  126. .monitor(this.elementRef, true)
  127. .pipe(takeUntil(this.destroy$))
  128. .subscribe(focusOrigin => {
  129. if (!focusOrigin) {
  130. Promise.resolve().then(() => this.onTouched());
  131. }
  132. });
  133. this.nzCheckboxWrapperComponent?.addCheckbox(this);
  134. this.directionality.change.pipe(takeUntil(this.destroy$)).subscribe((direction) => {
  135. this.dir = direction;
  136. this.cdr.detectChanges();
  137. });
  138. this.dir = this.directionality.value;
  139. fromEventOutsideAngular(this.elementRef.nativeElement, 'click')
  140. .pipe(takeUntil(this.destroy$))
  141. .subscribe(event => {
  142. event.preventDefault();
  143. this.focus();
  144. if (this.nzDisabled) {
  145. return;
  146. }
  147. this.ngZone.run(() => {
  148. this.innerCheckedChange(!this.nzChecked);
  149. this.cdr.markForCheck();
  150. });
  151. });
  152. fromEventOutsideAngular(this.inputElement.nativeElement, 'click')
  153. .pipe(takeUntil(this.destroy$))
  154. .subscribe(event => event.stopPropagation());
  155. }
  156. ngAfterViewInit() {
  157. if (this.nzAutoFocus) {
  158. this.focus();
  159. }
  160. }
  161. ngOnDestroy() {
  162. this.focusMonitor.stopMonitoring(this.elementRef);
  163. this.nzCheckboxWrapperComponent?.removeCheckbox(this);
  164. this.destroy$.next();
  165. this.destroy$.complete();
  166. }
  167. setValue(value) {
  168. this.nzChecked = value;
  169. this.onChange(value);
  170. this.nzCheckedChange.emit(value);
  171. }
  172. static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.2", ngImport: i0, type: NzCheckboxComponent, deps: [{ token: i0.NgZone }, { token: i0.ElementRef }, { token: i0.ChangeDetectorRef }, { token: i1.FocusMonitor }, { token: i2.Directionality }], target: i0.ɵɵFactoryTarget.Component });
  173. static ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "16.1.0", version: "19.2.2", type: NzCheckboxComponent, isStandalone: true, selector: "[nz-checkbox]", inputs: { nzValue: "nzValue", nzAutoFocus: ["nzAutoFocus", "nzAutoFocus", booleanAttribute], nzDisabled: ["nzDisabled", "nzDisabled", booleanAttribute], nzIndeterminate: ["nzIndeterminate", "nzIndeterminate", booleanAttribute], nzChecked: ["nzChecked", "nzChecked", booleanAttribute], nzId: "nzId", nzName: "nzName" }, outputs: { nzCheckedChange: "nzCheckedChange" }, host: { properties: { "class.ant-checkbox-group-item": "!!checkboxGroupComponent", "class.ant-checkbox-wrapper-in-form-item": "!!nzFormStatusService", "class.ant-checkbox-wrapper-checked": "nzChecked", "class.ant-checkbox-wrapper-disabled": "nzDisabled || checkboxGroupComponent?.finalDisabled()", "class.ant-checkbox-rtl": "dir === 'rtl'" }, classAttribute: "ant-checkbox-wrapper" }, providers: [
  174. {
  175. provide: NG_VALUE_ACCESSOR,
  176. useExisting: forwardRef(() => NzCheckboxComponent),
  177. multi: true
  178. }
  179. ], viewQueries: [{ propertyName: "inputElement", first: true, predicate: ["inputElement"], descendants: true, static: true }], exportAs: ["nzCheckbox"], ngImport: i0, template: `
  180. <span
  181. class="ant-checkbox"
  182. [class.ant-checkbox-checked]="nzChecked && !nzIndeterminate"
  183. [class.ant-checkbox-disabled]="nzDisabled || checkboxGroupComponent?.finalDisabled()"
  184. [class.ant-checkbox-indeterminate]="nzIndeterminate"
  185. >
  186. <input
  187. #inputElement
  188. type="checkbox"
  189. class="ant-checkbox-input"
  190. [attr.autofocus]="nzAutoFocus ? 'autofocus' : null"
  191. [attr.id]="nzId"
  192. [attr.name]="nzName || checkboxGroupComponent?.nzName()"
  193. [checked]="nzChecked"
  194. [ngModel]="nzChecked"
  195. [disabled]="nzDisabled || (checkboxGroupComponent?.finalDisabled() ?? false)"
  196. (ngModelChange)="innerCheckedChange($event)"
  197. />
  198. <span class="ant-checkbox-inner"></span>
  199. </span>
  200. <span><ng-content></ng-content></span>
  201. `, isInline: true, dependencies: [{ kind: "ngmodule", type: FormsModule }, { kind: "directive", type: i3.CheckboxControlValueAccessor, selector: "input[type=checkbox][formControlName],input[type=checkbox][formControl],input[type=checkbox][ngModel]" }, { kind: "directive", type: i3.NgControlStatus, selector: "[formControlName],[ngModel],[formControl]" }, { kind: "directive", type: i3.NgModel, selector: "[ngModel]:not([formControlName]):not([formControl])", inputs: ["name", "disabled", "ngModel", "ngModelOptions"], outputs: ["ngModelChange"], exportAs: ["ngModel"] }], changeDetection: i0.ChangeDetectionStrategy.OnPush, encapsulation: i0.ViewEncapsulation.None });
  202. }
  203. i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.2", ngImport: i0, type: NzCheckboxComponent, decorators: [{
  204. type: Component,
  205. args: [{
  206. selector: '[nz-checkbox]',
  207. exportAs: 'nzCheckbox',
  208. preserveWhitespaces: false,
  209. changeDetection: ChangeDetectionStrategy.OnPush,
  210. encapsulation: ViewEncapsulation.None,
  211. template: `
  212. <span
  213. class="ant-checkbox"
  214. [class.ant-checkbox-checked]="nzChecked && !nzIndeterminate"
  215. [class.ant-checkbox-disabled]="nzDisabled || checkboxGroupComponent?.finalDisabled()"
  216. [class.ant-checkbox-indeterminate]="nzIndeterminate"
  217. >
  218. <input
  219. #inputElement
  220. type="checkbox"
  221. class="ant-checkbox-input"
  222. [attr.autofocus]="nzAutoFocus ? 'autofocus' : null"
  223. [attr.id]="nzId"
  224. [attr.name]="nzName || checkboxGroupComponent?.nzName()"
  225. [checked]="nzChecked"
  226. [ngModel]="nzChecked"
  227. [disabled]="nzDisabled || (checkboxGroupComponent?.finalDisabled() ?? false)"
  228. (ngModelChange)="innerCheckedChange($event)"
  229. />
  230. <span class="ant-checkbox-inner"></span>
  231. </span>
  232. <span><ng-content></ng-content></span>
  233. `,
  234. providers: [
  235. {
  236. provide: NG_VALUE_ACCESSOR,
  237. useExisting: forwardRef(() => NzCheckboxComponent),
  238. multi: true
  239. }
  240. ],
  241. host: {
  242. class: 'ant-checkbox-wrapper',
  243. '[class.ant-checkbox-group-item]': '!!checkboxGroupComponent',
  244. '[class.ant-checkbox-wrapper-in-form-item]': '!!nzFormStatusService',
  245. '[class.ant-checkbox-wrapper-checked]': 'nzChecked',
  246. '[class.ant-checkbox-wrapper-disabled]': 'nzDisabled || checkboxGroupComponent?.finalDisabled()',
  247. '[class.ant-checkbox-rtl]': `dir === 'rtl'`
  248. },
  249. imports: [FormsModule]
  250. }]
  251. }], ctorParameters: () => [{ type: i0.NgZone }, { type: i0.ElementRef }, { type: i0.ChangeDetectorRef }, { type: i1.FocusMonitor }, { type: i2.Directionality }], propDecorators: { inputElement: [{
  252. type: ViewChild,
  253. args: ['inputElement', { static: true }]
  254. }], nzCheckedChange: [{
  255. type: Output
  256. }], nzValue: [{
  257. type: Input
  258. }], nzAutoFocus: [{
  259. type: Input,
  260. args: [{ transform: booleanAttribute }]
  261. }], nzDisabled: [{
  262. type: Input,
  263. args: [{ transform: booleanAttribute }]
  264. }], nzIndeterminate: [{
  265. type: Input,
  266. args: [{ transform: booleanAttribute }]
  267. }], nzChecked: [{
  268. type: Input,
  269. args: [{ transform: booleanAttribute }]
  270. }], nzId: [{
  271. type: Input
  272. }], nzName: [{
  273. type: Input
  274. }] } });
  275. /**
  276. * Use of this source code is governed by an MIT-style license that can be
  277. * found in the LICENSE file at https://github.com/NG-ZORRO/ng-zorro-antd/blob/master/LICENSE
  278. */
  279. class NzCheckboxGroupComponent {
  280. onChange = () => { };
  281. onTouched = () => { };
  282. isDisabledFirstChange = true;
  283. directionality = inject(Directionality);
  284. nzName = input(null);
  285. nzDisabled = input(false, { transform: booleanAttribute });
  286. nzOptions = input([]);
  287. value = signal(null);
  288. finalDisabled = linkedSignal(() => this.nzDisabled());
  289. dir = toSignal(this.directionality.change, { initialValue: this.directionality.value });
  290. normalizedOptions = computed(() => normalizeOptions(this.nzOptions()));
  291. constructor() {
  292. const elementRef = inject(ElementRef);
  293. const focusMonitor = inject(FocusMonitor);
  294. const destroyRef = inject(DestroyRef);
  295. afterNextRender(() => {
  296. focusMonitor
  297. .monitor(elementRef, true)
  298. .pipe(takeUntilDestroyed(destroyRef))
  299. .subscribe(focusOrigin => {
  300. if (!focusOrigin) {
  301. this.onTouched();
  302. }
  303. });
  304. destroyRef.onDestroy(() => {
  305. focusMonitor.stopMonitoring(elementRef);
  306. });
  307. });
  308. }
  309. writeValue(value) {
  310. untracked(() => {
  311. this.value.set(value);
  312. });
  313. }
  314. registerOnChange(fn) {
  315. this.onChange = fn;
  316. }
  317. registerOnTouched(fn) {
  318. this.onTouched = fn;
  319. }
  320. setDisabledState(disabled) {
  321. untracked(() => {
  322. this.finalDisabled.set((this.isDisabledFirstChange && this.nzDisabled()) || disabled);
  323. });
  324. this.isDisabledFirstChange = false;
  325. }
  326. onCheckedChange(optionValue, checked) {
  327. if (this.finalDisabled())
  328. return;
  329. this.value.update(value => {
  330. if (checked) {
  331. return value?.concat(optionValue) || [optionValue];
  332. }
  333. else {
  334. return value?.filter(val => val !== optionValue) || [];
  335. }
  336. });
  337. this.onChange(this.value());
  338. }
  339. static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.2", ngImport: i0, type: NzCheckboxGroupComponent, deps: [], target: i0.ɵɵFactoryTarget.Component });
  340. static ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "17.0.0", version: "19.2.2", type: NzCheckboxGroupComponent, isStandalone: true, selector: "nz-checkbox-group", inputs: { nzName: { classPropertyName: "nzName", publicName: "nzName", isSignal: true, isRequired: false, transformFunction: null }, nzDisabled: { classPropertyName: "nzDisabled", publicName: "nzDisabled", isSignal: true, isRequired: false, transformFunction: null }, nzOptions: { classPropertyName: "nzOptions", publicName: "nzOptions", isSignal: true, isRequired: false, transformFunction: null } }, host: { properties: { "class.ant-checkbox-group-rtl": "dir() === 'rtl'" }, classAttribute: "ant-checkbox-group" }, providers: [
  341. {
  342. provide: NG_VALUE_ACCESSOR,
  343. useExisting: forwardRef(() => NzCheckboxGroupComponent),
  344. multi: true
  345. },
  346. {
  347. provide: NZ_CHECKBOX_GROUP,
  348. useExisting: forwardRef(() => NzCheckboxGroupComponent)
  349. }
  350. ], exportAs: ["nzCheckboxGroup"], ngImport: i0, template: `
  351. <ng-content>
  352. @for (option of normalizedOptions(); track option.value) {
  353. <label
  354. nz-checkbox
  355. [nzValue]="option.value"
  356. [nzName]="nzName()"
  357. [nzDisabled]="option.disabled || finalDisabled()"
  358. >
  359. {{ option.label }}
  360. </label>
  361. }
  362. </ng-content>
  363. `, isInline: true, dependencies: [{ kind: "component", type: NzCheckboxComponent, selector: "[nz-checkbox]", inputs: ["nzValue", "nzAutoFocus", "nzDisabled", "nzIndeterminate", "nzChecked", "nzId", "nzName"], outputs: ["nzCheckedChange"], exportAs: ["nzCheckbox"] }], changeDetection: i0.ChangeDetectionStrategy.OnPush, encapsulation: i0.ViewEncapsulation.None });
  364. }
  365. i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.2", ngImport: i0, type: NzCheckboxGroupComponent, decorators: [{
  366. type: Component,
  367. args: [{
  368. selector: 'nz-checkbox-group',
  369. exportAs: 'nzCheckboxGroup',
  370. imports: [NzCheckboxComponent],
  371. template: `
  372. <ng-content>
  373. @for (option of normalizedOptions(); track option.value) {
  374. <label
  375. nz-checkbox
  376. [nzValue]="option.value"
  377. [nzName]="nzName()"
  378. [nzDisabled]="option.disabled || finalDisabled()"
  379. >
  380. {{ option.label }}
  381. </label>
  382. }
  383. </ng-content>
  384. `,
  385. providers: [
  386. {
  387. provide: NG_VALUE_ACCESSOR,
  388. useExisting: forwardRef(() => NzCheckboxGroupComponent),
  389. multi: true
  390. },
  391. {
  392. provide: NZ_CHECKBOX_GROUP,
  393. useExisting: forwardRef(() => NzCheckboxGroupComponent)
  394. }
  395. ],
  396. host: {
  397. class: 'ant-checkbox-group',
  398. '[class.ant-checkbox-group-rtl]': `dir() === 'rtl'`
  399. },
  400. encapsulation: ViewEncapsulation.None,
  401. changeDetection: ChangeDetectionStrategy.OnPush
  402. }]
  403. }], ctorParameters: () => [] });
  404. function normalizeOptions(value) {
  405. return value.map(item => {
  406. if (typeof item === 'string' || typeof item === 'number') {
  407. return {
  408. label: `${item}`,
  409. value: item
  410. };
  411. }
  412. return item;
  413. });
  414. }
  415. /**
  416. * Use of this source code is governed by an MIT-style license that can be
  417. * found in the LICENSE file at https://github.com/NG-ZORRO/ng-zorro-antd/blob/master/LICENSE
  418. */
  419. class NzCheckboxModule {
  420. static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.2", ngImport: i0, type: NzCheckboxModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule });
  421. static ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "19.2.2", ngImport: i0, type: NzCheckboxModule, imports: [NzCheckboxComponent, NzCheckboxGroupComponent, NzCheckboxWrapperComponent], exports: [NzCheckboxComponent, NzCheckboxGroupComponent, NzCheckboxWrapperComponent] });
  422. static ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "19.2.2", ngImport: i0, type: NzCheckboxModule, imports: [NzCheckboxComponent, NzCheckboxGroupComponent] });
  423. }
  424. i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.2", ngImport: i0, type: NzCheckboxModule, decorators: [{
  425. type: NgModule,
  426. args: [{
  427. imports: [NzCheckboxComponent, NzCheckboxGroupComponent, NzCheckboxWrapperComponent],
  428. exports: [NzCheckboxComponent, NzCheckboxGroupComponent, NzCheckboxWrapperComponent]
  429. }]
  430. }] });
  431. /**
  432. * Use of this source code is governed by an MIT-style license that can be
  433. * found in the LICENSE file at https://github.com/NG-ZORRO/ng-zorro-antd/blob/master/LICENSE
  434. */
  435. /**
  436. * Generated bundle index. Do not edit.
  437. */
  438. export { NZ_CHECKBOX_GROUP, NzCheckboxComponent, NzCheckboxGroupComponent, NzCheckboxModule, NzCheckboxWrapperComponent };
  439. //# sourceMappingURL=ng-zorro-antd-checkbox.mjs.map