Add SelectFromDictionary

This commit is contained in:
Фролов 2021-08-20 12:31:24 +05:00
parent a1173fb6a4
commit d9969a7468
4 changed files with 52 additions and 44 deletions

View File

@ -0,0 +1,23 @@
import {Select} from 'antd'
const { Option } = Select
interface StandardComponentProps {
dictionary: Map<any, any>,
}
export const SelectFromDictionary = ({dictionary, ...other}: StandardComponentProps) =>{
const options: any[] = []
dictionary.forEach((value, key) => {
options.push(
<Option
key={key}
value={key}>
{value}
</Option>)
})
return <Select {...other}>
{options}
</Select>
}

View File

@ -1,6 +1,7 @@
export { Table } from 'antd'
export { EditableTable } from './EditableTable'
export { DatePickerWrapper } from './DatePickerWrapper'
export { Table } from 'antd'
export { SelectFromDictionary } from './SelectFromDictionary'
export const RegExpIsFloat = /^[-+]?\d+\.?\d*$/
export const formatDate='YYYY.MM.DD HH:mm'
@ -80,4 +81,4 @@ export const makePaginationObject = (paginationContainer:PaginationContainer, ..
total: paginationContainer.count ?? paginationContainer.items?.length ?? 0,
current: page,
}
}
}

View File

@ -1,13 +1,12 @@
import { useState, useEffect } from 'react'
import { Input, Select } from 'antd'
import { Input } from 'antd'
import moment from 'moment'
import { EditableTable, DatePickerWrapper, numericColumnOptions, makeColumn } from "../../components/Table"
import { EditableTable, DatePickerWrapper, SelectFromDictionary, numericColumnOptions, makeColumn } from "../../components/Table"
import LoaderPortal from '../../components/LoaderPortal'
import { invokeWebApiWrapperAsync } from '../../components/factory'
import { WellOperationService} from '../../services/api'
import { dictionarySectionType, getNameById } from './dictionary'
import { dictionarySectionType, getByKeyOrReturnKey } from './dictionary'
const { Option } = Select
const { TextArea } = Input;
const basePageSize = 160;
@ -16,14 +15,16 @@ const format='YYYY.MM.DD HH:mm'
export const WellOperationsEditor = ({idWell, idType}) => {
const [pageNumAndPageSize, setPageNumAndPageSize] = useState({current:1, pageSize:basePageSize})
const [paginationTotal, setPaginationTotal] = useState(0)
const [dictionaryOperationCategory, setDictionaryOperationCategory] = useState([])
const [dictionaryOperationCategory, setDictionaryOperationCategory] = useState(new Map())
const [operations, setOperations] = useState([])
const [showLoader, setShowLoader] = useState(false)
useEffect(() => invokeWebApiWrapperAsync(
async () => {
const r = await WellOperationService.getCategories(idWell)
setDictionaryOperationCategory(r)
const categories = await WellOperationService.getCategories(idWell)
const dictCategories = new Map()
categories?.forEach((item) => dictCategories.set(item.id, item.name))
setDictionaryOperationCategory(dictCategories)
}),[idWell])
const updateOperations = () => invokeWebApiWrapperAsync(
@ -42,34 +43,18 @@ export const WellOperationsEditor = ({idWell, idType}) => {
useEffect(updateOperations, [idWell, idType, pageNumAndPageSize])
const SelectorSectionType = <Select>
{dictionarySectionType.map(item =><Option
key={item.id}
value={item.id}>
{item.name}
</Option>)}
</Select>
const SelectorOperationCategory = <Select>
{dictionaryOperationCategory.map(item =><Option
key={item.id}
value={item.id}>
{item.name}
</Option>)}
</Select>
const columns = [
makeColumn('Конструкция секции','idWellSectionType', {
editable:true,
input:SelectorSectionType,
width:105,
render:(_, record)=>getNameById(dictionarySectionType, record.idWellSectionType)
input:<SelectFromDictionary dictionary={dictionarySectionType}/>,
width:110,
render:(_, record)=>getByKeyOrReturnKey(dictionarySectionType, record.idWellSectionType)
}),
makeColumn('Операция','idCategory', {
editable:true,
input:SelectorOperationCategory,
input:<SelectFromDictionary dictionary={dictionaryOperationCategory}/>,
width:200,
render:(_, record)=>getNameById(dictionaryOperationCategory, record.idCategory)
render:(_, record)=>getByKeyOrReturnKey(dictionaryOperationCategory, record.idCategory)
}),
makeColumn('Доп. инфо','categoryInfo', {editable:true, width:300, input:<TextArea/>}),
makeColumn('Глубина забоя','wellDepth', numericColumnOptions),

View File

@ -1,15 +1,14 @@
export const getNameById = (dictionary, id) => {
if(!dictionary?.length)
return id
const item = dictionary.find(i => i?.id === id)
return item?.name ?? id
}
export const dictionarySectionType = new Map([
[1, 'Пилотный ствол'],
[2, 'Направление'],
[3, 'Кондуктор'],
[4, 'Эксплуатационная колонна'],
[5, 'Транспортный ствол'],
[6, 'Хвостовик'],
])
export const dictionarySectionType = [
{id:1, name:'Пилотный ствол'},
{id:2, name:'Направление'},
{id:3, name:'Кондуктор'},
{id:4, name:'Эксплуатационная колонна'},
{id:5, name:'Транспортный ствол'},
{id:6, name:'Хвостовик'},
]
export const getByKeyOrReturnKey = (dictionary, key) => {
if(!dictionary)
return key
return dictionary.get(key) ?? key
}