CompositeIndexForm.jsx 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. import {memo, useCallback, useEffect, useRef, useState} from 'react';
  2. import {ComputedEditor} from "../../../components/ComputedEditor";
  3. import {ProFormDigit, ProFormGroup, ProFormSelect} from "@ant-design/pro-components";
  4. import {useAppDispatch, useAppSelector} from "../../../../redux/configureStore";
  5. import {selectModals} from "../../MainPage/slice/selectors";
  6. import {Col, Row} from "antd";
  7. import {styled} from "styled-components";
  8. import {DATA_TYPE} from "../constants";
  9. import {getModalInfoList} from "../../MainPage/slice/thunks";
  10. import useI18NPrefix from "../../../hooks/useI18NPrefix";
  11. export const CompositeIndexForm = memo(({
  12. initialValues,
  13. onChange
  14. }) => {
  15. const t = useI18NPrefix('global');
  16. const computedEditorRef = useRef();
  17. const dispatch = useAppDispatch();
  18. const modals = useAppSelector(selectModals);
  19. const metricsRef = useRef([]);
  20. const [metrics,setMetrics]=useState([]);
  21. const [analyzes,setAnalyzes]=useState([]);
  22. const [dateDims, setDateDims] = useState([]);
  23. const [metricFormData, setMetricFormData] = useState([]);
  24. useEffect(() => {
  25. const initialSelector = async (modelId)=>{
  26. await onModalSelect(modelId)
  27. }
  28. if (initialValues?.modelId){
  29. initialSelector(initialValues?.modelId)
  30. }
  31. }, []);
  32. const onModalSelect = useCallback(async (modelId) => {
  33. if(Array.isArray(modelId)){
  34. modelId = modelId.join(',');
  35. }
  36. if (!modelId) return false
  37. // computedEditorRef?.current?.clear();
  38. await dispatch(getModalInfoList({
  39. id:modelId,
  40. resolve(data) {
  41. let dimensions = [];
  42. let metrics = [];
  43. if (Array.isArray(data)) {
  44. data.forEach(item => {
  45. if (!item) {
  46. return;
  47. }
  48. if (Array.isArray(item.dimensions)) {
  49. dimensions.push(...item.dimensions);
  50. }
  51. if (Array.isArray(item.metrics)) {
  52. metrics.push(...item.metrics);
  53. }
  54. });
  55. } else if (data) {
  56. dimensions = Array.isArray(data.dimensions) ? data.dimensions : [];
  57. metrics = Array.isArray(data.metrics) ? data.metrics : [];
  58. }
  59. const analyze = dimensions.filter(({type})=>type!=="time").map(({name, bizName, description, id})=>({
  60. label: description|| name|| bizName,
  61. value: id
  62. }))
  63. const date = dimensions.filter(({type})=>type==="time").map(({name, bizName, description, id})=>({
  64. label: description|| name|| bizName,
  65. value: id
  66. }))
  67. // todo批量设置不清空
  68. setMetrics(
  69. metrics.filter(item=>item.bizName!==initialValues?.bizName).map((v) => {
  70. return {
  71. ...v,
  72. analyzeArr: analyze,
  73. dateArr: date,
  74. }
  75. })
  76. );
  77. setAnalyzes(analyze);
  78. setDateDims(date);
  79. metricsRef.current=metrics;
  80. }
  81. }))
  82. // onChange?.(computedEditorRef.current?.formatValue(initialValues.expr,'change'))
  83. }, [])
  84. const onComputedEditorChange = useCallback((expression)=>{
  85. if (expression){
  86. const data = computedEditorRef.current?.formatValue(expression,'change');
  87. console.log(data);
  88. setMetricFormData(data?.metrics)
  89. onChange?.(data)
  90. }
  91. },[])
  92. return (
  93. <>
  94. <ProFormSelect
  95. name="modelId"
  96. label={t("formItem.model")}
  97. rules={[{required: true}]}
  98. options={modals}
  99. onChange={onModalSelect}
  100. fieldProps={{
  101. mode: 'multiple'
  102. }}
  103. />
  104. <Row>
  105. <Col span={4}>
  106. <CardLabel children={t("formItem.formIndex")+":"} className="required"/>
  107. </Col>
  108. <Col span={18}>
  109. <ComputedEditor
  110. value={initialValues.expr}
  111. onChange={onComputedEditorChange}
  112. fields={metrics}
  113. ref={computedEditorRef}
  114. />
  115. </Col>
  116. </Row>
  117. <ProFormGroup style={{marginLeft: 105}}>
  118. <ProFormSelect
  119. name="dataFormatType"
  120. label={t('formItem.dataFormat')}
  121. valueEnum={DATA_TYPE}
  122. width={480}
  123. />
  124. <ProFormDigit name="decimalPlaces" placeholder={t("placeholder.decimalPlaces")} initialValue={initialValues?.dataFormat?.decimalPlaces} min={1} max={10}/>
  125. </ProFormGroup>
  126. <Row >
  127. {metricFormData.map((item, index) => {
  128. return <>
  129. <Col span={Math.floor(24 / (metricFormData.length + 1))} key={index}>
  130. <ProFormSelect
  131. name={['metrics.dimensionId',index]}
  132. options={item?.analyzeArr}
  133. label={t("formItem.dimension")}
  134. fieldProps={{
  135. mode: 'multiple'
  136. }}
  137. />
  138. <ProFormSelect
  139. name={['metrics.dateDimId',index]}
  140. label={t("formItem.dateDimension")}
  141. rules={[{required: true}]}
  142. options={item?.dateArr}
  143. />
  144. </Col>
  145. </>
  146. })}
  147. </Row>
  148. </>
  149. )
  150. })
  151. const CardLabel = styled.label`
  152. position: absolute;
  153. right: 10px;
  154. top: 20px;
  155. &.required {
  156. // 添加必填红色*的样式
  157. &::before {
  158. display: inline-block;
  159. margin-inline-end: 4px;
  160. color: #ff4d4f;
  161. font-size: 14px;
  162. font-family: SimSun, sans-serif;
  163. line-height: 1;
  164. content: "*";
  165. }
  166. }
  167. `