ChangeColumnComponent.tsx 4.45 KB
Newer Older
1
import * as React from 'react';
2
import { Dialog, DialogType, DialogFooter, Checkbox, PrimaryButton, DefaultButton } from '@fluentui/react';
3

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

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

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

interface SimpleColumn {
    key: string; // key for management
    name: string; // name to display
26
27
}

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

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

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

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

    saveUserSelectColumn = (): void => {
65
        const { currentSelected } = this.state;
66
        const { allColumns, onSelectedChange, whichComponent } = this.props;
67
68
        const selectedColumns = allColumns.map(column => column.key).filter(key => currentSelected.includes(key));
        onSelectedChange(selectedColumns);
69
70
71
72
73
        if (whichComponent === 'table') {
            localStorage.setItem('columns', JSON.stringify(selectedColumns));
        } else {
            localStorage.setItem('paraColumns', JSON.stringify(selectedColumns));
        }
74
        this.hideDialog();
75
    };
76
77
78
79

    // user exit dialog
    cancelOption = (): void => {
        // reset select column
80
        this.setState({ currentSelected: this.props.selectedColumns }, () => {
81
82
            this.hideDialog();
        });
83
    };
84

85
86
87
88
    private hideDialog = (): void => {
        this.props.onHideDialog();
    };

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

131
export default ChangeColumnComponent;