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

117 lines
2.9 KiB
TypeScript
Raw Normal View History

import * as React from 'react';
import * as _ from 'lodash';
2017-07-12 06:02:01 +00:00
import { LexEdit } from './LexEdit';
2017-06-22 11:34:06 +00:00
import {
Input,
Dropdown,
} from 'semantic-ui-react';
2017-06-27 09:33:34 +00:00
const { Flex } = require('reflexbox');
2017-07-12 06:02:01 +00:00
// container component
export class LexEditor extends React.Component<any, any> {
public render() {
2017-07-13 11:17:37 +00:00
let searchLens = this.props.fieldMetaMap[this.props.searchState.searchType].lens;
2017-07-13 10:30:36 +00:00
let searchText = this.props.searchState.searchValue;
2017-07-13 11:17:37 +00:00
let matchedEntries = _.chain(this.props.allEntries)
.filter((q: any) => _.get<any>(q, searchLens, '') === searchText)
.take(10)
.value();
2017-07-13 13:36:47 +00:00
let { fieldMetaMap: fieldMetaMap, save: save } = this.props;
return (
<div>
2017-07-12 06:02:01 +00:00
<LexSearch
2017-07-12 12:52:15 +00:00
{...this.props}
2017-07-12 06:02:01 +00:00
/>
<LexMatches
2017-07-13 13:36:47 +00:00
{...{ fieldMetaMap, save }}
matchedEntries={matchedEntries}
2017-07-13 11:17:37 +00:00
selectionMeta={this.props.selectFields}
searchText={searchText}
searchLens={searchLens}
/>
</div>
);
}
}
class LexSearch extends React.Component<any, any> {
public render() {
2017-07-12 06:02:01 +00:00
let dropOptions = _.keys(this.props.fieldMetaMap).map((k, i, c) => {
2017-06-22 11:34:06 +00:00
return { key: i, value: k, text: _.capitalize(k) };
});
return (
2017-06-22 11:34:06 +00:00
<div>
<Dropdown
options={dropOptions}
onChange={(e, d) => this.handleChange(d, true)}
2017-07-13 10:30:36 +00:00
value={this.props.searchState.searchType}
2017-06-22 11:34:06 +00:00
compact={true}
selection={true}
/>
<Input
type="text"
placeholder="Search input"
dir="auto"
2017-07-13 10:30:36 +00:00
value={this.props.searchState.searchValue}
2017-07-18 10:24:19 +00:00
icon="filter"
2017-06-22 11:34:06 +00:00
onChange={(e, d) => this.handleChange(d, false)}
/>
</div>
);
}
2017-06-22 11:34:06 +00:00
private handleChange(e: any, t: boolean) {
if (t) {
2017-07-13 10:30:36 +00:00
this.props.searchForType(e.value);
2017-06-22 11:34:06 +00:00
} else {
2017-07-13 10:30:36 +00:00
this.props.searchForValue(e.value);
2017-06-22 11:34:06 +00:00
}
}
}
2017-07-12 06:02:01 +00:00
function LexMatches(params: any) {
const selectMode = (props: any) => {
if (props.searchText === '') {
2017-06-21 12:37:48 +00:00
return (
2017-07-12 06:02:01 +00:00
<div>
<h4>Empty</h4>
Type something in the searchbar.
</div>
);
} else {
let editEntries = props.matchedEntries.map((mObj: any) => {
let uniqueKey = mObj.guid[0] + '#' + mObj.$.id;
return (
<LexEdit
2017-07-13 13:36:47 +00:00
{...props}
2017-07-12 06:02:01 +00:00
key={uniqueKey}
lexItem={mObj}
selectionMeta={props.selectionMeta}
2017-07-18 10:24:19 +00:00
existing={true}
2017-07-12 06:02:01 +00:00
/>
);
});
let addProps = {};
_.set(addProps, props.searchLens, props.searchText);
let addEntry = (
2017-06-21 12:37:48 +00:00
<LexEdit
2017-07-13 13:36:47 +00:00
{...props}
2017-07-12 06:02:01 +00:00
key={props.searchText}
lexItem={addProps}
2017-06-21 12:37:48 +00:00
selectionMeta={props.selectionMeta}
2017-07-18 10:24:19 +00:00
existing={false}
2017-06-21 12:37:48 +00:00
/>
);
2017-07-12 06:02:01 +00:00
editEntries.push(addEntry);
return editEntries;
}
};
2017-07-13 10:30:36 +00:00
// const debouncedSelect = _.throttle(selectMode,10000,{leading:true});
return (
2017-06-22 11:34:06 +00:00
<Flex wrap={true}>
2017-07-12 06:02:01 +00:00
{selectMode(params)}
</Flex>
);
}