Alphabetically sort the forms in the select list.
[ustaxviewer.git] / src / TaxReturnView.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, createState } from 'solid-js';
7 import { For } from 'solid-js/dom';
8 import { Form, Person, TaxReturn } from 'ustaxlib/core';
9
10 import FormView from './FormView';
11
12 const S = require('./TaxReturnView.css');
13
14 interface Props {
15 tr: TaxReturn;
16 }
17
18 export default function TaxReturnView(props: Props) {
19 const [ state, setState ] = createState({ form: props.tr.forms[0] });
20
21 const changeForm = e => {
22 setState({ form: props.tr.forms[e.target.value] });
23 };
24
25 const formIndexToName = createMemo(() => {
26 let forms = props.tr.forms.map((form, i) => {
27 let name = form.name;
28 const person = form.person();
29 if (person !== undefined) {
30 const personName = person === Person.joint ? 'Joint' : person.name;
31 name += ` (${personName})`;
32 }
33 return [i, name];
34 });
35 forms.sort((a, b) => {
36 if (a[1] < b[1])
37 return -1;
38 if (a[1] > b[1])
39 return 1;
40 return 0;
41 });
42 return forms;
43 });
44
45 const formSelector = (
46 <select onchange={changeForm}>
47 <For each={formIndexToName()}>
48 {tuple => (<option value={tuple[0]}>{tuple[1]}</option>)}
49 </For>
50 </select>
51 );
52
53 return (
54 <div>
55 <div class={S.header}>
56 <h1>ustaxlib Federal {props.tr.year}</h1>
57 {formSelector}
58 </div>
59
60 <FormView tr={props.tr} form={state.form as Form<any>} />
61 </div>
62 );
63 }
64