Skip to content

Commit

Permalink
Added text[] search
Browse files Browse the repository at this point in the history
  • Loading branch information
grassick committed Jun 11, 2021
1 parent 09ec179 commit f1a6f72
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 39 deletions.
2 changes: 1 addition & 1 deletion lib/widgets/blocks/search/SearchBlockInstance.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as React from "react";
import React from "react";
import { SearchBlockDef } from "./search";
import { InstanceCtx } from "../../../contexts";
/** Search block that filters the rowset */
Expand Down
53 changes: 24 additions & 29 deletions lib/widgets/blocks/search/SearchBlockInstance.js
Original file line number Diff line number Diff line change
@@ -1,37 +1,21 @@
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.SearchControl = void 0;
const React = __importStar(require("react"));
const react_1 = __importDefault(require("react"));
const blocks_1 = require("../../blocks");
const mwater_expressions_1 = require("mwater-expressions");
const localization_1 = require("../../localization");
const react_1 = require("react");
const react_2 = require("react");
/** Search block that filters the rowset */
const SearchBlockInstance = (props) => {
const { blockDef, instanceCtx } = props;
const [searchText, setSearchText] = react_1.useState("");
const searchControlRef = react_1.useRef(null);
const [searchText, setSearchText] = react_2.useState("");
const searchControlRef = react_2.useRef(null);
// Focus if enabled
react_1.useEffect(() => {
react_2.useEffect(() => {
if (blockDef.autoFocus && searchControlRef.current) {
searchControlRef.current.focus();
}
Expand All @@ -52,6 +36,17 @@ const SearchBlockInstance = (props) => {
]
};
}
if (exprType === "text[]") {
return {
type: "op",
op: "~*",
table: table,
exprs: [
{ type: "op", op: "to_text", table: table, exprs: [expr] },
{ type: "literal", valueType: "text", value: escapeRegex(searchText) }
]
};
}
if (exprType === "enum") {
// Find matching enums
const enumValues = exprUtils.getExprEnumValues(expr).filter(ev => localization_1.localize(ev.name, instanceCtx.locale).toLowerCase().includes(searchText.toLowerCase()));
Expand Down Expand Up @@ -108,14 +103,14 @@ const SearchBlockInstance = (props) => {
// Set filter
instanceCtx.setFilter(blockDef.rowsetContextVarId, createFilter(value));
};
return React.createElement(SearchControl, { value: searchText, onChange: handleChange, ref: searchControlRef, placeholder: localization_1.localize(blockDef.placeholder, instanceCtx.locale) });
return react_1.default.createElement(SearchControl, { value: searchText, onChange: handleChange, ref: searchControlRef, placeholder: localization_1.localize(blockDef.placeholder, instanceCtx.locale) });
};
exports.default = SearchBlockInstance;
/** Simple input box with magnifying glass */
class SearchControl extends React.Component {
class SearchControl extends react_1.default.Component {
constructor() {
super(...arguments);
this.inputRef = React.createRef();
this.inputRef = react_1.default.createRef();
this.handleChange = (ev) => {
if (this.props.onChange) {
this.props.onChange(ev.target.value);
Expand All @@ -128,9 +123,9 @@ class SearchControl extends React.Component {
}
}
render() {
return (React.createElement("div", { style: { position: "relative", display: "inline-block", margin: 5, width: "15em" } },
React.createElement("i", { className: "fa fa-search", style: { position: "absolute", right: 8, top: 10, color: "#AAA", pointerEvents: "none" } }),
React.createElement("input", { type: "text", ref: this.inputRef, className: "form-control", style: { width: "100%" }, value: this.props.value, onChange: this.handleChange, placeholder: this.props.placeholder })));
return (react_1.default.createElement("div", { style: { position: "relative", display: "inline-block", margin: 5, width: "15em" } },
react_1.default.createElement("i", { className: "fa fa-search", style: { position: "absolute", right: 8, top: 10, color: "#AAA", pointerEvents: "none" } }),
react_1.default.createElement("input", { type: "text", ref: this.inputRef, className: "form-control", style: { width: "100%" }, value: this.props.value, onChange: this.handleChange, placeholder: this.props.placeholder })));
}
}
exports.SearchControl = SearchControl;
4 changes: 2 additions & 2 deletions lib/widgets/blocks/search/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class SearchBlock extends LeafBlock_1.default {
}
let error;
// Validate expr
error = exprValidator.validateExpr(searchExpr, { table: rowsetCV.table, types: ["text", "enum", "enumset"] });
error = exprValidator.validateExpr(searchExpr, { table: rowsetCV.table, types: ["text", "enum", "enumset", "text[]"] });
if (error) {
return error;
}
Expand All @@ -76,7 +76,7 @@ class SearchBlock extends LeafBlock_1.default {
onItemsChange(value.concat(null));
};
return (React.createElement("div", null,
React.createElement(ListEditor_1.default, { items: value, onItemsChange: onItemsChange }, (expr, onExprChange) => (React.createElement(mwater_expressions_ui_1.ExprComponent, { value: expr, schema: props.schema, dataSource: props.dataSource, onChange: onExprChange, table: rowsetCV.table, types: ["text", "enum", "enumset"], variables: blocks_1.createExprVariables(props.contextVars) }))),
React.createElement(ListEditor_1.default, { items: value, onItemsChange: onItemsChange }, (expr, onExprChange) => (React.createElement(mwater_expressions_ui_1.ExprComponent, { value: expr, schema: props.schema, dataSource: props.dataSource, onChange: onExprChange, table: rowsetCV.table, types: ["text", "enum", "enumset", "text[]"], variables: blocks_1.createExprVariables(props.contextVars) }))),
React.createElement("button", { type: "button", className: "btn btn-link btn-sm", onClick: handleAddSearchExpr }, "+ Add Expression")));
}))
: null,
Expand Down
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 14 additions & 2 deletions src/widgets/blocks/search/SearchBlockInstance.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import * as React from "react";
import _ from "lodash";
import React from "react";
import { SearchBlockDef } from "./search";
import { createExprVariables, Filter } from "../../blocks";
import { Expr, ExprUtils } from "mwater-expressions";
import * as _ from "lodash";
import { localize } from "../../localization";
import { InstanceCtx } from "../../../contexts";
import { useState, useRef, useEffect } from "react";
Expand Down Expand Up @@ -43,6 +43,18 @@ const SearchBlockInstance = (props: {
} as Expr
}

if (exprType === "text[]") {
return {
type: "op",
op: "~*",
table: table,
exprs: [
{ type: "op", op: "to_text", table: table, exprs: [expr] },
{ type: "literal", valueType: "text", value: escapeRegex(searchText) }
]
} as Expr
}

if (exprType === "enum") {
// Find matching enums
const enumValues = exprUtils.getExprEnumValues(expr)!.filter(ev => localize(ev.name, instanceCtx.locale).toLowerCase().includes(searchText.toLowerCase()))
Expand Down
4 changes: 2 additions & 2 deletions src/widgets/blocks/search/search.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export class SearchBlock extends LeafBlock<SearchBlockDef> {
let error: string | null

// Validate expr
error = exprValidator.validateExpr(searchExpr, { table: rowsetCV.table, types: ["text", "enum", "enumset"] })
error = exprValidator.validateExpr(searchExpr, { table: rowsetCV.table, types: ["text", "enum", "enumset", "text[]"] })
if (error) {
return error
}
Expand Down Expand Up @@ -95,7 +95,7 @@ export class SearchBlock extends LeafBlock<SearchBlockDef> {
dataSource={props.dataSource}
onChange={onExprChange}
table={rowsetCV.table!}
types={["text", "enum", "enumset"]}
types={["text", "enum", "enumset", "text[]"]}
variables={createExprVariables(props.contextVars)}
/>
)}
Expand Down

0 comments on commit f1a6f72

Please sign in to comment.