Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
137 changes: 137 additions & 0 deletions components/table/__tests__/table-831.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
import { describe, expect, it } from 'vitest';
import { h } from 'vue';
import { mount } from '@vue/test-utils';
import FTable from '../table';
import FTableColumn from '../column';

describe('FTable column minWidth/maxWidth support', () => {
const defaultData = [
{ id: 1, name: 'John', age: 30 },
{ id: 2, name: 'Jane', age: 25 },
];

it('should apply minWidth as number to column', () => {
const wrapper = mount(FTable, {
props: {
data: defaultData,
},
slots: {
default: () => [
h(FTableColumn, {
prop: 'name',
label: 'Name',
minWidth: 120,
}),
h(FTableColumn, {
prop: 'age',
label: 'Age',
}),
],
},
});

const th = wrapper.findAll('th')[0];
expect(th.attributes('style')).toContain('min-width');
});

it('should apply maxWidth as number to column', () => {
const wrapper = mount(FTable, {
props: {
data: defaultData,
},
slots: {
default: () => [
h(FTableColumn, {
prop: 'name',
label: 'Name',
maxWidth: 100,
}),
h(FTableColumn, {
prop: 'age',
label: 'Age',
}),
],
},
});

const th = wrapper.findAll('th')[0];
expect(th.attributes('style')).toContain('max-width');
});

it('should apply minWidth as string to column', () => {
const wrapper = mount(FTable, {
props: {
data: defaultData,
},
slots: {
default: () => [
h(FTableColumn, {
prop: 'name',
label: 'Name',
minWidth: '150px',
}),
h(FTableColumn, {
prop: 'age',
label: 'Age',
}),
],
},
});

const th = wrapper.findAll('th')[0];
const style = th.attributes('style');
expect(style).toContain('min-width: 150px');
});

it('should apply maxWidth as string to column', () => {
const wrapper = mount(FTable, {
props: {
data: defaultData,
},
slots: {
default: () => [
h(FTableColumn, {
prop: 'name',
label: 'Name',
maxWidth: '80px',
}),
h(FTableColumn, {
prop: 'age',
label: 'Age',
}),
],
},
});

const th = wrapper.findAll('th')[0];
const style = th.attributes('style');
expect(style).toContain('max-width: 80px');
});

it('should apply both minWidth and maxWidth to column', () => {
const wrapper = mount(FTable, {
props: {
data: defaultData,
},
slots: {
default: () => [
h(FTableColumn, {
prop: 'name',
label: 'Name',
minWidth: 80,
maxWidth: 200,
}),
h(FTableColumn, {
prop: 'age',
label: 'Age',
}),
],
},
});

const th = wrapper.findAll('th')[0];
const style = th.attributes('style');
expect(style).toContain('min-width: 80px');
expect(style).toContain('max-width: 200px');
});
});
5 changes: 3 additions & 2 deletions components/table/column.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,9 @@ export const columnProps = {
type: String as PropType<(typeof ALIGN)[number]>,
default: 'left',
},
width: Number,
minWidth: Number,
width: [Number, String] as PropType<number | string>,
minWidth: [Number, String] as PropType<number | string>,
maxWidth: [Number, String] as PropType<number | string>,
colClassName: [Function, String, Array, Object] as PropType<
| string
| []
Expand Down
13 changes: 11 additions & 2 deletions components/table/useTableStyle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ export default ({
];
};

const getCellStyle = ({
const getCellStyle = ({
column,
columns,
row,
Expand Down Expand Up @@ -287,7 +287,16 @@ export default ({
);
fixedStyle.right = `${width}px`;
}
return { ...alignStyle, ...fixedStyle };
const widthStyle: CSSProperties = {};
if (column.props.minWidth !== undefined) {
const val = column.props.minWidth;
widthStyle.minWidth = typeof val === 'number' ? `${val}px` : val;
}
if (column.props.maxWidth !== undefined) {
const val = column.props.maxWidth;
widthStyle.maxWidth = typeof val === 'number' ? `${val}px` : val;
}
return { ...alignStyle, ...fixedStyle, ...widthStyle };
};

const getCustomCellStyle = ({
Expand Down
Loading