|
|
@@ -15,19 +15,20 @@
|
|
|
<input-tsx v-if="item.type === 'input'" v-model="modelRef[item.key]" />
|
|
|
<select-tsx v-else-if="item.type === 'select'" :list="item.request!()" :value="modelRef[item.key]" :on-change="(e) => onChange(e, item.key)" />
|
|
|
</a-form-item>
|
|
|
- <a-form-item>
|
|
|
+ <!-- <a-form-item>
|
|
|
<slot name="reset" > <a-button @click="resetForm">重置</a-button></slot>
|
|
|
</a-form-item>
|
|
|
<a-form-item>
|
|
|
<slot name="search" > <a-button type="primary">搜索</a-button></slot>
|
|
|
- </a-form-item>
|
|
|
+ </a-form-item> -->
|
|
|
</a-form>
|
|
|
+
|
|
|
</template>
|
|
|
|
|
|
<script lang='ts' setup >
|
|
|
-import { onMounted, reactive, toRaw, computed, ref } from 'vue'
|
|
|
+import { onMounted, reactive, toRaw } from 'vue'
|
|
|
import { Form } from 'ant-design-vue'
|
|
|
-import { InputTsx } from './components/indext'
|
|
|
+import { InputTsx, SelectTsx } from './components/indext'
|
|
|
|
|
|
/**
|
|
|
* 将submit的提交抛出去
|
|
|
@@ -45,12 +46,23 @@ export interface FormItemProps {
|
|
|
}[]
|
|
|
}
|
|
|
|
|
|
+/**
|
|
|
+* 直接将table的columns传入时进行数据转化处理
|
|
|
+ */
|
|
|
+export interface ColumnsProps {
|
|
|
+ title: string,
|
|
|
+ dataIndex: string,
|
|
|
+ key?: string,
|
|
|
+ formProps?: FormItemProps
|
|
|
+}
|
|
|
+
|
|
|
export interface FormProProps {
|
|
|
reset: boolean // 是否开启一键reset表单
|
|
|
search: boolean // 是否开启搜索功能
|
|
|
validate: boolean // 是否开启验证功能
|
|
|
layout: 'horizontal' | 'vertical' | 'inline', // 布局方式 默认横向布局
|
|
|
- formProps: FormItemProps[] // 表单props
|
|
|
+ formProps?: FormItemProps[] // 表单props
|
|
|
+ columnsProps?: ColumnsProps[] // 与table表格一起传入的时候, 便于从table表格中筛选
|
|
|
[key: string]: any
|
|
|
}
|
|
|
|
|
|
@@ -59,50 +71,64 @@ enum ComponentTypeEnum {
|
|
|
'select' = 'select'
|
|
|
}
|
|
|
|
|
|
+const componentTypes: COMMON.COMPONENTS.ComType[] = ['input', 'select', 'datepick']
|
|
|
+
|
|
|
+const getLablePrefix = (key: COMMON.COMPONENTS.ComType) => {
|
|
|
+ switch (key) {
|
|
|
+ case 'input':
|
|
|
+ return '请填写'
|
|
|
+ case 'select':
|
|
|
+ return '请选择'
|
|
|
+ default:
|
|
|
+ return '请填写'
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
const useForm = Form.useForm
|
|
|
|
|
|
const emit = defineEmits(['submit', 'reset', 'search'])
|
|
|
|
|
|
-const a = ref('')
|
|
|
-
|
|
|
-// const props = defineProps<FormProProps>()
|
|
|
-const props: FormProProps = {
|
|
|
- layout: 'horizontal',
|
|
|
- search: true,
|
|
|
- reset: true,
|
|
|
- formProps: [{
|
|
|
- rules: {
|
|
|
- required: true,
|
|
|
- message: '此项为必填项'
|
|
|
- },
|
|
|
- type: 'input',
|
|
|
- key: 'modelLabel',
|
|
|
- label: '测试title'
|
|
|
- }]
|
|
|
+const props = defineProps<FormProProps>()
|
|
|
+
|
|
|
+const initFormPro = () => {
|
|
|
+ console.log('form pro props:', props)
|
|
|
+
|
|
|
+ if (props.formProps) {
|
|
|
+ props.formProps.forEach((item: FormItemProps) => {
|
|
|
+ const key = item.key
|
|
|
+ rulesRef[key] = typeof item.rules === 'object' ? item.rules : [{ required: item.rules, message: getLablePrefix(item.type) + item.label }]
|
|
|
+ modelRef[key] = item.value || ''
|
|
|
+ formProps.push({ ...item })
|
|
|
+ })
|
|
|
+ } else {
|
|
|
+ props.columnsProps.forEach((item: ColumnsProps) => {
|
|
|
+ if (item.formProps) {
|
|
|
+ const key = item.formProps!.key ? item.formProps!.key : item.dataIndex
|
|
|
+ const rules = typeof item.formProps!.rules === 'object'
|
|
|
+ ? item.formProps!.rules
|
|
|
+ : [{ required: item.formProps!.rules, message: getLablePrefix(item.formProps.type) + item.title }]
|
|
|
+
|
|
|
+ rulesRef[key!] = rules
|
|
|
+ modelRef[item.formProps.key] = item.formProps.value || ''
|
|
|
+ formProps.push({ ...item.formProps })
|
|
|
+ }
|
|
|
+ })
|
|
|
+ }
|
|
|
+
|
|
|
+ console.log('formProps:', formProps)
|
|
|
}
|
|
|
|
|
|
const modelRef = reactive<Record<string, any>>({})
|
|
|
|
|
|
const rulesRef = reactive<Record<string, any>>({})
|
|
|
|
|
|
-const formProps = reactive<FormItemProps[]>(props.formProps)
|
|
|
-
|
|
|
-const resetForm = () => {
|
|
|
- resetFields([])
|
|
|
-}
|
|
|
+const formProps = reactive<FormItemProps[]>([])
|
|
|
|
|
|
const onChange = (record: any, key: string) => {
|
|
|
console.log('form pro item change:', record)
|
|
|
modelRef[key] = record
|
|
|
}
|
|
|
|
|
|
-props.formProps.forEach(item => {
|
|
|
- rulesRef[item.key] = [item.rules]
|
|
|
- modelRef[item.key] = item.value || ''
|
|
|
-
|
|
|
- console.log(modelRef)
|
|
|
-})
|
|
|
-
|
|
|
const { resetFields, validate, validateInfos } = useForm(modelRef, rulesRef, {
|
|
|
onValidate: (...args) => console.log(...args)
|
|
|
})
|
|
|
@@ -122,9 +148,11 @@ defineExpose({
|
|
|
onSubmit
|
|
|
})
|
|
|
|
|
|
-const reset = () => {
|
|
|
- // resetFields
|
|
|
-}
|
|
|
+onMounted(() => {
|
|
|
+ console.log('FormPro onMounted-----')
|
|
|
+
|
|
|
+ initFormPro()
|
|
|
+})
|
|
|
|
|
|
</script>
|
|
|
|