ChangeColumnComponent.tsx 4.83 KB
Newer Older
1
import * as React from 'react';
2
import { Dialog, DialogType, DialogFooter, Checkbox, PrimaryButton, DefaultButton } from '@fluentui/react';
3
4
import { EXPERIMENT } from '@static/datamodel';
import { Storage } from '@model/localStorage';
5

Lijiaoa's avatar
Lijiaoa committed
6
7
8
9
10
/**
 * changeColumnComponent file is for [customized table column, customized hyper-parameter graph yAxis]
 * and currently it uses localstorage to store the customized results
 */

11
interface ChangeColumnState {
12
13
    // buffer, not saved yet
    currentSelected: string[];
14
15
16
}

interface ChangeColumnProps {
17
18
19
20
21
    allColumns: SimpleColumn[]; // all column List
    selectedColumns: string[]; // user selected column list
    onSelectedChange: (val: string[]) => void;
    onHideDialog: () => void;
    minSelected?: number;
22
    whichComponent: string; // which component use this component
23
24
25
26
27
}

interface SimpleColumn {
    key: string; // key for management
    name: string; // name to display
28
29
}

30
31
32
33
34
//interface CheckBoxItems {
//    label: string;
//    checked: boolean;
//    onChange: () => void;
//}
35

36
37
38
class ChangeColumnComponent extends React.Component<ChangeColumnProps, ChangeColumnState> {
    constructor(props: ChangeColumnProps) {
        super(props);
39
        this.state = {
40
            currentSelected: this.props.selectedColumns
41
        };
42
43
44
45
    }

    makeChangeHandler = (label: string): any => {
        return (ev: any, checked: boolean): void => this.onCheckboxChange(ev, label, checked);
46
    };
47

48
49
50
51
52
    onCheckboxChange = (
        ev: React.FormEvent<HTMLElement | HTMLInputElement> | undefined,
        label: string,
        val?: boolean
    ): void => {
53
        const source: string[] = [...this.state.currentSelected];
54
55
56
        if (val === true) {
            if (!source.includes(label)) {
                source.push(label);
57
                this.setState({ currentSelected: source });
58
59
            }
        } else {
60
61
62
            // remove from source
            const result = source.filter(item => item !== label);
            this.setState({ currentSelected: result });
63
64
65
66
        }
    };

    saveUserSelectColumn = (): void => {
67
        const { currentSelected } = this.state;
68
        const { allColumns, onSelectedChange, whichComponent } = this.props;
69
70
        const selectedColumns = allColumns.map(column => column.key).filter(key => currentSelected.includes(key));
        onSelectedChange(selectedColumns);
71
        if (whichComponent === 'table') {
72
73
74
75
76
77
            const storage = new Storage(
                `${EXPERIMENT.profile.id}_columns`,
                JSON.stringify(selectedColumns),
                30 * 24 * 60 * 60 * 1000
            );
            storage.setValue();
78
        } else {
79
80
81
82
83
84
            const storage = new Storage(
                `${EXPERIMENT.profile.id}_paraColumns`,
                JSON.stringify(selectedColumns),
                30 * 24 * 60 * 60 * 1000
            );
            storage.setValue();
85
        }
86
        this.hideDialog();
87
    };
88
89
90
91

    // user exit dialog
    cancelOption = (): void => {
        // reset select column
92
        this.setState({ currentSelected: this.props.selectedColumns }, () => {
93
94
            this.hideDialog();
        });
95
    };
96

97
98
99
100
    private hideDialog = (): void => {
        this.props.onHideDialog();
    };

101
    render(): React.ReactNode {
102
103
        const { allColumns, minSelected } = this.props;
        const { currentSelected } = this.state;
104
105
106
        return (
            <div>
                <Dialog
107
                    hidden={false}
108
109
                    dialogContentProps={{
                        type: DialogType.largeHeader,
110
111
                        title: 'Customize columns',
                        subText: 'You can choose which columns you wish to see.'
112
113
114
115
116
117
                    }}
                    modalProps={{
                        isBlocking: false,
                        styles: { main: { maxWidth: 450 } }
                    }}
                >
118
                    <div className='columns-height'>
119
120
121
122
123
124
125
126
127
                        {allColumns.map(item => (
                            <Checkbox
                                key={item.key}
                                label={item.name}
                                checked={currentSelected.includes(item.key)}
                                onChange={this.makeChangeHandler(item.key)}
                                styles={{ root: { marginBottom: 8 } }}
                            />
                        ))}
128
129
                    </div>
                    <DialogFooter>
130
131
132
                        <PrimaryButton
                            text='Save'
                            onClick={this.saveUserSelectColumn}
Lijiaoa's avatar
Lijiaoa committed
133
                            disabled={currentSelected.length < (minSelected ?? 1)}
134
                        />
135
                        <DefaultButton text='Cancel' onClick={this.cancelOption} />
136
137
138
139
140
141
142
                    </DialogFooter>
                </Dialog>
            </div>
        );
    }
}

143
export default ChangeColumnComponent;