Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[sqllab] slide animations when adding/removing/toggling TableElement #1472

Merged
merged 2 commits into from
Oct 29, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,7 @@ import CopyToClipboard from '../../components/CopyToClipboard';
import { getShortUrl } from '../../../utils/common';

const propTypes = {
queryEditor: React.PropTypes.object,
};

const defaultProps = {
queryEditor: null,
queryEditor: React.PropTypes.object.isRequired,
};

export default class CopyQueryTabUrl extends React.PureComponent {
Expand All @@ -19,8 +15,8 @@ export default class CopyQueryTabUrl extends React.PureComponent {
}

componentWillMount() {
const params = [];
const qe = this.props.queryEditor;
const params = [];
if (qe.dbId) params.push('dbid=' + qe.dbId);
if (qe.title) params.push('title=' + encodeURIComponent(qe.title));
if (qe.schema) params.push('schema=' + encodeURIComponent(qe.schema));
Expand Down Expand Up @@ -52,4 +48,3 @@ export default class CopyQueryTabUrl extends React.PureComponent {
}

CopyQueryTabUrl.propTypes = propTypes;
CopyQueryTabUrl.defaultProps = defaultProps;
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,11 @@ class TabbedSqlEditors extends React.PureComponent {
<MenuItem eventKey="2" onClick={this.renameTab.bind(this, qe)}>
<i className="fa fa-i-cursor" /> rename tab
</MenuItem>
<MenuItem eventKey="3">
<i className="fa fa-clipboard" /> <CopyQueryTabUrl queryEditor={qe} />
</MenuItem>
{qe &&
<MenuItem eventKey="3">
<i className="fa fa-clipboard" /> <CopyQueryTabUrl queryEditor={qe} />
</MenuItem>
}
</DropdownButton>
</div>
);
Expand Down
181 changes: 99 additions & 82 deletions caravel/assets/javascripts/SqlLab/components/TableElement.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from 'react';

import { ButtonGroup, Well } from 'react-bootstrap';
import { ButtonGroup, Collapse, Well } from 'react-bootstrap';
import shortid from 'shortid';

import CopyToClipboard from '../../components/CopyToClipboard';
Expand All @@ -10,18 +10,21 @@ import ModalTrigger from '../../components/ModalTrigger';
const propTypes = {
table: React.PropTypes.object,
actions: React.PropTypes.object,
timeout: React.PropTypes.integer, // used for tests
};

const defaultProps = {
table: null,
actions: {},
table: null,
timeout: 500,
};

class TableElement extends React.PureComponent {
constructor(props) {
super(props);
this.state = {
sortColumns: false,
expanded: true,
};
}

Expand All @@ -36,18 +39,17 @@ class TableElement extends React.PureComponent {
this.props.actions.addQueryEditor(qe);
}

collapseTable(e) {
e.preventDefault();
this.props.actions.collapseTable(this.props.table);
}

expandTable(e) {
toggleTable(e) {
e.preventDefault();
this.props.actions.expandTable(this.props.table);
if (this.props.table.expanded) {
this.props.actions.collapseTable(this.props.table);
} else {
this.props.actions.expandTable(this.props.table);
}
}

removeTable() {
this.props.actions.removeTable(this.props.table);
this.setState({ expanded: false });
}
dataPreviewModal() {
const query = {
Expand All @@ -65,11 +67,8 @@ class TableElement extends React.PureComponent {
this.setState({ sortColumns: !this.state.sortColumns });
}

render() {
renderHeader() {
const table = this.props.table;
let metadata = null;
let buttonToggle;

let header;
if (table.partitions) {
let partitionQuery;
Expand Down Expand Up @@ -101,25 +100,26 @@ class TableElement extends React.PureComponent {
</Well>
);
}
if (table.expanded) {
buttonToggle = (
<a
href="#"
onClick={(e) => { this.collapseTable(e); }}
>
<strong>{table.name}</strong>
<small className="m-l-5"><i className="fa fa-minus" /></small>
</a>
);
const cols = table.columns.slice();
return header;
}
renderMetadata() {
const table = this.props.table;
let cols;
if (table.columns) {
cols = table.columns.slice();
if (this.state.sortColumns) {
cols.sort((a, b) => a.name.toUpperCase() > b.name.toUpperCase());
}
metadata = (
}
const metadata = (
<Collapse
in={table.expanded}
timeout={this.props.timeout}
>
<div>
{header}
{this.renderHeader()}
<div className="table-columns">
{cols.map((col) => {
{cols && cols.map((col) => {
let name = col.name;
if (col.indexed) {
name = <strong>{col.name}</strong>;
Expand All @@ -137,18 +137,17 @@ class TableElement extends React.PureComponent {
<hr />
</div>
</div>
);
} else {
buttonToggle = (
<a
href="#"
onClick={(e) => { this.expandTable(e); }}
>
{table.name}
<small className="m-l-5"><i className="fa fa-plus" /></small>
</a>
);
}
</Collapse>
);
return metadata;
}
removeFromStore() {
this.props.actions.removeTable(this.props.table);
}

render() {
const table = this.props.table;

let keyLink;
if (table.indexes && table.indexes.length > 0) {
keyLink = (
Expand All @@ -171,52 +170,70 @@ class TableElement extends React.PureComponent {
);
}
return (
<div className="TableElement">
<div className="clearfix">
<div className="pull-left">
{buttonToggle}
</div>
<div className="pull-right">
<ButtonGroup className="ws-el-controls pull-right">
{keyLink}
<Link
className={
`fa fa-sort-${!this.state.sortColumns ? 'alpha' : 'numeric'}-asc ` +
'pull-left sort-cols m-l-2'}
onClick={this.toggleSortColumns.bind(this)}
tooltip={
!this.state.sortColumns ?
'Sort columns alphabetically' :
'Original table column order'}
<Collapse
in={this.state.expanded}
timeout={this.props.timeout}
transitionAppear
onExited={this.removeFromStore.bind(this)}
>
<div className="TableElement">
<div className="clearfix">
<div className="pull-left">
<a
href="#"
/>
<Link
className="fa fa-search-plus pull-left m-l-2"
onClick={this.dataPreviewModal.bind(this)}
tooltip="Data preview"
href="#"
/>
<CopyToClipboard
copyNode={
<a className="fa fa-clipboard pull-left m-l-2" />
className="table-name"
onClick={(e) => { this.toggleTable(e); }}
>
<strong>{table.name}</strong>
<small className="m-l-5">
<i className={`fa fa-${table.expanded ? 'minus' : 'plus'}-square-o`} />
</small>
</a>
</div>
<div className="pull-right">
<ButtonGroup className="ws-el-controls pull-right">
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would be nice to pull this out into a component called <TableControls />

{keyLink}
<Link
className={
`fa fa-sort-${!this.state.sortColumns ? 'alpha' : 'numeric'}-asc ` +
'pull-left sort-cols m-l-2'}
onClick={this.toggleSortColumns.bind(this)}
tooltip={
!this.state.sortColumns ?
'Sort columns alphabetically' :
'Original table column order'}
href="#"
/>
<Link
className="fa fa-search-plus pull-left m-l-2"
onClick={this.dataPreviewModal.bind(this)}
tooltip="Data preview"
href="#"
/>
{table.selectStar &&
<CopyToClipboard
copyNode={
<a className="fa fa-clipboard pull-left m-l-2" />
}
text={table.selectStar}
shouldShowText={false}
tooltipText="Copy SELECT statement to clipboard"
/>
}
text={table.selectStar}
shouldShowText={false}
tooltipText="Copy SELECT statement to clipboard"
/>
<Link
className="fa fa-trash pull-left m-l-2"
onClick={this.removeTable.bind(this)}
tooltip="Remove from panel"
href="#"
/>
</ButtonGroup>
<Link
className="fa fa-trash table-remove pull-left m-l-2"
onClick={this.removeTable.bind(this)}
tooltip="Remove from panel"
href="#"
/>
</ButtonGroup>
</div>
</div>
<div>
{this.renderMetadata()}
</div>
</div>
<div>
{metadata}
</div>
</div>
</Collapse>
);
}
}
Expand Down
5 changes: 3 additions & 2 deletions caravel/assets/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,11 @@
"devDependencies": {
"babel": "^6.3.26",
"babel-cli": "^6.14.0",
"babel-polyfill": "^6.14.0",
"babel-preset-es2015": "^6.14.0",
"babel-core": "^6.10.4",
"babel-loader": "^6.2.4",
"babel-polyfill": "^6.14.0",
"babel-preset-airbnb": "^2.1.1",
"babel-preset-es2015": "^6.14.0",
"babel-preset-react": "^6.11.1",
"chai": "^3.5.0",
"codeclimate-test-reporter": "^0.3.3",
Expand All @@ -115,6 +115,7 @@
"less-loader": "^2.2.2",
"mocha": "^2.4.5",
"react-addons-test-utils": "^15.3.2",
"sinon": "^1.17.6",
"style-loader": "^0.13.0",
"transform-loader": "^0.2.3",
"url-loader": "^0.5.7",
Expand Down
11 changes: 1 addition & 10 deletions caravel/assets/spec/javascripts/sqllab/CopyQueryTabUrl_spec.jsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import React from 'react';
import CopyQueryTabUrl from '../../../javascripts/SqlLab/components/CopyQueryTabUrl';
import CopyToClipboard from '../../../javascripts/components/CopyToClipboard';
import { shallow } from 'enzyme';
import { describe, it } from 'mocha';
import { expect } from 'chai';
import { initialState } from './fixtures';
Expand All @@ -10,16 +8,9 @@ describe('CopyQueryTabUrl', () => {
const mockedProps = {
queryEditor: initialState.queryEditors[0],
};
it('should be valid', () => {
expect(React.isValidElement(<CopyQueryTabUrl />)).to.equal(true);
});
it('renders with props', () => {
it('is valid with props', () => {
expect(
React.isValidElement(<CopyQueryTabUrl {...mockedProps} />)
).to.equal(true);
});
it('renders a CopyToClipboard', () => {
const wrapper = shallow(<CopyQueryTabUrl {...mockedProps} />);
expect(wrapper.find(CopyToClipboard)).to.have.length(1);
});
});
19 changes: 18 additions & 1 deletion caravel/assets/spec/javascripts/sqllab/TableElement_spec.jsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import React from 'react';
import Link from '../../../javascripts/SqlLab/components/Link';
import TableElement from '../../../javascripts/SqlLab/components/TableElement';
import { table } from './fixtures';
import { mockedActions, table } from './fixtures';
import { mount, shallow } from 'enzyme';
import { describe, it } from 'mocha';
import { expect } from 'chai';


describe('TableElement', () => {
const mockedProps = {
actions: mockedActions,
table,
timeout: 0,
};
it('renders', () => {
expect(
Expand Down Expand Up @@ -40,4 +42,19 @@ describe('TableElement', () => {
expect(wrapper.state().sortColumns).to.equal(true);
expect(wrapper.find('.col-name').first().text()).to.equal('last_login');
});
it('calls the collapseTable action', () => {
const wrapper = mount(<TableElement {...mockedProps} />);
expect(mockedActions.collapseTable.called).to.equal(false);
wrapper.find('.table-name').simulate('click');
expect(mockedActions.collapseTable.called).to.equal(true);
});
it('removes the table', () => {
const wrapper = mount(<TableElement {...mockedProps} />);
expect(wrapper.state().expanded).to.equal(true);
wrapper.find('.table-remove').simulate('click');
expect(wrapper.state().expanded).to.equal(false);
setTimeout(() => {
expect(mockedActions.removeTable.called).to.equal(true);
}, 10);
});
});
Loading