/* * @Author: yangzonglong * @Date: 2021-09-21 15:33:58 * @version: v1.0.0 * @Descripttion: 工厂库位联动 * @LastEditors: yangzonglong * @LastEditTime: 2021-09-22 09:44:50 * @Auditor: */ import React, { memo, useState, useEffect } from 'react'; import { Cascader } from 'antd'; import { waitServer } from '../../utils/server'; import useConfig from './../../use/useConfig'; const CACHE_KEY = 'COMMON_PLANT_LOCATION'; const LOCACTION_FIND = '/bk/api/warehouse/StorageLocation/find'; type PlanLocationProps = { plantName: string; plantCode: string; storageLocationCode: string; storageLocationName: string; binFlag: boolean } export type OptionItemProps = { value: string; label: string; children: Array<{ label: string; value: string; binFlag?: boolean }> } interface IProps { value: string[]; onChange(data: (string | number)[]): void } export default memo(({ value, onChange }: IProps) => { const config = useConfig(); const [options, setOptions] = useState([]); useEffect(() => { let activity = true; waitServer(CACHE_KEY, { url: LOCACTION_FIND }, (result: { rows: PlanLocationProps[] }) => { if (!activity) return; // 当前组件已被卸载 const data: OptionItemProps[] = []; result?.rows?.forEach((item: PlanLocationProps) => { if (!data.length) { data.push({ label: item.plantName!, value: item.plantCode!, children: [] }) } if (!data.find(plant => plant.value === item.plantCode)) { data.push({ label: item.plantName!, value: item.plantCode!, children: [] }) } data.find(plant => plant.value === item.plantCode)?.children.push({ label: item.storageLocationName!, value: item.storageLocationCode!, binFlag: item.binFlag }); }) setOptions(data) }) return () => { activity = false }; }, []); return ( ) })