123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160 |
- <script setup lang="tsx">
- import { Form, FormSchema } from '@/components/Form'
- import { useForm } from '@/hooks/web/useForm'
- import { PropType, reactive, watch, ref, unref } from 'vue'
- import { useValidator } from '@/hooks/web/useValidator'
- import { useI18n } from '@/hooks/web/useI18n'
- import { ElTree, ElCheckboxGroup, ElCheckbox } from 'element-plus'
- import { getMenuListApi } from '@/api/menu'
- import { filter } from '@/utils/tree'
- const { t } = useI18n()
- const { required } = useValidator()
- const props = defineProps({
- currentRow: {
- type: Object as PropType<any>,
- default: () => null
- }
- })
- const treeRef = ref<typeof ElTree>()
- const formSchema = ref<FormSchema[]>([
- {
- field: 'roleName',
- label: t('role.roleName'),
- component: 'Input'
- },
- {
- field: 'role',
- label: t('role.role'),
- component: 'Input'
- },
- {
- field: 'status',
- label: t('menu.status'),
- component: 'Select',
- componentProps: {
- options: [
- {
- label: t('userDemo.disable'),
- value: 0
- },
- {
- label: t('userDemo.enable'),
- value: 1
- }
- ]
- }
- },
- {
- field: 'menu',
- label: t('role.menu'),
- colProps: {
- span: 24
- },
- formItemProps: {
- slots: {
- default: (formData: any) => {
- console.log(formData)
- return (
- <>
- <div class="flex w-full">
- <div class="flex-1">
- <ElTree
- ref={treeRef}
- show-checkbox
- node-key="id"
- highlight-current
- data={treeData.value}
- onNode-click={nodeClick}
- >
- {{
- default: (data) => {
- return <span>{data.data.meta.title}</span>
- }
- }}
- </ElTree>
- </div>
- <div class="flex-1">
- {unref(currentTreeData) && unref(currentTreeData)?.meta?.permission ? (
- <ElCheckboxGroup v-model={unref(currentTreeData).meta.currentPermission}>
- {unref(currentTreeData)?.meta?.permission.map((v: string) => {
- return <ElCheckbox label={v} />
- })}
- </ElCheckboxGroup>
- ) : null}
- </div>
- </div>
- </>
- )
- }
- }
- }
- }
- ])
- const currentTreeData = ref()
- const nodeClick = (treeData: any) => {
- currentTreeData.value = treeData
- }
- const rules = reactive({
- roleName: [required()],
- role: [required()],
- status: [required()]
- })
- const { formRegister, formMethods } = useForm()
- const { setValues, getFormData, getElFormExpose } = formMethods
- const treeData = ref([])
- const getMenuList = async () => {
- const res = await getMenuListApi()
- if (res) {
- treeData.value = res.data.list
- }
- }
- getMenuList()
- const submit = async () => {
- const elForm = await getElFormExpose()
- const valid = await elForm?.validate().catch((err) => {
- console.log(err)
- })
- if (valid) {
- const formData = await getFormData()
- const checkedKeys = [
- ...(unref(treeRef)?.getCheckedKeys() || []),
- ...(unref(treeRef)?.getHalfCheckedKeys() || [])
- ]
- const data = filter(unref(treeData), (item: any) => {
- return checkedKeys.includes(item.id)
- })
- formData.menu = data || []
- return formData
- }
- }
- watch(
- () => props.currentRow,
- (currentRow) => {
- if (!currentRow) return
- setValues(currentRow)
- },
- {
- deep: true,
- immediate: true
- }
- )
- defineExpose({
- submit
- })
- </script>
- <template>
- <Form :rules="rules" @register="formRegister" :schema="formSchema" />
- </template>
|