import React, { useEffect, useRef, useState } from 'react'; import { useSelector, useDispatch } from 'react-redux'; import axios from 'axios'; import { Collapse, Form, Input, Select, Alert, Switch, Button, Cascader, } from '@arco-design/web-react'; import { FormInstance } from '@arco-design/web-react/es/Form'; import { ReducerState } from '../../redux'; import { UPDATE_FORM, GO_NEXT, UPDATE_ACTIVE_KEYS } from './redux/actionTypes'; import useLocale from '../../utils/useLocale'; import styles from './style/index.module.less'; function BaseInfo() { const locale = useLocale(); const state = useSelector((state: ReducerState) => state.stepForm); const dispatch = useDispatch(); const { data, activeKeys } = state; const formRef = useRef(); const [clusterOptions, setClusterOptions] = useState([]); const [lineOptions, setLineOptions] = useState([]); const fetchClusterOptions = () => { axios.get(`/api/clusterList`).then((res) => { setClusterOptions(res.data || []); }); }; const fetchLineOptions = (cluster) => { axios .get('/api/lineList', { params: { cluster, }, }) .then((res) => { setLineOptions(res.data || []); // 默认选中第一个 if (!data.lineName && res.data.length && formRef.current) { formRef.current.setFieldValue('lineName', res.data[0].value); } }); }; const onCollapseChange = (_key, activeKeys) => { dispatch({ type: UPDATE_ACTIVE_KEYS, payload: { activeKeys } }); }; const onClusterChange = (value) => { const cluster = Array.isArray(value) && value[value.length - 1]; if (formRef.current) { formRef.current.setFieldValue('lineName', undefined); } if (cluster) { fetchLineOptions(cluster); } else { setLineOptions([]); } }; const onNextClick = () => { formRef.current.validate().then(() => { dispatch({ type: GO_NEXT }); }); }; useEffect(() => { fetchClusterOptions(); }, []); useEffect(() => { formRef.current.setFieldsValue(data); data.cluster && fetchLineOptions(data.cluster[0]); }, [data]); useEffect(() => { return () => { const formData = formRef.current?.getFieldsValue() || {}; dispatch({ type: UPDATE_FORM, payload: { data: formData } }); }; }, []); return (
); } export default BaseInfo;