| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170 |
- import type { ColEx } from '../types';
- import type { AdvanceState } from '../types/hooks';
- import type { ComputedRef, Ref } from 'vue';
- import type { FormProps, FormSchema } from '../types/form';
- import { computed, unref, watch } from 'vue';
- import { isBoolean, isFunction, isNumber, isObject } from '/@/utils/is';
- import { useBreakpoint } from '/@/hooks/event/useBreakpoint';
- import { useDebounceFn } from '@vueuse/core';
- const BASIC_COL_LEN = 24;
- interface UseAdvancedContext {
- advanceState: AdvanceState;
- emit: EmitType;
- getProps: ComputedRef<FormProps>;
- getSchema: ComputedRef<FormSchema[]>;
- formModel: Recordable;
- defaultValueRef: Ref<Recordable>;
- }
- export default function ({
- advanceState,
- emit,
- getProps,
- getSchema,
- formModel,
- defaultValueRef,
- }: UseAdvancedContext) {
- const { realWidthRef, screenEnum, screenRef } = useBreakpoint();
- const getEmptySpan = computed((): number => {
- if (!advanceState.isAdvanced) {
- return 0;
- }
- // For some special cases, you need to manually specify additional blank lines
- const emptySpan = unref(getProps).emptySpan || 0;
- if (isNumber(emptySpan)) {
- return emptySpan;
- }
- if (isObject(emptySpan)) {
- const { span = 0 } = emptySpan;
- const screen = unref(screenRef) as string;
- const screenSpan = (emptySpan as any)[screen.toLowerCase()];
- return screenSpan || span || 0;
- }
- return 0;
- });
- const debounceUpdateAdvanced = useDebounceFn(updateAdvanced, 30);
- watch(
- [
- // TODO
- // () => unref(getSchema),
- () => advanceState.isAdvanced,
- () => unref(realWidthRef),
- ],
- () => {
- const { showAdvancedButton } = unref(getProps);
- if (showAdvancedButton) {
- debounceUpdateAdvanced();
- }
- },
- { immediate: true }
- );
- function getAdvanced(itemCol: Partial<ColEx>, itemColSum = 0, isLastAction = false) {
- const width = unref(realWidthRef);
- const mdWidth =
- parseInt(itemCol.md as string) ||
- parseInt(itemCol.xs as string) ||
- parseInt(itemCol.sm as string) ||
- (itemCol.span as number) ||
- BASIC_COL_LEN;
- const lgWidth = parseInt(itemCol.lg as string) || mdWidth;
- const xlWidth = parseInt(itemCol.xl as string) || lgWidth;
- const xxlWidth = parseInt(itemCol.xxl as string) || xlWidth;
- if (width <= screenEnum.LG) {
- itemColSum += mdWidth;
- } else if (width < screenEnum.XL) {
- itemColSum += lgWidth;
- } else if (width < screenEnum.XXL) {
- itemColSum += xlWidth;
- } else {
- itemColSum += xxlWidth;
- }
- if (isLastAction) {
- advanceState.hideAdvanceBtn = false;
- if (itemColSum <= BASIC_COL_LEN * 2) {
- // When less than or equal to 2 lines, the collapse and expand buttons are not displayed
- advanceState.hideAdvanceBtn = true;
- advanceState.isAdvanced = true;
- } else if (
- itemColSum > BASIC_COL_LEN * 2 &&
- itemColSum <= BASIC_COL_LEN * (unref(getProps).autoAdvancedLine || 3)
- ) {
- advanceState.hideAdvanceBtn = false;
- // More than 3 lines collapsed by default
- } else if (!advanceState.isLoad) {
- advanceState.isLoad = true;
- advanceState.isAdvanced = !advanceState.isAdvanced;
- }
- return { isAdvanced: advanceState.isAdvanced, itemColSum };
- }
- if (itemColSum > BASIC_COL_LEN) {
- return { isAdvanced: advanceState.isAdvanced, itemColSum };
- } else {
- // The first line is always displayed
- return { isAdvanced: true, itemColSum };
- }
- }
- function updateAdvanced() {
- let itemColSum = 0;
- let realItemColSum = 0;
- const { baseColProps = {} } = unref(getProps);
- for (const schema of unref(getSchema)) {
- const { show, colProps } = schema;
- let isShow = true;
- if (isBoolean(show)) {
- isShow = show;
- }
- if (isFunction(show)) {
- isShow = show({
- schema: schema,
- model: formModel,
- field: schema.field,
- values: {
- ...unref(defaultValueRef),
- ...formModel,
- },
- });
- }
- if (isShow && (colProps || baseColProps)) {
- const { itemColSum: sum, isAdvanced } = getAdvanced(
- { ...baseColProps, ...colProps },
- itemColSum
- );
- itemColSum = sum || 0;
- if (isAdvanced) {
- realItemColSum = itemColSum;
- }
- schema.isAdvanced = isAdvanced;
- }
- }
- advanceState.actionSpan = (realItemColSum % BASIC_COL_LEN) + unref(getEmptySpan);
- getAdvanced(unref(getProps).actionColOptions || { span: BASIC_COL_LEN }, itemColSum, true);
- emit('advanced-change');
- }
- function handleToggleAdvanced() {
- advanceState.isAdvanced = !advanceState.isAdvanced;
- }
- return { handleToggleAdvanced };
- }
|