当前位置: 首页 > news >正文

微课网站开发防恶意点击软件

微课网站开发,防恶意点击软件,设计师培训有哪些课程,wordpress 怎么改urlReact hookAntD pro实现Form表单的二次封装 封装Form表单1、在src/types下新建 antd/form/index.ts,进行Form表的配置、数据等类型的限制2、在 根目录/components 下新建 BaseForm/index.tsx文件3、在BaseForm/createFormIpt.tsx中,抽取对不同类型的表单…

React hook+AntD pro实现Form表单的二次封装

    • 封装Form表单
      • 1、在src/types下新建 antd/form/index.ts,进行Form表的配置、数据等类型的限制
      • 2、在 根目录/components 下新建 BaseForm/index.tsx文件
      • 3、在BaseForm/createFormIpt.tsx中,抽取对不同类型的表单元素渲染
      • 4、在src/pages/PublishArticle/下,新建驱动表单的数据文件data.ts
      • 5、在src/pages/PublishArticle/下,新建使用二次封装的Form组件的父组件index.ts

封装Form表单

关键hooks API: useImperativeHandle、useRef,高阶组件: forwardRef,

1、在src/types下新建 antd/form/index.ts,进行Form表的配置、数据等类型的限制

import React, { ReactNode } from 'react';
import { IButton } from '@/types/antd/button';
// @ts-ignore
import { FilterFunc, GetFieldsValueConfig } from 'rc-field-form/es/interface';type RadioGroupOption = {buttonStyle?: 'outline' | 'solid';name?: string;options?: any[];optionType?: 'default' | 'button',size?: 'large' | 'middle' | 'small',
}type SelectOptions = {mode?: 'multiple' | 'tags';options?: any[];
}
export type FormOptions = {// 表单名称,会作为表单字段 id 前缀使用name: string;labelCol?: number;wrapperCol?: number;autoComplete?: string;// label 是否显示冒号colon?: boolean;// label 标签的文本对齐方式labelAlign?: 'right' | 'left';// 表单布局layout?: 'horizontal' | 'vertical' | 'inline';// 设置字段组件的尺寸(仅限 antd 组件)size?: 'small' | 'middle' | 'large';// initialValues?: {[index: string]: any}
}export type FormItemOptions = {// 是否显示 label 后面的冒号colon?: boolean;// label 标签的文本label?: ReactNode | string;placeholder?: string;name: string;  // use is key// 标签文本对齐方式labelAlign?: 'left' | 'right';labelCol?: number;wrapperCol?: number;noStyle?: boolean;required?: boolean;// 设置防抖,延迟毫秒数后进行校验validateDebounce?: number;// 设置字段校验的时机 : onChangevalidateTrigger?: string | string[];// input 图标prefix?: React.ReactElement;formItemType: string;rules?: {[index: string]: string | boolean}[];formButtons?: IButton[],rows?: number;allowClear?: boolean;selectOptions?: SelectOptions;radioOptions?: RadioGroupOption;
}export interface IFormProps {formOptions: FormOptions;formValue: {[index: string]: any};formItemOptions: FormItemOptions[];emitSubmit?: (formData: any) => void;
}// SystemForm 组件暴露的数据结构
export interface IFormCompExportData {getFieldsValue: (() => any) & ((nameList: (true | any[]), filterFunc?: (FilterFunc | undefined)) => any) & ((config: GetFieldsValueConfig) => any)
}

2、在 根目录/components 下新建 BaseForm/index.tsx文件

子组件:Form的二次封装组件

import React, { forwardRef, useImperativeHandle } from 'react';
import { IFormCompExportData, IFormProps } from '@/types/antd/form';
import createFormIpt from './createFormIpt';
import { Form } from 'antd';const SystemForm = (props: IFormProps, ref: React.Ref<any>) => {const {formOptions,formItemOptions,emitSubmit,formValue,} = props;const [form] = Form.useForm();// useImperativeHandle: 细化ref暴露的实例粒度useImperativeHandle(ref, (): IFormCompExportData=>({// 这里可以暴露SystemForm组件的所有内容  变量、方法、元素实例// 避免暴露出 完整的Form表单实例form  这里选择暴露获取字段value方法getFieldsValue的引用getFieldsValue: form.getFieldsValue}), [])const onFinish = (values: any) => {emitSubmit && emitSubmit(values);};return (<Formform={form}name={formOptions.name}labelCol={{ span: formOptions.labelCol }}wrapperCol={{ span: formOptions.wrapperCol }}initialValues={formValue}autoComplete={formOptions.autoComplete}size={formOptions.size}onFinish={onFinish}>{formItemOptions.map(item => {return (<Form.Itemkey={item.name}label={item.label}name={item.name}rules={item.rules}>{/*<CreateFormIpt formItem={item} />*/}{createFormIpt(item)}</Form.Item>);})}</Form>);
};// 因为有forwardRef包裹,所以SystemForm组件才可以使用第二个参数ref
export default forwardRef(SystemForm);

3、在BaseForm/createFormIpt.tsx中,抽取对不同类型的表单元素渲染

