StepDiy.js 2.3 KB
Newer Older
钟是志's avatar
钟是志 committed
1
import React, { useState, useEffect, useMemo, useCallback, memo, useRef } from 'react';
2
import { Steps } from 'antd';
3
import ZdyTable from '@/webPublic/one_stop_public/Table';
4 5 6

const { Step } = Steps;

钟是志's avatar
钟是志 committed
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
const Child = memo((props) => {
	const { value, json, partFormProps } = props;
	const diyProps = useMemo(
		() => {
			let b = {};
			if (value && typeof value === 'object') {
				b = {
					...value,
				};
			}
			if (json.otherProps && typeof json.otherProps === 'string') {
				try {
					let a = new Function(json.otherProps)();
					if (a) {
						return {
							...a,
							...b,
						};
					}
					return b;
				} catch (e) {
					console.error('Step组件 otherProps错误');
				}
			} else {
				return b;
			}
		},
		[json.otherProps, value],
	);
36

钟是志's avatar
钟是志 committed
37
	const [current, setCurrent] = useState(diyProps?.StepsProps?.current || 0);
38

钟是志's avatar
钟是志 committed
39 40 41 42 43 44 45 46 47 48 49 50 51 52
	const partFormKey = useMemo(
		() => {
			if (typeof current !== 'undefined') {
				let item = diyProps.Step.find((g, index) => {
					return index === current;
				});
				if (item) {
					return item.formKey;
				}
			}
			return undefined;
		},
		[current],
	);
53

钟是志's avatar
钟是志 committed
54 55 56 57 58 59 60 61
	useEffect(
		() => {
			if (diyProps?.StepsProps?.current !== current) {
				setCurrent(diyProps?.StepsProps?.current);
			}
		},
		[diyProps?.StepsProps?.current],
	);
62

钟是志's avatar
钟是志 committed
63 64 65 66 67 68 69 70
	return (
		<div style={diyProps.outSideDivStyle}>
			<Steps {...diyProps.StepsProps} current={current}>
				{Array.isArray(diyProps.Step) &&
					diyProps.Step.map((g) => {
						return <Step {...g} key={g.title} />;
					})}
			</Steps>
71

钟是志's avatar
钟是志 committed
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
			{Array.isArray(diyProps.Step) &&
				diyProps.Step.filter((g) => !!g.formKey).map((g, index) => {
					return (
						<div key={g.formKey} style={{ display: partFormKey === g.formKey ? 'block' : 'none' }}>
							<ZdyTable
								{...partFormProps}
								currentFormTitle={'Steps组件的子表单' + g.formKey}
								key={g.formKey}
								currentFormKey={g.formKey}
                form={props.form}
								{...partFormProps?.datas[g.formKey]}
							/>
						</div>
					);
				})}
		</div>
	);
});

export default function StepDiy(props){
	const { value, json, partFormProps, form } = props;
	const js = useRef();
	useEffect(() => {
		js.current = json;
	}, []);
	if (!js.current) {
		return null;
	}
	return <Child json={js.current} value={value} partFormProps={partFormProps} form={form} />;
101
}