Replies: 3 comments 8 replies
-
|
@gemini-code-assist 这个命名如何,有没有更好的方案? |
Beta Was this translation helpful? Give feedback.
0 replies
-
|
如果只有 Form.Item 这一个应用场景的话,不如直接支持 renderProps ? const Demo = () => (
<Form>
<Form.Item name="coupon" label="Coupon Code">
{(props) => (
<Space.Compact>
<Input {...props} placeholder="Enter your coupon" />
<Button type="primary">Apply</Button>
</Space.Compact>
)}
</Form.Item>
</Form>
); |
Beta Was this translation helpful? Give feedback.
4 replies
-
|
这个很像是 Context.Provider 和 Context.Consumer。不如直接提供 TooltipContext 或者 useTooltipContext 方法? |
Beta Was this translation helpful? Give feedback.
4 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
📋 RFC: Introduce
PropsInjectorfor flexible props forwardingSummary
Introduce a new utility component,
PropsInjector, to solve the problem of forwarding props fromForm.Itemto deeply nested form controls within layout components likeSpace.CompactorFlexor any other layer wrapper. This component uses the Render Props pattern to provide a flexible and explicit way to pass props likevalueandonChangeto the desired target.Basic example
In this example,
Form.Itempassesvalue,onChange, etc., toPropsInjector. The injector then passes these props as an object to its children function, which can be explicitly spread onto the target<Input />component.Motivation
A major pain point when composing forms in Ant Design is using layout components like
Space.Compactinside aForm.Item. The props injected byForm.Item(such asvalueandonChange) are passed to the top-level child, which is the layout component (Space.Compact), not the actual form control (Input,Select, etc.) inside it.The current workarounds are inconvenient and lead to boilerplate code:
React.cloneElement: This involves complex and imperative logic that clutters the component's render method.As one developer put it, "If I use it with a Form, I have to create an HOC to pass props to the Input. This is very inconvenient."
PropsInjectorprovides a standardized, declarative, and highly flexible solution to this common problem, eliminating the need for custom wrappers and making complex form layouts clean and intuitive.Detailed Design
We will introduce a new component,
PropsInjector, which will be statically attached toForm.Item. It is designed as a generic utility that implements the Render Props pattern.API
Its API is minimal and powerful:
Implementation Details
PropsInjectorcomponent collects all props passed to it, except forchildren.childrenprop is a function.Alternatives Considered
<Injector.Target />): We considered an API like:While highly declarative, this approach is less flexible. The Render Props pattern gives the user full control to pass props to multiple children, modify them, or use them for conditional rendering, which is a significant advantage in complex scenarios.
targetKey): An API like<Injector targetKey="myInput">...was also considered. This feels "magical," is prone to stringly-typed errors, and has poor IDE support. The Render Props pattern is far more explicit and type-safe.The Render Props pattern was chosen for its superior flexibility, explicitness, and simplicity of implementation.
Drawbacks
{(props) => (...) }syntax introduces an extra layer of nesting compared to direct component composition, which might feel slightly cluttered to developers unfamiliar with the pattern.However, we believe the trade-off is well worth it for the explicitness and power it provides.
Adoption strategy
This is a new, non-breaking, and additive feature.
PropsInjectoras util component.FormandSpace.Compactwith examples demonstrating this new, recommended pattern for composing complex inputs.Beta Was this translation helpful? Give feedback.
All reactions