feat(core,react-web): add slot() field factory for custom renderers#7
feat(core,react-web): add slot() field factory for custom renderers#7
Conversation
Introduces a new `slot()` field factory that allows embedding arbitrary
React components inside a DataForm without registering global renderers.
Changes:
- packages/core: add SlotFieldDefinition and slot() factory
- packages/react-web: add SlotRendererProps interface
- packages/react-web: FieldsGrid renders slot fields via slots prop
- packages/react-web: DataForm accepts slots prop and threads it down
Usage:
// schema
const fields = { status: slot().width(50), result: slot() }
// page
<DataForm schema={schema} slots={{
status: ({ value }) => <StatusBadge status={value as string} />,
result: ({ value }) => <pre>{String(value)}</pre>,
}} />
Slot fields with no matching renderer are silently omitted (null render).
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
📝 WalkthroughWalkthroughThis pull request introduces a new slot field type to the core package and integrates slot rendering support throughout the React Web component layer. It adds a SlotFieldDefinition class, updates component APIs to accept slot renderers, implements conditional slot field rendering in FieldsGrid, and extends type definitions to support the new functionality. Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~22 minutes Poem
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
packages/react-web/src/types.ts (1)
47-47:DataFormComponents.Slotis exposed but not used in the default rendering flow.The default path uses
slotslookup inFieldsGrid;components.Slotis not consumed there, which can create API confusion. Consider removing it or wiring it as a real fallback.♻️ Suggested cleanup (if you keep the current `slots`-only behavior)
export interface DataFormComponents { ActionBar?: ComponentType<ActionBarProps>; ActionButton?: ComponentType<ActionButtonProps>; FieldsGrid?: ComponentType<FieldsGridProps>; GroupWrapper?: ComponentType<GroupWrapperProps>; Loading?: ComponentType<LoadingProps>; Divider?: ComponentType<DividerProps>; - Slot?: ComponentType<SlotRendererProps>; }🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@packages/react-web/src/types.ts` at line 47, DataFormComponents exposes Slot (Slot?: ComponentType<SlotRendererProps>) but FieldsGrid never reads components.Slot and instead uses the slots lookup, creating API confusion; either remove Slot from the DataFormComponents type or wire it as a fallback by updating FieldsGrid to check components.Slot when a named slot is missing (i.e., inside FieldsGrid’s slot resolution logic use components?.Slot as the default renderer before falling back to slots map), referencing the symbols DataFormComponents.Slot, components.Slot, and FieldsGrid to locate the change.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In `@packages/react-web/src/types.ts`:
- Line 47: DataFormComponents exposes Slot (Slot?:
ComponentType<SlotRendererProps>) but FieldsGrid never reads components.Slot and
instead uses the slots lookup, creating API confusion; either remove Slot from
the DataFormComponents type or wire it as a fallback by updating FieldsGrid to
check components.Slot when a named slot is missing (i.e., inside FieldsGrid’s
slot resolution logic use components?.Slot as the default renderer before
falling back to slots map), referencing the symbols DataFormComponents.Slot,
components.Slot, and FieldsGrid to locate the change.
ℹ️ Review info
Configuration used: defaults
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (7)
packages/core/src/fields/index.tspackages/core/src/fields/slot.tspackages/core/src/index.tspackages/react-web/src/components/Form.tsxpackages/react-web/src/components/defaults/FieldsGrid.tsxpackages/react-web/src/index.tspackages/react-web/src/types.ts
Motivation
When building read-only view pages (e.g.
EntryView,JourneyView) with Ybyra, developers often need to display complex or custom UI elements (status badges, formatted lists, JSON viewers, timeline components) in specific positions within the form layout.Previously, the only options were:
registerRenderers()(pollutes the global registry, not scoped to a single page)Solution
This PR introduces a
slot()field factory that allows embedding any React component inline inside aDataFormwithout touching the global renderer registry.API
SlotRendererPropsBehaviour
slotsare silently omitted (rendernull)width,hidden,scopes,group) work normally on slot fieldsFieldsGridcomponent overrides (components.FieldsGrid) receive theslotsprop as wellChanges
@ybyra/corefields/slot.tsSlotFieldDefinition+slot()factory@ybyra/corefields/index.ts,index.tsslotandSlotFieldDefinition@ybyra/react-webtypes.tsSlotRendererPropsinterface; updatedFieldsGridPropsandDataFormComponents@ybyra/react-webcomponents/Form.tsxslotsprop and thread toFieldsGrid@ybyra/react-webcomponents/defaults/FieldsGrid.tsxslotslookup@ybyra/react-webindex.tsSlotRendererPropsSummary by CodeRabbit