formily icon indicating copy to clipboard operation
formily copied to clipboard

[Bug Report] 自定义组件无法触发阅读态

Open panghaoyuan opened this issue 3 years ago • 0 comments

[ ] I have searched the issues of this repository and believe that this is not a duplicate.

Reproduction link

Edit on CodeSandbox

复现demo https://github.com/panghaoyuan/formily-bug-demo

Steps to reproduce

git clone https://github.com/panghaoyuan/formily-bug-demo.git
pnpm i 
pnpm dev

What is expected?

自定义组件可以触发阅读态

What is actually happening?

自定义组件无法触发阅读态

Package

@formily/[email protected]


通过源码断点发现问题,但是无法解决问题,希望以下代码有参考价值 自定义组件中 useField()null

export function mapReadPretty(component, readPrettyProps) {
    return function (target) {
        return observer(function (props) {
            var field = useField(); // 自定义组件field为null
            if (!isVoidField(field) && (field === null || field === void 0 ? void 0 : field.pattern) === 'readPretty') {
                return React.createElement(component, __assign(__assign({}, readPrettyProps), props));
            }
            return React.createElement(target, props);
        }, {
            forwardRef: true,
        });
    };
}

https://github.com/alibaba/formily/blob/23b94cdb026f492d65bb5d8234e5d6e52dd22ae7/packages/react/src/shared/connect.ts#L47-L68

panghaoyuan avatar Sep 21 '22 06:09 panghaoyuan