Skip to content

Commit

Permalink
Initial commit for logTable method
Browse files Browse the repository at this point in the history
  • Loading branch information
snowak-cb committed Jun 12, 2017
1 parent 3abd15c commit 724dbd1
Show file tree
Hide file tree
Showing 13 changed files with 134 additions and 32 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ Properties you can pass to the console element
| ---- | ---- | ----
| log | (...messages: any)=>void | Log messages to the console. If string, print the value, otherwise, print the JSON value of the message.
| logX | (type: string, ...messages: any)=>void | Log messages of a particular type to the console. The messages will be given the class `react-console-message-{type}`.
| logTable | (tableObject: Object)=>void | Log tabular data to the console. `tableObject` has the format `{ headers: ['header 1', 'header 2'], rows: [['row 1, col 1', 'row 1, col 2'], ['row 2, col 1', 'row 2, col 2']]}`. `headers` is optional.
| return | ()=>void | Signal the current command has finished and a new prompt should be displayed.
| clearScreen | ()=>void | Clear the visible log in the console. Does not clear command history.

Expand Down
Binary file modified dist/dist-min.tar.gz
Binary file not shown.
Binary file modified dist/dist-min.zip
Binary file not shown.
2 changes: 1 addition & 1 deletion dist/dist-min/react-console.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/dist-min/react-console.min.js.map

Large diffs are not rendered by default.

Binary file modified dist/dist.tar.gz
Binary file not shown.
Binary file modified dist/dist.zip
Binary file not shown.
44 changes: 35 additions & 9 deletions dist/dist/react-console.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/dist/react-console.js.map

Large diffs are not rendered by default.

44 changes: 35 additions & 9 deletions lib/react-console.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/react-console.js.map

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ compiler.watch({},function(err,stats) {
});

// Karma test server
/*
let karmaServer = new karma.Server({
configFile: `${__dirname}/karma.conf.js`,
singleRun: false,
Expand All @@ -66,3 +67,4 @@ karmaServer.on('run_complete', function() {
logEnd();
});
karmaServer.start();
*/
67 changes: 57 additions & 10 deletions src/react-console.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -73,28 +73,68 @@ class ConsolePrompt extends React.Component<ConsolePromptProps,{}> {
}
}

interface ConsoleTableHeaderProps {
headers?: string[];
}
let ConsoleTableHeader: React.SFC<ConsoleTableHeaderProps> = function(props: ConsoleTableHeaderProps){
if(props.headers){
return <thead>
<tr>
{props.headers.map((header: string) => {
return <th scope="col" key={ header }>{ header }</th>;
})}
</tr>
</thead>
}
return null;
}

interface ConsoleMessageProps {
type?: string;
isTable?: boolean;
value: any[];
}
let ConsoleMessage: React.SFC<ConsoleMessageProps> = function(props: ConsoleMessageProps) {
return <div className={"react-console-message" + (props.type?" react-console-message-"+props.type:"")}>
{props.value.map((val: any)=>{
if(typeof val == 'string') {
return val;
} else {
return JSON.stringify(val);
}
}).join("\n")}
</div>;
if(props.isTable){
const data = props.value[0];
return <div className="react-console-message react-console-table">
<table>
<ConsoleTableHeader headers={data.headers} />
<tbody>
{data.rows && data.rows.map((row: string[], index: number) => {
return <tr key={index}>{row.map((cell: string, cellIndex: number) => {
return <td key={cellIndex}>{cell}</td>; })
}</tr>;
})}
</tbody>
</table>
</div>;
}else{
return <div className={"react-console-message" + (props.type?" react-console-message-"+props.type:"")}>
{props.value.map((val: any)=>{
if(typeof val == 'string') {
return val;
} else {
return JSON.stringify(val);
}
}).join("\n")}
</div>;
}
}
ConsoleMessage.defaultProps = {
type: null,
value: [],
isTable: false
}

export interface ConsoleTableObject {
rows: Array<string[]>;
headers?: Array<string>;
}

export interface LogMessage {
type?: string;
isTable?: boolean;
value: any[];
}
export interface LogEntry {
Expand Down Expand Up @@ -189,6 +229,13 @@ export default class extends React.Component<ConsoleProps,ConsoleState> {
log: log,
}, this.scrollIfBottom() );
}
logTable = (tableData: ConsoleTableObject) => {
let log = this.state.log;
log[this.state.log.length-1].message.push({isTable: true, value: [tableData]});
this.setState({
log: log,
}, this.scrollIfBottom() );
}
return = () => {
this.setState({
acceptInput: true,
Expand Down Expand Up @@ -968,7 +1015,7 @@ export default class extends React.Component<ConsoleProps,ConsoleState> {
return [
<ConsolePrompt label={val.label} value={val.command} />,
...val.message.map( (val: LogMessage, idx: number) => {
return <ConsoleMessage key={idx} type={val.type} value={val.value} />;
return <ConsoleMessage key={idx} type={val.type} value={val.value} isTable={val.isTable} />;
})
];
})}
Expand Down

0 comments on commit 724dbd1

Please sign in to comment.