
Security News
Static vs. Runtime Reachability: Insights from Latio’s On the Record Podcast
The Latio podcast explores how static and runtime reachability help teams prioritize exploitable vulnerabilities and streamline AppSec workflows.
mui-form-hook
Advanced tools
请确保您的代码仓库中已经存在以下依赖
"peerDependencies": {
"@types/react": "^17.0.0 || ^18.0.0",
"react": "^17.0.0 || ^18.0.0",
"react-dom": "^17.0.0 || ^18.0.0",
"react-hook-form": "^7.30.0",
"@mui/icons-material": "^5.4.2",
"@mui/material": "^5.4.2",
"@mui/lab": "^5.0.0-alpha.83",
"@mui/x-data-grid": "^5.8.0"
},
# 分开下载
yarn add mui-form-hook
yarn add @types/react@18.2.0
yarn add react-dom@18.2.0
yarn add react-hook-form@7.30.0
yarn add @mui/icons-material@5.4.2
yarn add @mui/material@5.4.2
yarn add @mui/lab@5.0.0-alpha.83
yarn add @mui/x-data-grid@5.8.0
# 一次下载
yarn add mui-form-hook @types/react@18.2.0 react-dom@18.2.0 react-hook-form@7.30.0 @mui/icons-material@5.4.2 @mui/material@5.4.2 @mui/lab@5.0.0-alpha.83 @mui/x-data-grid@5.8.0
import React from 'react'
import { Button } from '@mui/material'
import { useFormComponent } from 'mui-form-hook'
/** 类型声明 */
export interface NChangeWardBedInput {
wardBedId?: string | undefined
changeTime?: Date
iHid?: string | undefined
}
/** 获取默认表单值 */
export const getDefaultFormData = () =>
({
/** 转入床位Id */
wardBedId: '',
/** 换床时间 */
changeTime: new Date(),
/** 住院信息id */
iHid: ''
} as NChangeWardBedInput)
export const Test = () => {
// 创建表单及其相关组件和api(和 useForm入参相同)
const { FormText, handleSubmit } = useFormComponent<NChangeWardBedInput>({
// 设置默认值
defaultValues: getDefaultFormData()
})
return (
<div>
<FormText
name="iHid" // 绑定表单属性
variant="outlined" // TextField 样式继承
rules={{ required: '请输入' }} // 定义验证规则
/>
<FormText
name="wardBedId"
variant="outlined"
rules={{ required: '请输入' }}
/>
<Button
// 在这里提交表单 handleSubmit和react-form-hook保持一致
// 事实上 useFormComponent完全继承了useForm的api
onClick={handleSubmit((data) => console.log(data))}
>
提交
</Button>
</div>
)
}
详细介绍了库提供的相关组件和 hook
/** Form表单组件入参基本参数
* @param TValue 组件绑定的数据类型
*/
export type IFormComponentPropsBase<TValue = any, TItem = null> = {
/** 错误状态 */
error?: boolean;
/** 组件下提示文本 */
helperText?: string | JSX.Element;
/** 组件内提示 */
label?: string | JSX.Element;
/** 表单值 */
value?: TValue;
/** 更新值 */
onChange?: IChange<TValue>;
/** 用于Select同时绑定多个Form属性
* @description 如果需要使用otherNames属性绑定多个属性, 请实现该方法
*/
onItemChange?: IChange<TItem>;
};
{
name: TName;
rules?: Omit<RegisterOptions<TFieldValues, TName>, 'valueAsNumber' | 'valueAsDate' | 'setValueAs' | 'disabled'>;
shouldUnregister?: boolean;
defaultValue?: UnpackNestedValue<FieldPathValue<TFieldValues, TName>>;
control?: Control<TFieldValues>;
}
{
/** 默认错误提示 */
defaultErrorMessage?: string | JSX.Element
/** 同时绑定多个Form属性
* @description 如果需要使用otherNames属性绑定多个属性, 请在原始组件中实现onItemChange方法
*/
otherNames?: {[itemProperty]:formName}
}
# 这使得更新会实时同步上层引用模块
pnpm dev
@mui-form/hook 提供了开发的方法让开发者可以进行二次开发,便于丰富 Form 的组件。
其他表单组件如果需集成的 useFormComponent 中,需要实现 useFormComponent 规定的参数,具体参数属性如下:
/** Form表单组件入参基本参数
* @param TValue 组件绑定的数据类型
*/
export type IFormComponentPropsBase<TValue = any, TItem = null> = {
/** 错误状态 */
error?: boolean;
/** 组件下提示文本 */
helperText?: string | JSX.Element;
/** 组件内提示 */
label?: string | JSX.Element;
/** 表单值 */
value?: TValue;
/** 更新值 */
onChange?: IChange<TValue>;
/** 用于Select同时绑定多个Form属性
* @description 如果需要使用otherNames属性绑定多个属性, 请实现该方法
*/
onItemChange?: IChange<TItem>;
};
开发流程
定义组件参数 建议用类型 IFormComponentProps处理组件入参,它规定了上述规范的参数,同时继承用户自定义的参数。
// IFormComponentProps需要三个泛型参数
// <TProps> 自定义组件参数
// <TV> value的类型
// <TI> item的类型 (下拉选择框等的单项数据类型)
export type IMyNewComponetProps<TV = any, TI = any> = IFormComponentProps<
MuiComponetProps,
TV,
TI
>;
实现组件
const MyNewComponet = ({...props}:IMyNewComponetProps)=>{
// ... 具体的实现内容
return (
<MuiComponet
{...props}
>)
}
把组件附加到 useFormComponent 中,使用withControllerFields
const { formProps } = useFormComponent({
defaultValues: getDefaultFormDat(),
});
const FormComponent = withControllerFields(MyNewComponet, { formProps });
FAQs
An ui library base on MUI for React front-end apps.
The npm package mui-form-hook receives a total of 8 weekly downloads. As such, mui-form-hook popularity was classified as not popular.
We found that mui-form-hook demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
The Latio podcast explores how static and runtime reachability help teams prioritize exploitable vulnerabilities and streamline AppSec workflows.
Security News
The latest Opengrep releases add Apex scanning, precision rule tuning, and performance gains for open source static code analysis.
Security News
npm now supports Trusted Publishing with OIDC, enabling secure package publishing directly from CI/CD workflows without relying on long-lived tokens.