import {computed, ExtractPropTypes, inject, PropType, provide, useStyles} from "@peryl/react-compose"; import {createEnum} from "@peryl/utils/createEnum"; import {unit} from "@peryl/utils/unit"; import {removeUnit} from "@peryl/utils/removeUnit"; import {InputMode, ThemeSize, tStyleComputed} from "../../../uses/useStyle"; import ApplicationConfigurationProvider from "../../ApplicationConfigurationProvider"; import {PlainObject} from "@peryl/utils/event"; /** * 表单的文本以及内容对齐方式 * @author 韦胜健 * @date 2022.9.4 23:06 */ export const eFormLabelAlign = createEnum(['left', 'center', 'right'] as const); /** * 表单的内容对其方式 * @author 韦胜健 * @date 2022.9.4 23:19 */ export const eFormContentAlign = createEnum([ 'left', 'center', 'right' ] as const); /** * Form布局相关属性 * @author 韦胜健 * @date 2022.9.4 22:46 */ export const FormLayoutPropsOption = { size: { type: String as PropType }, // 表单尺寸 column: { type: [String, Number] as PropType, default: 1 }, // 多列表单的列数 columnGutter: { type: [Number, String] as PropType, default: '16px' }, // 列之间的间距 singleColumnCenter: { type: Boolean as PropType }, // 单列的时候会使得表单内容居中,表单文本标题不计宽度,设置该属性为true则使得文本宽度参与计算居中 labelWidth: { type: [String, Number] as PropType }, // formItem 文本宽度 labelAlign: { type: String as PropType }, // 文本对其方式 contentAlign: { type: String as PropType }, // 内容对其方式 singleColumnContentWidth: { type: Number }, // 单列的时候form item content的宽度 width: { type: [String, Number] as PropType, }, // 表单宽度 colon: { type: Boolean as PropType, default: true }, // label的冒号 verticalLabel: { type: Boolean as PropType }, // 纵向的表单 gutter: { type: Number }, // 列间隔 rowGutter: { type: Number }, // 行间隔 validateMessagePosition: { type: String as PropType }, // 校验消息的位置 hideLabel: { type: Boolean }, // 隐藏form-item的label inputMode: { type: String as PropType }, // 内部Input的类型mode } as const; /** * FormLayoutProps属性类型 * @author 韦胜健 * @date 2022.9.4 22:48 */ export type tFormLayoutPropsType = ExtractPropTypes; /** * FormItem布局相关属性 * @author 韦胜健 * @date 2022.9.4 22:46 */ export const FormItemLayoutPropsOption = { hideLabel: { type: Boolean }, // 隐藏label labelWidth: { type: [String, Number] as PropType }, // 显示文本宽度 column: { type: [String, Number] as PropType, default: 1 }, // 多列表单的列数 block: { type: Boolean as PropType }, // 占用一行 labelAlign: { type: String as PropType }, // label 对齐方式 contentAlign: { type: String as PropType }, // content 对齐方式 singleColumnContentWidth: { type: Number }, // 单列的时候form item content的宽度 colon: { type: Boolean as PropType, default: null }, // label的冒号 validateMessagePosition: { type: String as PropType }, // 校验消息的位置 negativeInnerMarginTop: { type: Boolean }, // inner节点启用负的marginTop值 tip: { type: [String, Function] as PropType any)> }, // 提示说明 } as const; /** * FormItemLayoutProps属性类型 * @author 韦胜健 * @date 2022.9.4 22:49 */ export type tFormItemLayoutPropsType = ExtractPropTypes /** * 默认的文本宽度 * @author 韦胜健 * @date 2022.9.4 23:21 */ export const DefaultFormLabelWidth = (() => { let value = '150px'; return { value }; })(); /** * 布局组合 * @author 韦胜健 * @date 2022.9.4 22:50 */ export const useFormLayout = (() => { const FORM_LAYOUT_PROVIDER = '@@FORM_LAYOUT_PROVIDER'; const FORM_ITEM_LAYOUT_PROVIDER = '@@FORM_ITEM_LAYOUT_PROVIDER'; const useForm = (props: tFormLayoutPropsType, styleComputed: tStyleComputed) => { const configuration = ApplicationConfigurationProvider.inject(); /*是否为单列*/ const isSingleColumn = computed(() => props.column === 1); /*列间距*/ const gutter = computed((): number => { if (props.gutter != null) { return removeUnit(props.gutter) as any; } /*单列的情况下不设置列间距*/ return isSingleColumn.value ? 0 : FormSizeGutter[styleComputed.value.size]; }); /*行间距*/ const rowGutter = computed((): number => { if (props.inputMode === 'text') {return 4;} if (props.rowGutter != null) { return removeUnit(props.rowGutter) as any; } return Math.max( 1.25 * ((removeUnit(configuration.value.theme.vars[`margin-${styleComputed.value.size}`]) || 0) as number), 14, ); }); /*校验消息的位置*/ const validateMessagePosition = computed(() => { if (!!props.validateMessagePosition) {return props.validateMessagePosition;} /*纵向文本的情况下,校验消息默认放在左下角*/ if (props.verticalLabel) {return FormValidateMessagePosition["bottom-right"];} if (isSingleColumn.value) { /*单列的情况下,校验消息默认放在右侧*/ return FormValidateMessagePosition['right']; } else { /*其他都放在右下角*/ return FormValidateMessagePosition['bottom-right']; } }); /*根节点样式*/ const styles = useStyles((styles) => { if (!isSingleColumn.value) { // styles.padding = `${rowGutter.value / 2}px ${gutter.value}px`; } }); /*inner节点样式,有列间距时拓展宽度*/ const innerStyles = useStyles(style => { if (!isSingleColumn.value && !!gutter.value) { style.width = `calc(100% + ${unit(gutter.value)})`; } style.margin = `${-rowGutter.value / 2}px ${unit(-gutter.value / 2)}`; }); const refer = { props, styles, gutter, rowGutter, innerStyles, isSingleColumn, validateMessagePosition }; provide(FORM_LAYOUT_PROVIDER, refer); return refer; }; const useFormItem = (props: tFormItemLayoutPropsType) => { const form = inject>(FORM_LAYOUT_PROVIDER)!; const parentFormItem = inject(FORM_ITEM_LAYOUT_PROVIDER, null); /** * 文本宽度 * @author 韦胜健 * @date 2022.9.4 23:21 */ const labelWidth = computed((): number | string => { if (form.props.hideLabel) { return 0; } /*纵向文本表单情况下,文本宽度占用整行*/ if (form.props.verticalLabel) { return '100%'; } let width = props.labelWidth != null ? props.labelWidth : form.props.labelWidth; /*如果没有设置宽度,单列的情况下不设置宽度使得label自动撑开宽度,多列的情况下设置默认宽度*/ if (width == null && form.props.column !== 1) { width = DefaultFormLabelWidth.value; } else { width = Number(width); } return width; }); /** * 占用的列数 * @author 韦胜健 * @date 2022.9.4 23:22 */ const column = computed((): number => { let column = props.column || 1; if (props.block) { column = Number(form.props.column); } return Number(column); }); /** * 冒号 * @author 韦胜健 * @date 2022.9.4 23:22 */ const colon = computed((): boolean => { if (props.colon != null) { return props.colon; } else { return form.props.colon; } }); /** * 文本对其方式 * @author 韦胜健 * @date 2022.9.4 23:22 */ const labelAlign = computed(() => form.props.verticalLabel ? eFormLabelAlign.left : (props.labelAlign || form.props.labelAlign)); /** * 内容对其方式 * @author 韦胜健 * @date 2022.9.4 23:22 */ const contentAlign = computed(() => props.contentAlign || form.props.contentAlign); const validateMessagePosition = computed(() => props.validateMessagePosition || form.validateMessagePosition.value); /** * 根节点样式 * @author 韦胜健 * @date 2022.9.4 23:23 */ const styles = useStyles((styles) => { styles.width = (() => { const totalColumn = Number(form.props.column); const itemColumn = column.value; if (itemColumn === totalColumn) { return '100%'; } return `calc(${Number((itemColumn / totalColumn).toFixed(4)) * 100}%)`; })(); styles.padding = `0 ${unit(form.gutter.value / 2)}`; if (!parentFormItem) { styles.margin = `${form.rowGutter.value / 2}px 0`; } else { styles.marginTop = `${form.rowGutter.value}px`; } }); /** * label节点样式 * @author 韦胜健 * @date 2022.9.4 23:24 */ const labelStyles = useStyles(style => { if (!form.isSingleColumn.value) { if (labelWidth.value != null && !form.props.verticalLabel) { style.width = unit(labelWidth.value); } } }); /** * content节点样式 * @author 韦胜健 * @date 2022/10/28 10:34 */ const contentStyles = useStyles(style => { if (!form.isSingleColumn.value) { if (labelWidth.value != null && !form.props.verticalLabel) { style.width = `calc(100% - ${unit(labelWidth.value)})`; } } else { style.width = unit((() => { if (props.singleColumnContentWidth) { return props.singleColumnContentWidth; } if (form.props.singleColumnContentWidth) { return form.props.singleColumnContentWidth; } if (form.props.column === 1 && !!form.props.labelWidth) { return `calc(100% - ${unit(form.props.labelWidth)})`; } return 400; })()); } }); const contentInnerStyles = useStyles(style => { if (props.negativeInnerMarginTop) {style.marginTop = `-${form.rowGutter.value}px`;} }); const refer = { colon, labelAlign, contentAlign, styles, labelStyles, contentStyles, contentInnerStyles, form, validateMessagePosition, }; provide(FORM_ITEM_LAYOUT_PROVIDER, refer); return refer; }; return { useForm, useFormItem }; })(); /** * 不同尺寸下的默认列间距 * @author 韦胜健 * @date 2024.7.18 20:52 */ const FormSizeGutter: Record = { large: 16, normal: 12, small: 8, mini: 4, }; export const FormValidateMessagePosition = createEnum([ 'bottom-left', 'bottom-right', 'right' ] as const);