Skip to content

Commit b9e433d

Browse files
authoredSep 10, 2022
fix(abc:st): fix built-in style into to className (#1525)
1 parent c43afb8 commit b9e433d

File tree

4 files changed

+65
-20
lines changed

4 files changed

+65
-20
lines changed
 

‎packages/abc/st/st-column-source.ts

+36-11
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { ACLService } from '@delon/acl';
55
import { AlainI18NService, ALAIN_I18N_TOKEN } from '@delon/theme';
66
import { AlainSTConfig } from '@delon/util/config';
77
import { deepCopy, warn } from '@delon/util/other';
8-
import type { NzSafeAny } from 'ng-zorro-antd/core/types';
8+
import type { NgClassInterface, NzSafeAny } from 'ng-zorro-antd/core/types';
99

1010
import { STRowSource } from './st-row.directive';
1111
import { STWidgetRegistry } from './st-widget';
@@ -363,6 +363,40 @@ export class STColumnSource {
363363
return res;
364364
}
365365

366+
private mergeClass(item: _STColumn): void {
367+
const builtInClassNames: string[] = [];
368+
if (item._isTruncate) {
369+
builtInClassNames.push('text-truncate');
370+
}
371+
const rawClassName = item.className;
372+
if (!rawClassName) {
373+
builtInClassNames.push(
374+
(
375+
{
376+
number: 'text-right',
377+
currency: 'text-right',
378+
date: 'text-center'
379+
} as NzSafeAny
380+
)[item.type!]
381+
);
382+
item._className = builtInClassNames.filter(w => !!w);
383+
return;
384+
}
385+
386+
const rawClassNameIsArray = Array.isArray(rawClassName);
387+
if (!rawClassNameIsArray && typeof rawClassName === 'object') {
388+
const objClassNames: NgClassInterface = rawClassName;
389+
builtInClassNames.forEach(key => (objClassNames[key] = true));
390+
item._className = objClassNames;
391+
return;
392+
}
393+
394+
const arrayClassNames = rawClassNameIsArray ? Array.from(rawClassName as string[]) : [rawClassName];
395+
arrayClassNames.splice(0, 0, ...builtInClassNames);
396+
item._className = [...new Set(arrayClassNames)].filter(w => !!w);
397+
console.log(arrayClassNames);
398+
}
399+
366400
process(
367401
list: STColumn[],
368402
options: STColumnSourceProcessOptions
@@ -440,16 +474,7 @@ export class STColumnSource {
440474
}
441475
item._isTruncate = !!item.width && options.widthMode.strictBehavior === 'truncate' && item.type !== 'img';
442476
// className
443-
if (!item.className) {
444-
item.className = (
445-
{
446-
number: 'text-right',
447-
currency: 'text-right',
448-
date: 'text-center'
449-
} as NzSafeAny
450-
)[item.type!];
451-
}
452-
item._className = item.className || (item._isTruncate ? 'text-truncate' : null);
477+
this.mergeClass(item);
453478
// width
454479
if (typeof item.width === 'number') {
455480
item._width = item.width;

‎packages/abc/st/st.component.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@
170170
[nzLeft]="!!c._left"
171171
[nzRight]="!!c._right"
172172
[attr.data-col-index]="cIdx"
173-
[ngClass]="c._className!"
173+
[ngClass]="c._className"
174174
[attr.colspan]="c.colSpan"
175175
>
176176
<span *ngIf="responsive" class="ant-table-rep__title">

‎packages/abc/st/test/st-column-source.spec.ts

+27-7
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import { ACLService } from '@delon/acl';
44
import { AlainI18NService, AlainI18NServiceFake } from '@delon/theme';
55
import { deepGet } from '@delon/util/other';
6-
import { NzSafeAny } from 'ng-zorro-antd/core/types';
6+
import { NgClassInterface, NzSafeAny } from 'ng-zorro-antd/core/types';
77

88
import { STColumnSource, STColumnSourceProcessOptions } from '../st-column-source';
99
import { STRowSource } from '../st-row.directive';
@@ -217,16 +217,32 @@ describe('st: column-source', () => {
217217
});
218218
describe('[className]', () => {
219219
it('should be custom class name', () => {
220-
page.expectValue([{ title: '', type: 'number', className: 'aa' }], 'aa', 'className');
220+
page.expectValue([{ title: '', className: 'aa' }], 'aa', '_className', true);
221221
});
222222
it('should be auto text-right when type is number', () => {
223-
page.expectValue([{ title: '', type: 'number' }], 'text-right', 'className');
223+
page.expectValue([{ title: '', type: 'number' }], 'text-right', '_className', true);
224224
});
225225
it('should be auto text-right when type is currency', () => {
226-
page.expectValue([{ title: '', type: 'currency' }], 'text-right', 'className');
226+
page.expectValue([{ title: '', type: 'currency' }], 'text-right', '_className', true);
227227
});
228228
it('should be auto text-center when type is date', () => {
229-
page.expectValue([{ title: '', type: 'date' }], 'text-center', 'className');
229+
page.expectValue([{ title: '', type: 'date' }], 'text-center', '_className', true);
230+
});
231+
it('should be working when className is object', () => {
232+
const res = srv.process([{ title: '', width: 10, type: 'number', className: { a: true, b: false } }], {
233+
widthMode: { strictBehavior: 'truncate' },
234+
safeType: 'html'
235+
}).columns;
236+
const obj = res[0]._className as NgClassInterface;
237+
expect(obj['text-truncate']).toBe(true);
238+
});
239+
it('should be remove duplicates', () => {
240+
const res = srv.process(
241+
[{ title: '', type: 'date', className: ['text-center', 'text-center'] }],
242+
options
243+
).columns;
244+
expect((res[0]._className as string[]).length).toBe(1);
245+
expect(res[0]._className).toContain('text-center');
230246
});
231247
});
232248
describe('[iif]', () => {
@@ -729,10 +745,14 @@ describe('st: column-source', () => {
729745
});
730746

731747
class PageObject {
732-
expectValue(columns: STColumn[], value: any, path: string = 'indexKey'): this {
748+
expectValue(columns: STColumn[], value: any, path: string = 'indexKey', contain: boolean = false): this {
733749
const newColumns = srv.process(columns, options).columns;
734750
expect(newColumns.length).toBeGreaterThan(0);
735-
expect(deepGet(newColumns[0], path)).toBe(value);
751+
if (contain) {
752+
expect(deepGet(newColumns[0], path)).toContain(value);
753+
} else {
754+
expect(deepGet(newColumns[0], path)).toBe(value);
755+
}
736756
return this;
737757
}
738758
expectBtnValue(columns: STColumn[], value: any, path: string = 'indexKey'): this {

‎packages/abc/st/test/st.spec.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1394,7 +1394,7 @@ describe('abc: st', () => {
13941394
.cd()
13951395
.updateColumn([{ title: '', index: 'id', width: 50, className: 'aaaa' }])
13961396
.expectElCount(`.st__width-strict`, 1)
1397-
.expectElCount(`.text-truncate`, 0)
1397+
.expectElCount(`.text-truncate`, context.comp._data.length)
13981398
.expectElCount(`td.aaaa`, context.comp._data.length)
13991399
.asyncEnd();
14001400
}));

0 commit comments

Comments
 (0)
Please sign in to comment.