Stringify line values, in case they are complex.
[ustaxviewer.git] / src / FormView.tsx
1 // Copyright 2020 Blue Static <https://www.bluestatic.org>
2 // This program is free software licensed under the GNU General Public License,
3 // version 3.0. The full text of the license can be found in LICENSE.txt.
4 // SPDX-License-Identifier: GPL-3.0-only
5
6 import { createMemo } from 'solid-js';
7 import { For } from 'solid-js/dom';
8 import { TaxReturn, Form } from 'ustaxlib/core';
9
10 const S = require('./FormView.css');
11
12 interface FormProps {
13 tr: TaxReturn;
14 form: Form<any>;
15 }
16
17 export default function FormView(props: FormProps) {
18 const lineKeys = createMemo(() => Object.keys(props.form.lines));
19
20 return (
21 <>
22 <h2 class={S.formName}>Form {props.form.name}</h2>
23
24 <table class={S.table}>
25 <For each={lineKeys()}>
26 {key => <Line tr={props.tr} line={props.form.lines[key]} />}
27 </For>
28 </table>
29 </>
30 );
31 }
32
33 function Line(props: { tr, line }) {
34 const { tr, line } = props;
35 const value = createMemo(() => {
36 try {
37 return JSON.stringify(line.value(tr));
38 } catch (e) {
39 return <span class={S.error} title={e.stack}>{e.message}</span>;
40 }
41 });
42 return (
43 <tr class={S.line}>
44 <th class={S.id}>{line.id}</th>
45 <td class={S.description}>{line.description}</td>
46 <td class={S.value}>{value()}</td>
47 </tr>
48 );
49 }