import {useFunctionWrapper} from "./useFunctionWrapper"; import {computed, inject, onBeforeUnmount, PropType, provide, reactive} from '@peryl/react-compose'; import {eMultipleValueType} from "./useMultipleModel"; import {PlainObject} from "@peryl/utils/event"; import {createEffects} from "@peryl/utils/createEffects"; export const EDIT_PROVIDER = '@@EDIT_PROVIDER'; export const EditProps = { disabled: { type: Boolean, default: null }, readonly: { type: Boolean, default: null }, loading: { type: Boolean, default: null }, placeholder: { type: String, default: null }, customReadonly: { type: Boolean, default: null }, valueType: { type: String as PropType }, // 多选的时候值的类型 multiple: { type: Boolean }, // 是否多选 } as const; export interface EditProvideData { editable: boolean | null, disabled: boolean | null, readonly: boolean | null, loading: boolean | null, placeholder: string | null, onBlur?: (...args: any[]) => void, onChange?: (...args: any[]) => void, } export const useEdit = useFunctionWrapper( 'edit', (ctx, option: { adjust?: (data: EditProvideData) => void | EditProvideData } = {}) => { /*销毁时处理掉useEdit产生的副作用*/ const { effects } = createEffects(); const parentEditComputed = inject(EDIT_PROVIDER, null) as null | { value: EditProvideData }; const editState = reactive({ loading: null as null | boolean }); const editComputed = computed((): EditProvideData => { if (ctx.isDestroyed) {return {} as EditProvideData;} const { disabled, readonly, loading, placeholder } = ctx.props as any as EditProvideData; let data: EditProvideData = { disabled, readonly, loading, placeholder, editable: false }; if (data.disabled == null) data.disabled = !!parentEditComputed ? parentEditComputed.value.disabled : false; if (data.readonly == null) data.readonly = !!parentEditComputed ? parentEditComputed.value.readonly : false; if (data.placeholder == null) data.placeholder = !!parentEditComputed ? parentEditComputed.value.placeholder : null; if (editState.loading == null) { data.loading = data.loading != null ? data.loading : (!!parentEditComputed ? parentEditComputed.value.loading : false); } else { data.loading = editState.loading; } /*合并自身的onChange以及父edit的onChange*/ data.onChange = mergeHandlers({ methodName: 'onChange', parent: parentEditComputed?.value, self: data, }); /*合并自身的onBlur以及父edit的onBlur*/ data.onBlur = mergeHandlers({ methodName: 'onBlur', parent: parentEditComputed?.value, self: data, }); if (!!option.adjust) data = option.adjust(data) || data; return { ...data, editable: !data.disabled && !data.readonly && !data.loading }; }); effects.push(() => {editComputed.effect.stop();}); provide(EDIT_PROVIDER, editComputed); const event = (ctx as any).event; /*处理onBlur事件传递*/ if (event.on.onBlur && !!editComputed.value.onBlur) { event.on.onBlur(editComputed.value.onBlur); effects.push(() => event.off.onBlur(editComputed.value.onBlur)); } /*处理onChange传递*/ if (event.on.onChange && !!editComputed.value.onChange) { event.on.onChange(editComputed.value.onChange); effects.push(() => event.off.onChange(editComputed.value.onChange)); } onBeforeUnmount(effects.clear); return { editState, editComputed, }; }); export type tEditControl = ReturnType export type tEditComputed = tEditControl["editComputed"] const mergeHandlers = ({ methodName, parent, self }: { methodName: string, parent: PlainObject | undefined, self: PlainObject }) => { if (parent?.[methodName] || self[methodName]) { const selfOnChange = self[methodName]; const parentOnChange = parent?.[methodName]; return (...args: any[]) => { // console.log({ args, methodName, parentOnChange, selfOnChange }); selfOnChange?.(...args); parentOnChange?.(...args); }; } return undefined; };