Skip to content

Instantly share code, notes, and snippets.

@Natanagar
Created July 16, 2020 19:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Natanagar/7d254768c80e7ce65e12c3b0b20dbb91 to your computer and use it in GitHub Desktop.
Save Natanagar/7d254768c80e7ce65e12c3b0b20dbb91 to your computer and use it in GitHub Desktop.
import React, { FC, useState } from 'react';
import {
TreeSelect,
Form,
//notification
} from 'antd';
import { CategoriesTreeMock } from '../products/__mock__/treeCategories';
//import getCategoriesStructure from 'src/model/categories/getCategoriesStructure';
const parserDataSelectTree = (array: any[]) => {
array.forEach((item, index) => {
item.value = item._id;
item.title = item.name;
if (item.childs && Array.isArray(item.childs)) {
item.children = item.childs;
parserDataSelectTree(item.children);
}
});
return array;
};
interface CategoryTreeFieldProps {
name?: string;
label?: string | React.ReactNode;
extra?: string;
initialValue?: string;
title?: string | React.ReactNode;
getCategoryId?: (value: string) => void;
}
const CategoryTreeField: FC<CategoryTreeFieldProps> = ({
name,
label,
extra,
initialValue,
title,
getCategoryId,
}) => {
const [categoriesTree, setCategoriesTree] = useState(
parserDataSelectTree(CategoriesTreeMock)
);
console.log(setCategoriesTree, categoriesTree);
/*useEffect(() => {
getCategoriesStructure()
.then((res: any) => setCategoriesTree(res))
.catch(() => {
notification.error({
message: 'Загрузка структуры категорий',
description: 'Ошибка',
});
});
}, []);*/
//const [value, setValue] = useState('');
const onChange = (value: any) => {
getCategoryId && getCategoryId(value);
//setValue(value);
};
return (
<Form.Item name="categoryId" label="Категория">
{title ? <div>{title}</div> : null}
<TreeSelect
showSearch
style={{ width: '100%' }}
defaultValue={undefined}
dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
treeData={categoriesTree}
placeholder="Категории"
onChange={onChange}
treeNodeFilterProp="name"
filterTreeNode={(value: string, treeNode: any) =>
treeNode.name.indexOf(value) > -1
}
/>
</Form.Item>
);
};
export default CategoryTreeField;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment