frontend/сomponents/search/index.tsx

211 lines
7.4 KiB
TypeScript
Raw Normal View History

2022-10-22 00:34:07 +03:00
import React, { useState } from "react";
2022-10-23 02:12:31 +03:00
import { AutoComplete, Input, Tag, Tooltip } from 'antd';
2022-10-23 02:14:01 +03:00
import { InputNumber, Popover, Radio } from 'antd';
2022-10-22 19:11:56 +03:00
import { useAppDispatch, useAppSelector } from "../../hooks";
2022-10-23 09:02:36 +03:00
import { createNode, deleteNode, hints, INode, loading, nodes, products, setLoading, updateNumberNode, updateOperationNode } from "../../store/reducers/nodesInputReducer";
2022-10-22 19:11:56 +03:00
import { createHints, search } from "../../store/reducers/asyncActions";
2022-10-22 21:01:16 +03:00
import styles from "./search.module.css"
2022-10-23 02:08:58 +03:00
import {CalculatorOutlined} from '@ant-design/icons';
import 'antd/dist/antd.css';
2022-10-22 00:34:07 +03:00
2022-10-22 22:44:45 +03:00
2022-10-23 02:12:31 +03:00
function parse_types(type: string) {
if (type == 'Name') return 'Наименование'
if (type == 'Category') return 'Категория'
return type;
}
2022-10-22 22:44:45 +03:00
2022-10-22 00:34:07 +03:00
export const Search: React.FC<{onData:(data:any)=>void}> = (props) =>{
const [data, setData] = useState("")
2022-10-22 19:11:56 +03:00
const [tags, setTags] = useState(new Array<JSX.Element>())
const dispatch = useAppDispatch();
2022-10-22 22:44:45 +03:00
const getNodes = useAppSelector(nodes);
2022-10-22 23:41:15 +03:00
const getProducts = useAppSelector(products);
2022-10-22 19:11:56 +03:00
const getHints = useAppSelector(hints);
2022-10-23 00:17:15 +03:00
const getLoading = useAppSelector(loading)
2022-10-23 02:08:58 +03:00
const [disableInput, setDisableInput] = useState(false)
2022-10-22 19:11:56 +03:00
const [autoCompleteValue, setAutoCompleteValue] = useState("")
const onChange = (text:string) =>{
2022-10-23 00:17:15 +03:00
if (text.length >= 3 && text.length%2 == 0){
2022-10-22 19:11:56 +03:00
dispatch(
createHints({word:text, hints:getHints.length == 0? []: getHints.map((el)=>el.value)})
)
}
setData(text)
}
const addTag = (value:INode) => {
let color = "red"
2022-10-23 02:08:58 +03:00
switch(value.type){
2022-10-22 19:11:56 +03:00
case "Category":
color = "gold"
break
case "Name":
color = "green"
break
case "All":
color = "gray"
break
}
let tag = <Tag
color={color}
closable
style={{ marginRight: 3 }}
2022-10-22 22:44:45 +03:00
onClose={() => {
dispatch(
deleteNode(value.value)
)
}}
2022-10-22 19:11:56 +03:00
>
{value.value.length <13? value.value:value.value.slice(0,10)+"..."}
</Tag>
2022-10-23 02:08:58 +03:00
if (value.type.split("_")[1] == "numeric"){
let popver = <div className={styles.popover}>
<div>{value.type.split("_")[0]}</div>
2022-10-23 09:02:36 +03:00
<Radio.Group defaultValue="=" size="small" onChange={(e) => {
console.log(e.target.value)
dispatch(
updateOperationNode({value: value.value, operation: e.target.value})
)
}}>
2022-10-23 02:08:58 +03:00
<Radio.Button value=">="></Radio.Button>
<Radio.Button value="=">=</Radio.Button>
<Radio.Button value="<="></Radio.Button>
</Radio.Group>
2022-10-23 09:02:36 +03:00
<InputNumber
autoFocus
onClick={()=>setDisableInput(true)}
size="small"
min={1}
max={100000}
defaultValue={100}
onChange={(ee) => {
console.log(ee)
dispatch(
updateNumberNode({value: value.value, number: ee!})
)
}}
/>
2022-10-23 02:08:58 +03:00
</div>
2022-10-23 09:02:36 +03:00
tag = <Popover onOpenChange={(e)=> e?null:setDisableInput(false)} content={popver} title="Задать значение">
2022-10-23 02:08:58 +03:00
<Tag
color={"cyan"}
closable
style={{ marginRight: 3 }}
onClose={() => {
dispatch(
deleteNode(value.value)
)
}}
>
2022-10-23 09:02:36 +03:00
<CalculatorOutlined
onChange={(e) => {
console.log(e)
}}
></CalculatorOutlined>
2022-10-23 02:08:58 +03:00
{value.value.length < 13? value.value:value.value.slice(0,10)+"..."}
</Tag>
</Popover>
}
2022-10-22 19:11:56 +03:00
setTags(tags.concat([tag]))
}
2022-10-22 23:11:32 +03:00
const onSelect = (value:string, type:any) =>{
2022-10-23 09:02:36 +03:00
var vts = value.split('--');
2022-10-22 23:11:32 +03:00
addTag({
'type': vts[1],
'value': vts[0]
})
2022-10-23 09:02:36 +03:00
if (vts[1].endsWith('_numeric')){
vts[1] = '*'+vts[1].split('_')[0]
vts[0] = '=0'
}
2022-10-22 23:11:32 +03:00
dispatch(createNode({
'type': vts[1],
'value': vts[0]
}));
2022-10-22 19:11:56 +03:00
setAutoCompleteValue("")
}
2022-10-22 00:34:07 +03:00
const onEnter = (value:any) => {
2022-10-22 23:11:32 +03:00
console.log(getNodes);
2022-10-23 00:17:15 +03:00
dispatch(setLoading(true))
2022-10-22 20:03:14 +03:00
dispatch(
search(
2022-10-22 22:44:45 +03:00
getNodes.concat(
2022-10-22 20:03:14 +03:00
autoCompleteValue.length ?
[
{
'type': 'All',
'value': autoCompleteValue
}
] : []
)
)
)
2022-10-22 00:34:07 +03:00
}
2022-10-22 22:44:45 +03:00
if (autoCompleteValue.endsWith(' ')) {
dispatch(
createNode({
type: "All",
value: autoCompleteValue.slice(0, autoCompleteValue.length-2)
})
)
addTag({
type: "All",
value: autoCompleteValue.slice(0, autoCompleteValue.length-2)
})
setAutoCompleteValue('');
}
2022-10-22 00:34:07 +03:00
return(
2022-10-22 19:11:56 +03:00
<AutoComplete
2022-10-23 02:08:58 +03:00
autoFocus={false}
onBlur={()=>null}
disabled={disableInput}
2022-10-22 22:44:45 +03:00
options={getHints.map((e) => {
const pre_str = e.value.value.slice(0, e.coordinate)
const after_str = e.value.value.slice(e.coordinate+autoCompleteValue.length, e.value.value.length)
const bold_str = e.value.value.slice(e.coordinate, e.coordinate+autoCompleteValue.length)
return {
2022-10-23 02:12:31 +03:00
label: <Tooltip title={parse_types(e.value.type)} placement={'topRight'}>
<div>
<span>{pre_str}</span>
{bold_str.toLocaleLowerCase () == autoCompleteValue.toLowerCase() ? <strong>{bold_str}</strong> : <span>{bold_str}</span>}
<span>{after_str}</span>
</div>
</Tooltip>,
2022-10-23 09:02:36 +03:00
value: e.value.value + '--' + e.value.type + '--' + Date.now().toString()
2022-10-22 22:44:45 +03:00
}
})}
2022-10-22 19:11:56 +03:00
onSelect={onSelect as any}
value={autoCompleteValue}
2022-10-22 20:03:14 +03:00
onChange={(e: any)=>setAutoCompleteValue(e)}
2022-10-22 19:11:56 +03:00
// onSearch={handleSearch}
2022-10-22 22:45:54 +03:00
dropdownMatchSelectWidth={252}
2022-10-22 19:11:56 +03:00
>
<Input.Search prefix={tags}
2022-10-23 02:08:58 +03:00
autoFocus={false}
onBlur={()=>null}
style={{ width: "60vw" }}
2022-10-22 22:40:21 +03:00
color="red-6"
2022-10-22 21:01:16 +03:00
className={styles.search}
2022-10-22 19:11:56 +03:00
onChange={(e)=>onChange(e.target.value)}
value={data}
onSearch={(e) => onEnter(e)}
size="large"
placeholder="Поиск товара"
2022-10-23 00:17:15 +03:00
loading={getLoading}
2022-10-22 19:11:56 +03:00
enterButton />
</AutoComplete>
2022-10-22 00:34:07 +03:00
);
}