Skip to content

feat(module:cascader): support multiple selection#8903

Merged
HyperLife1119 merged 16 commits intoNG-ZORRO:masterfrom
Laffery:refactor/cascader-multiple
Dec 4, 2024
Merged

feat(module:cascader): support multiple selection#8903
HyperLife1119 merged 16 commits intoNG-ZORRO:masterfrom
Laffery:refactor/cascader-multiple

Conversation

@Laffery
Copy link
Collaborator

@Laffery Laffery commented Dec 2, 2024

PR Checklist

Please check if your PR fulfills the following requirements:

PR Type

What kind of change does this PR introduce?

  • Bugfix
  • Feature
  • Code style update (formatting, local variables)
  • Refactoring (no functional changes, no api changes)
  • Build related changes
  • CI related changes
  • Documentation content changes
  • Application (the showcase website) / infrastructure changes
  • Other... Please describe:

What is the current behavior?

Issue Number: close #6981 #8853

What is the new behavior?

Redesign the cascader component (which option list is now based on NzTreeBase) and support multiple selection

Does this PR introduce a breaking change?

  • Yes
  • No

BREAKING CHANGE

In the past, the cascader component kept a trick that if you wrote value with NzCascaderOption[] type, it extracted value by mapping each item to its value property, for example:

@Component({
	template: `<nz-cascader [nzOptions]="options" [ngModel]="value"></nz-cascader>`
})
export class ExampleComponent {
	value = [{ label: 'NG ZORRO', value: 'ng-zorro-antd' }]
}

then the value of cascader would be 'ng-zorro-antd'.

It's strange that the input and output values don't match when we haven't changed the values and it's hard to maintain. We expect that the value passed in should be the value in the list of options.

In v19, this trick is removed and if you're already using this trick in your code, please consider the add a map function to pass the actual value.


旧版本中,在表单中为 Cascader 组件赋值为 NzCascaderOption[] 类型时,Cascader 组件会根据提供的 nzValueProperty 映射成实际的值并写入,例如:

@Component({
	template: `<nz-cascader [nzOptions]="options" [ngModel]="value"></nz-cascader>`
})
export class ExampleComponent {
	value = [{ label: 'NG ZORRO', value: 'ng-zorro-antd' }]
}

此时 Cascader 组件输出的值将为 'ng-zorro-antd'。这就导致输入与输出的值不一致,可能存在潜在的数据问题。

在 v19 中,我们将移除该特性,如果您已经在代码中运用了该特性,请考虑增加一个 map 方法将其映射到实际的值。

Other information

@zorro-bot
Copy link

zorro-bot bot commented Dec 2, 2024

This preview will be available after the AzureCI is passed.

@codecov
Copy link

codecov bot commented Dec 2, 2024

Codecov Report

Attention: Patch coverage is 94.13203% with 24 lines in your changes missing coverage. Please review.

Project coverage is 91.92%. Comparing base (809155f) to head (68c1646).
Report is 1 commits behind head on master.

Files with missing lines Patch % Lines
components/cascader/cascader.component.ts 93.71% 6 Missing and 6 partials ⚠️
components/cascader/cascader-tree.service.ts 89.41% 6 Missing and 3 partials ⚠️
components/cascader/cascader.service.ts 98.00% 1 Missing and 1 partial ⚠️
components/cascader/cascader-option.component.ts 93.75% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #8903      +/-   ##
==========================================
+ Coverage   91.87%   91.92%   +0.04%     
==========================================
  Files         552      554       +2     
  Lines       19498    19702     +204     
  Branches     2895     2935      +40     
==========================================
+ Hits        17914    18111     +197     
+ Misses       1266     1265       -1     
- Partials      318      326       +8     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@Laffery Laffery added the 💔 Breaking Change This PR or the solution to this issue would introduce breaking changes label Dec 3, 2024
[value]="inputValue!"
[autofocus]="autofocus"
[showInput]="true"
[showInput]="false"
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

在 antd 中,select 组件多选时不展示 input,showInput 应为 false

image

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个特性在我们项目中使用较多,而且目前多选也没有支持传入nzShowSearch的参数 。 直接关掉的话,影响会比较大。

protected get templateOutletContext(): NzSafeAny {
return {
$implicit: this.contentTemplateOutletContext,
...this.contentTemplateOutletContext
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

传递 labelRenderContext

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

不是很理解这里为什么在使用 $implicit 后,还需要将 contentTemplateOutletContext 展开传入 🤔

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

当前 nzLabelRender template 的用法是具名 context,所以需要展开传递

image

当然我觉得也可以只用 $implicit,这里写法就会变成这样:

 <ng-template #renderTpl let-data>
    @for (label of data.labels; track label) {
      @if (!$last) {
        <span>{{ label }} /</span>
      } @else {
        <span>
          {{ label }} (
          <a href="javascript:;" (click)="handleAreaClick($event, label, selectedOptions[$index])">
            {{ data.selectedOptions[$index].code }}
          </a>
          )
        </span>
      }
    }
  </ng-template>

这将会是一个 Breaking change,需要在 release note 中说明。WDYT?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

那还是暂时保留吧

@@ -583,18 +589,6 @@ describe('cascader', () => {
expect(values![0]).toBe('zhejiang');
expect(values![1]).toBe('hangzhou');
expect(values![2]).toBe('xihu');
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

特性移除,参见 Breaking change

@Laffery Laffery marked this pull request as ready for review December 3, 2024 09:18
@Laffery Laffery removed the PR: draft label Dec 3, 2024
@Laffery Laffery requested a review from HyperLife1119 December 3, 2024 09:18
protected get templateOutletContext(): NzSafeAny {
return {
$implicit: this.contentTemplateOutletContext,
...this.contentTemplateOutletContext
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

不是很理解这里为什么在使用 $implicit 后,还需要将 contentTemplateOutletContext 展开传入 🤔

@Laffery Laffery force-pushed the refactor/cascader-multiple branch from 92fa24d to 66f8e6f Compare December 4, 2024 05:52
@Laffery Laffery requested a review from HyperLife1119 December 4, 2024 06:21
Copy link

@azimtechie azimtechie left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Copy link
Collaborator

@HyperLife1119 HyperLife1119 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

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

Labels

💔 Breaking Change This PR or the solution to this issue would introduce breaking changes Component: Cascader PR: reviewed-approved PR: target-major

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Cascader support multiple selection

4 participants