chore: add comment

This commit is contained in:
Zeke Zhang 2025-04-02 16:58:01 +08:00
parent 7e993b867d
commit cf36a3216b

View File

@ -102,12 +102,28 @@ const CollectionFieldInternalField = (props) => {
const dynamicProps = useDynamicComponentProps(uiSchema?.['x-use-component-props'], props);
useEffect(() => {
// There seems to be a bug in formily where after setting a field to readPretty, switching to editable,
// then back to readPretty, and refreshing the page, the field remains in editable state. The expected state is readPretty.
// This code is meant to fix this issue.
/**
* There seems to be a bug in formily where after setting a field to readPretty, switching to editable,
* then back to readPretty, and refreshing the page, the field remains in editable state. The expected state is readPretty.
* This code is meant to fix this issue.
*/
if (fieldSchema['x-read-pretty'] === true && !field.readPretty) {
field.readPretty = true;
}
/**
* This solves the issue: After creating a form and setting a field to "read-only", the field remains editable when refreshing the page and reopening the dialog.
*
* Note: This might be a bug in Formily
* When both x-disabled and x-read-pretty exist in the Schema:
* - If x-disabled appears before x-read-pretty in the Schema JSON, the disabled state becomes ineffective
* - The reason is that during field instance initialization, field.disabled is set before field.readPretty, which causes the pattern value to be changed to 'editable'
* - This issue is related to the order of JSON fields, which might return different orders in different environments (databases), thus making the issue inconsistent to reproduce
*
* Reference to Formily source code:
* 1. Setting readPretty may cause pattern to be changed to 'editable': https://github.com/alibaba/formily/blob/d4bb96c40e7918210b1bd7d57b8fadee0cfe4b26/packages/core/src/models/BaseField.ts#L208-L224
* 2. The execution order of the each method depends on the order of JSON fields: https://github.com/alibaba/formily/blob/123d536b6076196e00b4e02ee160d72480359f54/packages/json-schema/src/schema.ts#L486-L519
*/
if (fieldSchema['x-disabled'] === true) {
field.disabled = true;
}