forked from getredash/redash
-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathEditableTable.jsx
More file actions
92 lines (80 loc) · 2.41 KB
/
EditableTable.jsx
File metadata and controls
92 lines (80 loc) · 2.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import React from "react";
import Form from "antd/lib/form";
import Input from "antd/lib/input";
import PropTypes from "prop-types";
import { TableMetadata } from "@/components/proptypes";
import TableVisibilityCheckbox from "./TableVisibilityCheckbox";
import SampleQueryList from "./SampleQueryList";
import "./schema-table.css";
const { TextArea } = Input;
export const EditableContext = React.createContext();
// eslint-disable-next-line react/prop-types
export const EditableRow = ({ form, index, ...props }) => (
<EditableContext.Provider value={form}>
<tr {...props} />
</EditableContext.Provider>
);
export class EditableCell extends React.Component {
static propTypes = {
dataIndex: PropTypes.string,
input_type: PropTypes.string,
editing: PropTypes.bool,
record: TableMetadata,
};
static defaultProps = {
dataIndex: undefined,
input_type: undefined,
editing: false,
record: {},
};
constructor(props) {
super(props);
this.state = {
visible: this.props.record ? this.props.record.visible : false,
description: this.props.record ? this.props.record.description : "test"
};
}
onChange = () => {
this.setState(prevState => ({ visible: !prevState.visible }));
};
onChangeDesc = (e) => {
let newThing = e.target ? e.target.value : "";
this.setState(prevState => ({ description: newThing }));
};
getInput = () => {
if (this.props.input_type === "visible") {
return <TableVisibilityCheckbox visible={this.state.visible} onChange={this.onChange} />;
} else if (this.props.input_type === "sample_queries") {
return <SampleQueryList />;
}
return <TextArea
className="table-textarea"
style={{ resize: "vertical" }}
rows={3}
value={this.state.description}
onChange={this.onChangeDesc}
/>;
};
render() {
const { editing, dataIndex, record, ...restProps } = this.props;
return (
<EditableContext.Consumer>
{form => {
return (
<td {...restProps}>
{editing ? (
<Form>
<Form.Item style={{ margin: 0 }} name={dataIndex} initialValue={record[dataIndex]}>
{this.getInput()}
</Form.Item>
</Form>
) : (
restProps.children
)}
</td>
);
}}
</EditableContext.Consumer>
);
}
}