Skip to content

Commit 7af1397

Browse files
authoredAug 14, 2022
feat(form): add visibleIfLogical logical type (#1496)
1 parent 9a0c96e commit 7af1397

File tree

5 files changed

+76
-7
lines changed

5 files changed

+76
-7
lines changed
 

‎packages/form/docs/schema.en-US.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -264,8 +264,9 @@ Equals to `<sf [ui]="ui">`, a group of UI structure corresponds to JSON Schema s
264264
| `[debug]` | Debug mode | `boolean` | - |
265265
| `[order]` | Order of property | `string[]` | - |
266266
| `[asyncData]` | Asynchronized static data source | `(input?: any) => Observable<SFSchemaEnumType[]>` | - |
267-
| `[hidden]` | If hide | `boolean` | `false` |
267+
| `[hidden]` | Whether to hide | `boolean` | `false` |
268268
| `[visibleIf]` | Is visible with conditions | `{ [key: string]: any[] | ((value: any, property: FormProperty) => boolean) }` | - |
269+
| `[visibleIfLogical]` | The logical used when specifying multiple `visibleIf` | `or, and` | `or` |
269270
| `[acl]` | ACL permission (Use `can()` verify) | `ACLCanType` | - |
270271

271272
**visibleIf**

‎packages/form/docs/schema.zh-CN.md

+1
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,7 @@ UI Schema 结构由通用性和小部件API两部分组成,以下是通用性
266266
| `[asyncData]` | 异步静态数据源 | `(input?: any) => Observable<SFSchemaEnumType[]>` | - |
267267
| `[hidden]` | 是否隐藏渲染 | `boolean` | `false` |
268268
| `[visibleIf]` | 指定条件时才显示 | `{ [key: string]: any[] | ((value: any, property: FormProperty) => boolean) }` | - |
269+
| `[visibleIfLogical]` | 指定多个 `visibleIf` 时采用的逻辑关系 | `or, and` | `or` |
269270
| `[acl]` | ACL权限,等同 `can()` 参数值 | `ACLCanType` | - |
270271

271272
**visibleIf**

‎packages/form/spec/schema.spec.ts

+52
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,58 @@ describe('form: schema', () => {
459459
labels[1].click();
460460
page.checkCount('.j-mobile', 0).checkCount('.j-name', 1).checkCount('.j-any', 1);
461461
});
462+
it('logical or', fakeAsync(() => {
463+
page.newSchema({
464+
properties: {
465+
show: {
466+
type: 'boolean',
467+
ui: {
468+
visibleIf: { t1: ['$ANY$'], t2: ['$ANY$'] },
469+
visibleIfLogical: 'or',
470+
class: 'vi-show'
471+
}
472+
},
473+
t1: {
474+
type: 'string',
475+
ui: { class: 'vi-t1' }
476+
},
477+
t2: {
478+
type: 'string',
479+
ui: { class: 'vi-t2' }
480+
}
481+
}
482+
});
483+
page.typeChar('t1', '.vi-t1 input');
484+
page.checkCount('.vi-show', 1);
485+
page.typeChar('t2', '.vi-t2 input');
486+
page.checkCount('.vi-show', 1);
487+
}));
488+
it('logical and', fakeAsync(() => {
489+
page.newSchema({
490+
properties: {
491+
show: {
492+
type: 'boolean',
493+
ui: {
494+
visibleIf: { t1: ['$ANY$'], t2: ['$ANY$'] },
495+
visibleIfLogical: 'and',
496+
class: 'vi-show'
497+
}
498+
},
499+
t1: {
500+
type: 'string',
501+
ui: { class: 'vi-t1' }
502+
},
503+
t2: {
504+
type: 'string',
505+
ui: { class: 'vi-t2' }
506+
}
507+
}
508+
});
509+
page.typeChar('t1', '.vi-t1 input');
510+
page.checkCount('.vi-show', 0);
511+
page.typeChar('t2', '.vi-t2 input');
512+
page.checkCount('.vi-show', 1);
513+
}));
462514
});
463515

464516
describe('[order]', () => {

‎packages/form/src/model/form.property.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,7 @@ export abstract class FormProperty {
364364

365365
combineLatest(propertiesBinding)
366366
.pipe(
367-
map(values => values.indexOf(true) !== -1),
367+
map(values => (this.ui.visibleIfLogical === 'and' ? values.every(v => v) : values.some(v => v))),
368368
distinctUntilChanged()
369369
)
370370
.subscribe(visible => this.setVisible(visible));

‎packages/form/src/schema/ui.ts

+20-5
Original file line numberDiff line numberDiff line change
@@ -183,25 +183,33 @@ export interface SFVisibleIf {
183183
export interface SFUISchemaItem extends SFRenderSchema, SFHorizontalLayoutSchema, ErrorSchema, SFSchemaI18n {
184184
[key: string]: NzSafeAny;
185185

186-
/** 是否开启调试模式,在数据变更、校验会打印出相信信息,不建议在生产环境中使用 */
186+
/**
187+
* Whether to enable the debug mode, the trust information will be printed out when the data is changed and verified, it is not recommended to use it in the production environment
188+
*
189+
* 是否开启调试模式,在数据变更、校验会打印出相信信息,不建议在生产环境中使用
190+
*/
187191
debug?: boolean;
188192

189193
/**
190-
* 属性顺序
194+
* Order of property
191195
*
192-
* 当你只想某几个属性靠前时,则允许使用通配符 `*` 来表示剩余部分,且只允许出现一次
196+
* 属性顺序,当你只想某几个属性靠前时,则允许使用通配符 `*` 来表示剩余部分,且只允许出现一次
193197
*
194198
* @example
195199
*
196200
* [ 'a', 'b', 'c', 'd' ] + [ 'c', 'b', '*' ] = [ 'c', 'b', 'a', 'd']
197201
*/
198202
order?: string[];
199203
/**
204+
* Whether to hide
205+
*
200206
* 是否隐藏
201207
*/
202208
hidden?: boolean;
203209
/**
204-
* 指定条件时才显示,但需要**注意**:
210+
* Is visible with conditions (When there are multiple, you can specify logic vis `visibleIfLogical`)
211+
*
212+
* 指定条件时才显示(当多个时可以通过 `visibleIfLogical` 来指定处理逻辑),但需要**注意**:
205213
* - 键值表示监听对象属性名
206214
* - JSON Schema 校验是各属性独立运行,监听对象属性每一次值变化都会重新做一次整个JSON结构计算
207215
*
@@ -212,6 +220,13 @@ export interface SFUISchemaItem extends SFRenderSchema, SFHorizontalLayoutSchema
212220
*/
213221
visibleIf?: SFVisibleIf;
214222

223+
/**
224+
* The logical used when specifying multiple `visibleIf`, default: `or`
225+
*
226+
* 指定多个 `visibleIf` 时采用的逻辑关系,默认:`or`
227+
*/
228+
visibleIfLogical?: 'or' | 'and';
229+
215230
/**
216231
* ACL 配置
217232
*/
@@ -228,7 +243,7 @@ export interface SFUISchema {
228243
}
229244

230245
/**
231-
* 内部运行时使用
246+
* @inner
232247
*/
233248
export interface SFUISchemaItemRun extends SFUISchemaItem {
234249
/** @internal 自定义模板 */

0 commit comments

Comments
 (0)
Please sign in to comment.