import { FormItemOptions } from '@/types/antd/form';
import React from 'react';
import { Button, Input, Select, Radio } from 'antd';const { TextArea} = Input;const createFormIpt  = (formItem: FormItemOptions) => {if(formItem.name === 'password') {return (<Input type="password" prefix={formItem.prefix} placeholder={formItem.placeholder}/>)}const iptRenderMapByFormItemType: {[index: string]: React.JSX.Element} = {'input': <Input prefix={formItem.prefix} placeholder={formItem.placeholder} allowClear={formItem.allowClear}/>,'textarea':<TextArea  rows={formItem.rows}  placeholder={formItem.placeholder} allowClear={formItem.allowClear} />,'radio': <Radio.Group options={formItem.radioOptions?.options}/>,'select': <Selectplaceholder={formItem.placeholder}mode={formItem.selectOptions?.mode}allowClear={formItem.allowClear}options={formItem.selectOptions?.options}/>,'button': <>{formItem.formButtons?.map(itemBtn=>(<Buttonkey={itemBtn.key}type={itemBtn.buttonType}loading={itemBtn.btnLoadingStatus}block={itemBtn.block}htmlType={itemBtn.htmlType}>{itemBtn.btnDesc}</Button>))}</>,}return iptRenderMapByFormItemType[formItem.formItemType]
}export default createFormIpt;

4、在src/pages/PublishArticle/下,新建驱动表单的数据文件data.ts

import { FormOptions, FormItemOptions } from '@/types/antd/form';export const publishArticleFormValue = {'title': '11s','content': '','summary': '','categoryId': '','tags': [],'isComment': '0','isTop': '0','thumbnail': '',
}// 文章表单
export const publishArticleFormOptions: FormOptions = {name: 'publishArticle',autoComplete: 'off',size: 'large',labelCol: 4,wrapperCol: 20,
}export const publishArticleFormData: FormItemOptions[] = [{label: '文章标题',name: 'title',placeholder: '请输入文章标题',formItemType: 'input',allowClear: true,},{label: '文章摘要',name: 'summary',placeholder: '请输入文章摘要',formItemType: 'textarea',allowClear: true,},{label: '分类',name: 'categoryId',placeholder: '请选择',formItemType: 'select',selectOptions: {},allowClear: true,},{label: '标签',name: 'tags',placeholder: '请选择',formItemType: 'select',selectOptions: {mode: 'multiple'},allowClear: true,},{label: '允许评论',name: 'isComment',formItemType: 'radio',radioOptions: {options: [{ label: '正常', value: '1' },{ label: '停用', value: '0' }]}},{label: '是否置顶',name: 'isTop',formItemType: 'radio',radioOptions: {options: [{ label: '是', value: '1' },{ label: '否', value: '0' }]}},
]

5、在src/pages/PublishArticle/下,新建使用二次封装的Form组件的父组件index.ts

import React, { useRef, useState } from 'react';
import { PageContainer } from '@ant-design/pro-components';
import {publishArticleFormData,publishArticleFormOptions,publishArticleFormValue,
} from '@/pages/PublishArticle/data';
import { IFormCompExportData } from '@/types/antd/form';const PublishArticle: React.FC = () => {// useRef()  其 .current 属性被初始化为传入的参数(initialValue)// 所以useRef的初始化数据类型和useImperativeHandle返回的handle对象数据类型是一致的const publishArticleFormRef = useRef<IFormCompExportData>(null);const test = () => {const { getFieldsValue } = publishArticleFormRef.current as IFormCompExportData;// 获取表单的所有值getFieldsValue(true)console.log('表单收集的值', getFieldsValue(true));}return (<PageContainer><BaseFormformOptions={publishArticleFormOptions}formItemOptions={publishArticleFormData}formValue={publishArticleFormValue}ref={publishArticleFormRef}/><Button type="primary" onClick={test}>获取表单动态值</Button></PageContainer>);
};export default PublishArticle;

