Skip to content

Commit

Permalink
Fix: Wrong TypeScript type in jsPDF.table (#3086) (#3087)
Browse files Browse the repository at this point in the history
* also implicitly calculate cell widths if headers are passed as string array
  • Loading branch information
jabaa authored Feb 10, 2021
1 parent 3592fc2 commit c91995d
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/modules/cell.js
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,7 @@ import { jsPDF } from "../jspdf.js";
});
}

if (autoSize) {
if (autoSize || (Array.isArray(headers) && typeof headers[0] === "string")) {
var headerName;
for (i = 0; i < headerNames.length; i += 1) {
headerName = headerNames[i];
Expand Down
Binary file added test/reference/table-autoSize-headerNames.pdf
Binary file not shown.
28 changes: 25 additions & 3 deletions test/specs/cell.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,16 +71,18 @@ describe("Module: Cell", () => {
}));
}

var header = createHeaders([
var headerNames = [
"coin",
"game_group",
"game_name",
"game_version",
"machine",
"vlt"
]);
];

it("table", () => {
var header = createHeaders(headerNames);

it("table with CellConfig[]", () => {
var doc = new jsPDF({
putOnlyUsedFonts: true,
orientation: "landscape",
Expand All @@ -90,6 +92,26 @@ describe("Module: Cell", () => {
comparePdf(doc.output(), "table.pdf");
});

it("table with string[] and without autoSize", () => {
var doc = new jsPDF({
putOnlyUsedFonts: true,
orientation: "landscape",
floatPrecision: 2
});
doc.table(1, 1, generateData(100), headerNames);
comparePdf(doc.output(), "table-autoSize-headerNames.pdf");
});

it("table with string[] and autoSize", () => {
var doc = new jsPDF({
putOnlyUsedFonts: true,
orientation: "landscape",
floatPrecision: 2
});
doc.table(1, 1, generateData(100), headerNames, { autoSize: true });
comparePdf(doc.output(), "table-autoSize-headerNames.pdf");
});

it("table-autoSize", () => {
var doc = new jsPDF({
putOnlyUsedFonts: true,
Expand Down
18 changes: 15 additions & 3 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -563,6 +563,18 @@ declare module "jspdf" {
y: number;
}

export interface TableConfig {
printHeaders?: boolean;
autoSize?: boolean;
margins?: number;
fontSize?: number;
padding?: number;
headerBackgroundColor?: string;
css?: {
"font-size": number;
};
}

export interface CellConfig {
name: string;
prompt: string;
Expand Down Expand Up @@ -981,9 +993,9 @@ declare module "jspdf" {
table(
x: number,
y: number,
data: any,
headers: string[],
config: any
data: { [key: string]: string }[],
headers: string[] | CellConfig[],
config: TableConfig
): jsPDF;
calculateLineHeight(
headerNames: string[],
Expand Down

0 comments on commit c91995d

Please sign in to comment.