fs-walle-react-ts/src/App.tsx

132 lines
3.1 KiB
TypeScript
Raw Normal View History

2017-06-12 08:46:08 +00:00
import * as React from 'react';
import {
// Button,
// Classes,
// InputGroup,
// Intent,
// Menu,
// MenuItem,
// Popover,
// Position,
// Spinner,
// Switch,
// Tag,
// Tooltip,
FocusStyleManager,
} from '@blueprintjs/core';
2017-06-14 12:40:42 +00:00
import * as XML from 'xml2js';
import * as XPath from 'xml2js-xpath';
2017-06-14 12:40:42 +00:00
// import { Table } from '@blueprintjs/table';
2017-06-12 08:46:08 +00:00
2017-06-14 08:33:46 +00:00
FocusStyleManager.onlyShowFocusOnTabs();
// const logo = require('./logo.svg');
2017-06-12 08:46:08 +00:00
class App extends React.Component<{}, null> {
render() {
return (
2017-06-14 12:40:42 +00:00
<div>
<nav className="pt-navbar pt-dark">
<div className="pt-navbar-group pt-align-left">
<button className="pt-button pt-minimal pt-icon-large pt-icon-edit" />
<div className="pt-navbar-heading">Freespeech Lexicon Editor</div>
2017-06-14 12:40:42 +00:00
</div>
</nav>
<LexEditor fileName="/new_es.xml" />
2017-06-14 12:40:42 +00:00
</div>
2017-06-12 08:46:08 +00:00
);
}
}
type ReactEvent = React.ChangeEvent<HTMLInputElement>;
2017-06-14 08:33:46 +00:00
interface LexEditorProps {
fileName: RequestInfo;
}
2017-06-14 12:40:42 +00:00
class LexEditor extends React.Component<LexEditorProps, any> {
constructor(props: LexEditorProps) {
super(props);
this.handleOnSearch = this.handleOnSearch.bind(this);
this.state = { lexData: {}, matchedEntries: [] };
2017-06-14 12:40:42 +00:00
}
public componentDidMount() {
2017-06-14 12:40:42 +00:00
return fetch(this.props.fileName)
.then((response) => response.text())
.then((xmlString) => {
XML.parseString(xmlString, (err, lexData) => {
this.setState({ ...this.state, lexData });
});
2017-06-14 12:40:42 +00:00
});
// .catch((error) => {
// console.error(error);
// });
}
public render() {
return (
<div>
<LexSearch handleOnSearch={this.handleOnSearch} />
<LexMatches matchedEntries={this.state.matchedEntries} />
</div>
);
}
private handleOnSearch(event: ReactEvent): void {
let searchText = event.target.value;
// this.setState({ ...this.state, searchText });
let pathQ = './/lang';
let words = XPath.find(this.state.lexData.document, pathQ);
let matchedEntries = words.filter((obj) => {
if (obj.label[0] === searchText) {
return true;
}
return false;
2017-06-14 12:40:42 +00:00
});
this.setState({ ...this.state, matchedEntries });
2017-06-14 12:40:42 +00:00
}
}
interface LexSearchProps {
handleOnSearch: (e: ReactEvent) => void;
}
class LexSearch extends React.Component<LexSearchProps, null> {
public render() {
2017-06-14 12:40:42 +00:00
return (
<div className="pt-input-group" style={{ width: '300px' }}>
<span className="pt-icon pt-icon-search" />
2017-06-14 12:40:42 +00:00
<input
className="pt-input"
type="search"
placeholder="Search input"
dir="auto"
onChange={e => this.props.handleOnSearch(e)}
2017-06-14 12:40:42 +00:00
/>
</div>
);
}
}
class LexMatches extends React.Component<any, any> {
public render() {
return (
<div>
{this.props.matchedEntries.map((mObj: any) => {
console.log('Matched ObjectEntry' + mObj);
return (<LexEntry key={mObj.unl} lexItem={mObj}/>);
})}
</div>
);
}
}
class LexEntry extends React.Component<any, any> {
public render() {
return null;
}
}
2017-06-12 08:46:08 +00:00
export default App;