"vscode:/vscode.git/clone" did not exist on "9254df13a23be7a1d70473da384ddda52d0e6737"
ChangeColumnComponent.tsx 4.05 KB
Newer Older
1
import * as React from 'react';
2
import { Dialog, DialogType, DialogFooter, Checkbox, PrimaryButton, DefaultButton } from '@fluentui/react';
3
4

interface ChangeColumnState {
5
6
    // buffer, not saved yet
    currentSelected: string[];
7
8
9
}

interface ChangeColumnProps {
10
11
12
13
14
15
16
17
18
19
    allColumns: SimpleColumn[]; // all column List
    selectedColumns: string[]; // user selected column list
    onSelectedChange: (val: string[]) => void;
    onHideDialog: () => void;
    minSelected?: number;
}

interface SimpleColumn {
    key: string; // key for management
    name: string; // name to display
20
21
22
23
24
25
26
}

interface CheckBoxItems {
    label: string;
    checked: boolean;
    onChange: () => void;
}
27

28
29
30
class ChangeColumnComponent extends React.Component<ChangeColumnProps, ChangeColumnState> {
    constructor(props: ChangeColumnProps) {
        super(props);
31
        this.state = {
32
            currentSelected: this.props.selectedColumns
33
        };
34
35
36
37
    }

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

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

    saveUserSelectColumn = (): void => {
59
60
61
        const { currentSelected } = this.state;
        const { allColumns, onSelectedChange } = this.props;
        const selectedColumns = allColumns.map(column => column.key).filter(key => currentSelected.includes(key));
62
        localStorage.setItem('columns', JSON.stringify(selectedColumns));
63
64
        onSelectedChange(selectedColumns);
        this.hideDialog();
65
    };
66
67
68
69

    // user exit dialog
    cancelOption = (): void => {
        // reset select column
70
        this.setState({ currentSelected: this.props.selectedColumns }, () => {
71
72
            this.hideDialog();
        });
73
    };
74

75
76
77
78
    private hideDialog = (): void => {
        this.props.onHideDialog();
    };

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

121
export default ChangeColumnComponent;