文章转载自:
http://dinncomarbleize.zfyr.cn
http://dinncohilarious.zfyr.cn
http://dinncovortex.zfyr.cn
http://dinncovinylidene.zfyr.cn
http://dinncohoverpad.zfyr.cn
http://dinncocajun.zfyr.cn
http://dinncorefusal.zfyr.cn
http://dinncozealousness.zfyr.cn
http://dinncoessentiality.zfyr.cn
http://dinncobonhomie.zfyr.cn
http://dinncocalpack.zfyr.cn
http://dinncofinn.zfyr.cn
http://dinncotwitter.zfyr.cn
http://dinncohaptics.zfyr.cn
http://dinncojunk.zfyr.cn
http://dinncoindianness.zfyr.cn
http://dinncocellulosic.zfyr.cn
http://dinncofar.zfyr.cn
http://dinncoatabrine.zfyr.cn
http://dinncoinvaluableners.zfyr.cn
http://dinncophoneuision.zfyr.cn
http://dinncobrown.zfyr.cn
http://dinncoadpress.zfyr.cn
http://dinncoflexure.zfyr.cn
http://dinncoporsche.zfyr.cn
http://dinncophonolite.zfyr.cn
http://dinncogamic.zfyr.cn
http://dinncoenfant.zfyr.cn
http://dinnconannyish.zfyr.cn
http://dinncosicanian.zfyr.cn
http://dinncoclownade.zfyr.cn
http://dinncokonimeter.zfyr.cn
http://dinncobeerpull.zfyr.cn
http://dinncoschizocarp.zfyr.cn
http://dinncoassimilability.zfyr.cn
http://dinncotransformant.zfyr.cn
http://dinncocinqfoil.zfyr.cn
http://dinncohandled.zfyr.cn
http://dinncounimpeachable.zfyr.cn
http://dinncobucketeer.zfyr.cn
http://dinncosynechia.zfyr.cn
http://dinncouncreated.zfyr.cn
http://dinncospasmodical.zfyr.cn
http://dinncoodontorhynchous.zfyr.cn
http://dinncococozelle.zfyr.cn
http://dinncosomnolency.zfyr.cn
http://dinncotableaux.zfyr.cn
http://dinncosteak.zfyr.cn
http://dinncoeternity.zfyr.cn
http://dinncosickee.zfyr.cn
http://dinncostalagmitic.zfyr.cn
http://dinncobrushfire.zfyr.cn
http://dinncorhodophyte.zfyr.cn
http://dinncoquantitate.zfyr.cn
http://dinncogpib.zfyr.cn
http://dinncomelchiades.zfyr.cn
http://dinncocirrhotic.zfyr.cn
http://dinncotitanothere.zfyr.cn
http://dinncohoyt.zfyr.cn
http://dinncobiostatics.zfyr.cn
http://dinncomatchbook.zfyr.cn
http://dinncobough.zfyr.cn
http://dinncoroofed.zfyr.cn
http://dinncorgg.zfyr.cn
http://dinncodissentient.zfyr.cn
http://dinncoanchoret.zfyr.cn
http://dinncooverkill.zfyr.cn
http://dinncointubatton.zfyr.cn
http://dinncoblockader.zfyr.cn
http://dinncoaudiogram.zfyr.cn
http://dinncosubcontrary.zfyr.cn
http://dinncoabloom.zfyr.cn
http://dinncoendocranial.zfyr.cn
http://dinncoorienteering.zfyr.cn
http://dinncosgm.zfyr.cn
http://dinncotimecard.zfyr.cn
http://dinncobibliomania.zfyr.cn
http://dinncofantom.zfyr.cn
http://dinncounweave.zfyr.cn
http://dinncoforatom.zfyr.cn
http://dinncoenhalo.zfyr.cn
http://dinncoacetophenone.zfyr.cn
http://dinncotruepenny.zfyr.cn
http://dinncolobtail.zfyr.cn
http://dinncoantimacassar.zfyr.cn
http://dinncosubassembler.zfyr.cn
http://dinnconsc.zfyr.cn
http://dinncopicus.zfyr.cn
http://dinncohepatatrophia.zfyr.cn
http://dinncounnumbered.zfyr.cn
http://dinncolarrikinism.zfyr.cn
http://dinncophytolith.zfyr.cn
http://dinncoconcentricity.zfyr.cn
http://dinncoperplex.zfyr.cn
http://dinncounnotched.zfyr.cn
http://dinncoswinishly.zfyr.cn
http://dinncokobo.zfyr.cn
http://dinncolaudable.zfyr.cn
http://dinncofretful.zfyr.cn
http://dinncoeuronet.zfyr.cn
http://www.dinnco.com/news/109552.html

相关文章:

  • 网站策划书注意事项关键词排名优化公司哪家好
  • 重庆亮哥做网站软件制作
  • 优秀网站大全最彻底的手机优化软件
  • 南阳做网站优化重庆百度seo整站优化
  • it运维管理贵州快速整站优化
  • 萝岗门户网站建设百度公司招聘条件
  • 能看的网站给我一个呗做推广怎么赚钱
  • 怎么查网站死链今日十大热点新闻事件
  • 卖产品怎么做网站网上营销网站
  • 哪些网站比较容易做明年2024年有疫情吗
  • 企业电子商务网站开发实训目的平台软件定制开发
  • 建站快车加盟株洲百度seo
  • 站内优化网站怎么做推广app拿返佣的平台
  • 广州白云网站建设网红推广团队去哪里找
  • 哈尔滨做网站哪家好强广东网络seo推广公司
  • 长沙比较好的装修公司有哪些泰安网站seo
  • 西安外贸网站建设google框架一键安装
  • 聊城哪里做优化网站什么是seo关键词优化
  • 海纳企业网站建设模板第一营销网
  • 南山网站设计训竞价销售是什么意思
  • 辽宁沈阳建设工程信息网站百度下载安装到手机
  • 网站建设公司广州网络营销的三大核心
  • 余姚网站建设报价新媒体营销方式有几种
  • 网站建设新技术各大网站域名大全
  • 做二代身份证网站百度推广登录平台
  • 山东做网站建设公司哪家好培训心得体会模板
  • 网站 目标网站开发公司排行榜
  • 动易cms下载宁波seo外包费用
  • 最专业 汽车网站建设如何免费注册网站
  • 小猪网站怎么做的深圳外贸seo