123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314 |
- <script setup lang="ts">
- import { Form, FormSchema } from '@/components/Form'
- import { ContentWrap } from '@/components/ContentWrap'
- import { useI18n } from '@/hooks/web/useI18n'
- import { useForm } from '@/hooks/web/useForm'
- import { reactive, unref, ref } from 'vue'
- import { ElButton, ElInput } from 'element-plus'
- import { useValidator } from '@/hooks/web/useValidator'
- import { getDictOneApi } from '@/api/common'
- const { required } = useValidator()
- const { t } = useI18n()
- const schema = reactive<FormSchema[]>([
- {
- field: 'field1',
- label: t('formDemo.input'),
- component: 'Input',
- formItemProps: {
- rules: [required()]
- }
- },
- {
- field: 'field2',
- label: t('formDemo.select'),
- component: 'Select',
- componentProps: {
- options: [
- {
- label: 'option1',
- value: '1'
- },
- {
- label: 'option2',
- value: '2'
- }
- ]
- },
- formItemProps: {
- rules: [required()]
- }
- },
- {
- field: 'field3',
- label: t('formDemo.radio'),
- component: 'RadioGroup',
- componentProps: {
- options: [
- {
- label: 'option-1',
- value: '1'
- },
- {
- label: 'option-2',
- value: '2'
- }
- ]
- }
- },
- {
- field: 'field4',
- label: t('formDemo.checkbox'),
- component: 'CheckboxGroup',
- value: [],
- componentProps: {
- options: [
- {
- label: 'option-1',
- value: '1'
- },
- {
- label: 'option-2',
- value: '2'
- },
- {
- label: 'option-3',
- value: '3'
- }
- ]
- }
- },
- {
- field: 'field5',
- component: 'DatePicker',
- label: t('formDemo.datePicker'),
- componentProps: {
- type: 'date'
- }
- },
- {
- field: 'field6',
- component: 'TimeSelect',
- label: t('formDemo.timeSelect')
- }
- ])
- const { register, methods } = useForm()
- const {
- setProps,
- delSchema,
- addSchema,
- setValues,
- setSchema,
- getComponentExpose,
- getFormItemExpose,
- getElFormExpose
- } = methods
- const changeLabelWidth = (width: number | string) => {
- setProps({
- labelWidth: width
- })
- }
- const changeSize = (size: string) => {
- setProps({
- size
- })
- }
- const changeDisabled = (bool: boolean) => {
- setProps({
- disabled: bool
- })
- }
- const changeSchema = (del: boolean) => {
- if (del) {
- delSchema('field2')
- } else if (!del && schema[1].field !== 'field2') {
- addSchema(
- {
- field: 'field2',
- label: t('formDemo.select'),
- component: 'Select',
- componentProps: {
- options: [
- {
- label: 'option1',
- value: '1'
- },
- {
- label: 'option2',
- value: '2'
- }
- ]
- }
- },
- 1
- )
- }
- }
- const setValue = async (reset: boolean) => {
- const elFormExpose = await getElFormExpose()
- if (reset) {
- elFormExpose?.resetFields()
- } else {
- setValues({
- field1: 'field1',
- field2: '2',
- field3: '2',
- field4: ['1', '3'],
- field5: '2022-01-27',
- field6: '17:00'
- })
- }
- }
- const index = ref(7)
- const setLabel = () => {
- setSchema([
- {
- field: 'field2',
- path: 'label',
- value: `${t('formDemo.select')} ${index.value}`
- },
- {
- field: 'field2',
- path: 'componentProps.options',
- value: [
- {
- label: 'option-1',
- value: '1'
- },
- {
- label: 'option-2',
- value: '2'
- },
- {
- label: 'option-3',
- value: '3'
- }
- ]
- }
- ])
- index.value++
- }
- const addItem = () => {
- const { addSchema } = methods
- if (unref(index) % 2 === 0) {
- addSchema({
- field: `field${unref(index)}`,
- label: `${t('formDemo.input')}${unref(index)}`,
- component: 'Input'
- })
- } else {
- addSchema(
- {
- field: `field${unref(index)}`,
- label: `${t('formDemo.input')}${unref(index)}`,
- component: 'Input'
- },
- unref(index)
- )
- }
- index.value++
- }
- const formValidation = async () => {
- const elFormExpose = await getElFormExpose()
- elFormExpose?.validate((isValid) => {
- console.log(isValid)
- })
- }
- const verifyReset = async () => {
- const elFormExpose = await getElFormExpose()
- elFormExpose?.resetFields()
- }
- const getDictOne = async () => {
- const res = await getDictOneApi()
- if (res) {
- const { setSchema } = methods
- setSchema([
- {
- field: 'field2',
- path: 'componentProps.options',
- value: res.data
- }
- ])
- }
- }
- const inoutFocus = async () => {
- const inputEl: ComponentRef<typeof ElInput> = await getComponentExpose('field1')
- inputEl?.focus()
- }
- const inoutValidation = async () => {
- const formItem = await getFormItemExpose('field1')
- const inputEl: ComponentRef<typeof ElInput> = await getComponentExpose('field1')
- inputEl?.focus()
- formItem?.validate('focus', (val: boolean) => {
- console.log(val)
- })
- }
- </script>
- <template>
- <ContentWrap :title="`UseForm ${t('formDemo.operate')}`" style="margin-bottom: 20px">
- <ElButton @click="changeLabelWidth(150)">{{ t('formDemo.change') }} labelWidth</ElButton>
- <ElButton @click="changeLabelWidth('auto')">{{ t('formDemo.restore') }} labelWidth</ElButton>
- <ElButton @click="changeSize('large')">{{ t('formDemo.change') }} size</ElButton>
- <ElButton @click="changeSize('default')">{{ t('formDemo.restore') }} size</ElButton>
- <ElButton @click="changeDisabled(true)">{{ t('formDemo.disabled') }}</ElButton>
- <ElButton @click="changeDisabled(false)">{{ t('formDemo.disablement') }}</ElButton>
- <ElButton @click="changeSchema(true)">
- {{ t('formDemo.delete') }} {{ t('formDemo.select') }}
- </ElButton>
- <ElButton @click="changeSchema(false)">
- {{ t('formDemo.add') }} {{ t('formDemo.select') }}
- </ElButton>
- <ElButton @click="setValue(false)">{{ t('formDemo.setValue') }}</ElButton>
- <ElButton @click="setValue(true)">{{ t('formDemo.resetValue') }}</ElButton>
- <ElButton @click="setLabel">
- {{ t('formDemo.set') }} {{ t('formDemo.select') }} label
- </ElButton>
- <ElButton @click="addItem"> {{ t('formDemo.add') }} {{ t('formDemo.subitem') }} </ElButton>
- <ElButton @click="formValidation"> {{ t('formDemo.formValidation') }} </ElButton>
- <ElButton @click="verifyReset"> {{ t('formDemo.verifyReset') }} </ElButton>
- <ElButton @click="getDictOne">
- {{ t('searchDemo.dynamicOptions') }}
- </ElButton>
- <ElButton @click="inoutFocus">
- {{ `${t('formDemo.input')} ${t('formDemo.focus')}` }}
- </ElButton>
- <ElButton @click="inoutValidation">
- {{ `${t('formDemo.input')} ${t('formDemo.formValidation')}` }}
- </ElButton>
- </ContentWrap>
- <ContentWrap :title="`UseForm ${t('formDemo.example')}`">
- <Form :schema="schema" @register="register" />
- </ContentWrap>
- </template>
- <style lang="less" scoped>
- .el-button + .el-button {
- margin-top: 10px;
- }
- </style>
|