diff --git a/superset-frontend/src/SqlLab/components/SqlEditorLeftBar/index.jsx b/superset-frontend/src/SqlLab/components/SqlEditorLeftBar/index.tsx similarity index 62% rename from superset-frontend/src/SqlLab/components/SqlEditorLeftBar/index.jsx rename to superset-frontend/src/SqlLab/components/SqlEditorLeftBar/index.tsx index 0f0750eabbe7a..4ea89ad477ef1 100644 --- a/superset-frontend/src/SqlLab/components/SqlEditorLeftBar/index.jsx +++ b/superset-frontend/src/SqlLab/components/SqlEditorLeftBar/index.tsx @@ -17,39 +17,50 @@ * under the License. */ import React from 'react'; -import PropTypes from 'prop-types'; import Button from 'src/components/Button'; -import { t, styled, css } from '@superset-ui/core'; +import { t, styled, css, SupersetTheme } from '@superset-ui/core'; import Collapse from 'src/components/Collapse'; import Icons from 'src/components/Icons'; import TableSelector from 'src/components/TableSelector'; import { IconTooltip } from 'src/components/IconTooltip'; -import TableElement from '../TableElement'; +import { QueryEditor } from 'src/SqlLab/types'; +import { DatabaseObject } from 'src/components/DatabaseSelector'; +import TableElement, { Table, TableElementProps } from '../TableElement'; -const propTypes = { - queryEditor: PropTypes.object.isRequired, - height: PropTypes.number, - tables: PropTypes.array, - actions: PropTypes.object, - database: PropTypes.object, - offline: PropTypes.bool, -}; +interface ExtendedTable extends Table { + expanded: boolean; +} -const defaultProps = { - actions: {}, - height: 500, - offline: false, - tables: [], -}; +interface actionsTypes { + queryEditorSetDb: (queryEditor: QueryEditor, dbId: number) => void; + queryEditorSetFunctionNames: (queryEditor: QueryEditor, dbId: number) => void; + collapseTable: (table: Table) => void; + expandTable: (table: Table) => void; + addTable: (queryEditor: any, value: any, schema: any) => void; + setDatabases: (arg0: any) => {}; + addDangerToast: (msg: string) => void; + queryEditorSetSchema: (schema?: string) => void; + queryEditorSetSchemaOptions: () => void; + queryEditorSetTableOptions: (options: Array) => void; + resetState: () => void; +} + +interface SqlEditorLeftBarProps { + queryEditor: QueryEditor; + height?: number; + tables?: ExtendedTable[]; + actions: actionsTypes & TableElementProps['actions']; + database: DatabaseObject; +} const StyledScrollbarContainer = styled.div` flex: 1 1 auto; overflow: auto; `; -const collapseStyles = css` +const collapseStyles = (theme: SupersetTheme) => css` .ant-collapse-item { - margin-bottom: ${({ theme }) => theme.gridUnit * 3}px; + margin-bottom: ${theme.gridUnit * 3}px; } .ant-collapse-header { padding: 0px !important; @@ -57,13 +68,13 @@ const collapseStyles = css` align-items: center; } .ant-collapse-content-box { - padding: 0px ${({ theme }) => theme.gridUnit * 4}px 0px 0px !important; + padding: 0px ${theme.gridUnit * 4}px 0px 0px !important; } .ant-collapse-arrow { - top: ${({ theme }) => theme.gridUnit * 2}px !important; - color: ${({ theme }) => theme.colors.primary.dark1} !important; + top: ${theme.gridUnit * 2}px !important; + color: ${theme.colors.primary.dark1} !important; &: hover { - color: ${({ theme }) => theme.colors.primary.dark2} !important; + color: ${theme.colors.primary.dark2} !important; } } `; @@ -71,32 +82,35 @@ const collapseStyles = css` export default function SqlEditorLeftBar({ actions, database, - height, queryEditor, - tables: tb, -}) { - const onDbChange = db => { - actions.queryEditorSetDb(queryEditor, db.id); - actions.queryEditorSetFunctionNames(queryEditor, db.id); + tables = [], + height = 500, +}: SqlEditorLeftBarProps) { + const onDbChange = ({ id: dbId }: { id: number }) => { + actions.queryEditorSetDb(queryEditor, dbId); + actions.queryEditorSetFunctionNames(queryEditor, dbId); }; - const onTableChange = (tableName, schemaName) => { + const onTableChange = (tableName: string, schemaName: string) => { if (tableName && schemaName) { actions.addTable(queryEditor, tableName, schemaName); } }; - const onToggleTable = tables => { - tb.forEach(table => { - if (!tables.includes(table.id.toString()) && table.expanded) { + const onToggleTable = (updatedTables: string[]) => { + tables.forEach((table: ExtendedTable) => { + if (!updatedTables.includes(table.id.toString()) && table.expanded) { actions.collapseTable(table); - } else if (tables.includes(table.id.toString()) && !table.expanded) { + } else if ( + updatedTables.includes(table.id.toString()) && + !table.expanded + ) { actions.expandTable(table); } }); }; - const renderExpandIconWithTooltip = ({ isActive }) => ( + const renderExpandIconWithTooltip = ({ isActive }: { isActive: boolean }) => (
props.contentHeight}px; + height: ${tableMetaDataHeight}px; `} - contentHeight={tableMetaDataHeight} > expanded) .map(({ id }) => id)} css={collapseStyles} @@ -151,7 +163,7 @@ export default function SqlEditorLeftBar({ onChange={onToggleTable} expandIcon={renderExpandIconWithTooltip} > - {tb.map(table => ( + {tables.map(table => ( ))} @@ -169,6 +181,3 @@ export default function SqlEditorLeftBar({
); } - -SqlEditorLeftBar.propTypes = propTypes; -SqlEditorLeftBar.defaultProps = defaultProps; diff --git a/superset-frontend/src/SqlLab/components/TableElement/index.tsx b/superset-frontend/src/SqlLab/components/TableElement/index.tsx index 0d1624d96211d..2e285a1c72294 100644 --- a/superset-frontend/src/SqlLab/components/TableElement/index.tsx +++ b/superset-frontend/src/SqlLab/components/TableElement/index.tsx @@ -37,7 +37,7 @@ interface Column { type: string; } -interface Table { +export interface Table { id: string; name: string; partitions?: { @@ -53,7 +53,7 @@ interface Table { columns: Column[]; } -interface TableElementProps { +export interface TableElementProps { table: Table; actions: { removeDataPreview: (table: Table) => void;