import React = require('react'); import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'; import { action, computed, observable } from 'mobx'; import { observer } from 'mobx-react'; import { emptyFunction, setupMoveUpEvents } from '../../../../Utils'; import './CollectionSchemaView.scss'; import { ColumnType } from './CollectionSchemaView'; import { Colors } from 'browndash-components'; export interface SchemaColumnHeaderProps { columnKeys: string[]; columnWidths: number[]; columnIndex: number; possibleKeys: string[]; sortField: string; sortDesc: boolean; setSort: (field: string, desc: boolean) => void; changeColumnKey: (index: number, newKey: string, defaultVal?: any) => void; addColumn: (index: number, key: string, defaultVal?: any) => void; removeColumn: (index: number) => void; resizeColumn: (e: any, index: number, left: boolean) => void; // dragColumn: (e: any, index: number) => boolean; } @observer export class SchemaColumnHeader extends React.Component { @observable _menuVisible: boolean = false; @observable _makeNewField: boolean = false; @observable _newFieldType: ColumnType = ColumnType.Number; @observable _newFieldDefault: any = 0; @observable _newFieldWarning: string = ''; @observable _menuValue: string = ''; @observable _menuOptions: string[] = []; @observable _filterVisible: boolean = false; private _makeNewColumn = false; @computed get fieldKey() { return this.props.columnKeys[this.props.columnIndex]; } @action sortClicked = (e: React.PointerEvent) => { e.stopPropagation(); e.preventDefault(); if (this.props.sortField == this.fieldKey) { this.props.setSort(this.fieldKey, !this.props.sortDesc); } else { this.props.setSort(this.fieldKey, false); } }; @computed get fieldDefaultInput() { switch (this._newFieldType) { case ColumnType.Number: return e.stopPropagation()} onChange={action(e => (this._newFieldDefault = e.target.value))} />; case ColumnType.Boolean: return ( <> e.stopPropagation()} onChange={action(e => (this._newFieldDefault = e.target.checked))} /> {this._newFieldDefault ? 'true' : 'false'} ); case ColumnType.String: return e.stopPropagation()} onChange={action(e => (this._newFieldDefault = e.target.value))} />; } } @computed get renderColumnMenu() { return (
e.stopPropagation()} /> {this._makeNewField ? (
{ this._newFieldType = ColumnType.Number; this._newFieldDefault = 0; })} /> number
{ this._newFieldType = ColumnType.Boolean; this._newFieldDefault = false; })} /> boolean
{ this._newFieldType = ColumnType.String; this._newFieldDefault = ''; })} /> string
value: {this.fieldDefaultInput}
{this._newFieldWarning}
{ if (this.props.possibleKeys.includes(this._menuValue)) { this._newFieldWarning = 'Field already exists'; } else if (this._menuValue.length === 0) { this._newFieldWarning = 'Field cannot be an empty string'; } else { this.setKey(this._menuValue, this._newFieldDefault); } })}> done
) : (
{ e.stopPropagation(); this._makeNewField = true; })}> + new field
{this._menuOptions.map(key => (
{ e.stopPropagation(); this.setKey(key); }}> {key}
))}
)}
); } @computed get columnFilterMenu() { return (
e.stopPropagation()} />
); } onSearchKeyDown = (e: React.KeyboardEvent) => { switch (e.key) { case 'Enter': this.setKey(this._menuOptions.length > 0 && this._menuValue.length > 0 ? this._menuOptions[0] : this._menuValue); break; case 'Escape': this.toggleColumnMenu(); break; } }; @action setKey = (key: string, defaultVal?: any) => { if (this._makeNewColumn) { this.props.addColumn(this.props.columnIndex, key, defaultVal); } else { this.props.changeColumnKey(this.props.columnIndex, key, defaultVal); } this.toggleColumnMenu(); }; @action updateKeySearch = (e: React.ChangeEvent) => { this._menuValue = e.target.value; this._menuOptions = this.props.possibleKeys.filter(value => value.toLowerCase().includes(this._menuValue.toLowerCase())); }; // @action // onPointerDown = (e: React.PointerEvent) => { // e.stopPropagation(); // setupMoveUpEvents(this, e, e => this.props.dragColumn(e, this.props.columnIndex), emptyFunction, emptyFunction); // }; @action toggleColumnMenu = (newCol?: boolean) => { this._makeNewColumn = false; if (this._menuVisible) { this._menuVisible = false; } else { this._filterVisible = false; this._menuVisible = true; this._menuValue = ''; this._menuOptions = this.props.possibleKeys; this._makeNewField = false; this._newFieldWarning = ''; this._makeNewField = false; if (newCol) { this._makeNewColumn = true; } } }; @action toggleFilterMenu = () => { console.log(this._filterVisible); if (this._filterVisible) { this._filterVisible = false; } else { this._filterVisible = true; this._menuVisible = false; } }; render() { return (
this.props.resizeColumn(e, this.props.columnIndex, true)}>
{this.fieldKey}
{ this.toggleColumnMenu(); }}>
{ this.toggleColumnMenu(true); }}>
{ this.props.removeColumn(this.props.columnIndex); }}>
this.toggleFilterMenu()}>
this.props.resizeColumn(e, this.props.columnIndex, false)}>
{this._menuVisible && this.renderColumnMenu} {this._filterVisible && this.columnFilterMenu}
); } }