ant-design / ant-design/pro-components
🧐[问题] 自定义表单项中如何获取阅读态
- Dominant language
- TypeScript
- Stars
- 4.8k
- Forks
- 1.4k
- Avg merge
- 10h 44m
- Merged PRs (30d)
- 3
Description
### 🧐 问题描述
目前自定义一个表单项,获取不到是否是readonly,手动添加readonly到组件上,但是如何获取到value 的值来分别渲染?
在customInput.tsx中直接获取不到value和readonly,[文档中写自定义表单项遵循](https://ant-design.antgroup.com/components/form-cn#components-form-demo-customized-form-controls)
- 提供受控属性 value 或其它与 [valuePropName](https://ant.design/components/form-cn/#formitem) 的值同名的属性。
- 提供 onChange 事件或 [trigger](https://ant.design/components/form-cn/#formitem) 的值同名的事件。
就算我手动在调用``获取到readonly了,也没法获取到value,使用了Form.useForm,提示`Instance created by `useForm` is not connected to any Form element. Forget to pass `form` prop?`
### 💻 示例代码
index.tsx
```
import {
ProForm,
ProFormText,
ProFormInstance
} from "@ant-design/pro-components";
import { Switch } from "antd";
import React, { useRef, useState } from "react";
import CustomInput from "./CustomInput";
export default function () {
const [readonly, setReadonly] = useState(false);
const formRef = useRef();
function setValue() {
formRef.current?.setFieldsValue({
username: "abc",
customInput: "def"
});
}
return (
click
;
{
console.log(values);
}}>
);
}
```
customInput.tsx
```
import { Input, Form } from "antd";
import { useMemo } from "react";
export default function (props) {
const { value = "", onChange, readonly, ...rest } = props;
console.log("props:", props);
const form = Form.useForm();
const _value = useMemo(() => form[0].getFieldValue(props.name), [value]);
return readonly ? (
) : (
);
}
```
### 🚑 其他信息
Contributor guide
Research direction
Start by reading the linked customized form controls documentation and the provided index.tsx and customInput.tsx examples, focusing on how ProForm passes readonly and field values to a custom control. Reproduce the behavior with the shown setValue and readonly toggle, then document or implement a supported way for the custom input to render its read-only value.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- react, typescript
- Domain
- frontend
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 30/100