@@ -2696,4 +2696,173 @@ describe('uiSchema', () => {
26962696 } ) ;
26972697 } ) ;
26982698 } ) ;
2699+
2700+ describe ( 'ui:definitions' , ( ) => {
2701+ it ( 'should apply ui:definitions to a simple $ref property' , ( ) => {
2702+ const schema : RJSFSchema = {
2703+ type : 'object' ,
2704+ properties : {
2705+ address : { $ref : '#/$defs/address' } ,
2706+ } ,
2707+ $defs : {
2708+ address : {
2709+ type : 'object' ,
2710+ properties : {
2711+ street : { type : 'string' } ,
2712+ city : { type : 'string' } ,
2713+ } ,
2714+ } ,
2715+ } ,
2716+ } ;
2717+ const uiSchema : UiSchema = {
2718+ 'ui:definitions' : {
2719+ '#/$defs/address' : {
2720+ street : { 'ui:placeholder' : 'Enter street' } ,
2721+ city : { 'ui:widget' : 'textarea' } ,
2722+ } ,
2723+ } ,
2724+ } ;
2725+
2726+ const { node } = createFormComponent ( { schema, uiSchema } ) ;
2727+
2728+ expect ( node . querySelector ( "input[placeholder='Enter street']" ) ) . toBeInTheDocument ( ) ;
2729+ expect ( node . querySelectorAll ( 'textarea' ) ) . toHaveLength ( 1 ) ;
2730+ } ) ;
2731+
2732+ it ( 'should apply ui:definitions to multiple properties sharing the same $ref' , ( ) => {
2733+ const schema : RJSFSchema = {
2734+ type : 'object' ,
2735+ properties : {
2736+ home : { $ref : '#/$defs/address' } ,
2737+ work : { $ref : '#/$defs/address' } ,
2738+ } ,
2739+ $defs : {
2740+ address : {
2741+ type : 'object' ,
2742+ properties : {
2743+ street : { type : 'string' } ,
2744+ } ,
2745+ } ,
2746+ } ,
2747+ } ;
2748+ const uiSchema : UiSchema = {
2749+ 'ui:definitions' : {
2750+ '#/$defs/address' : {
2751+ street : { 'ui:placeholder' : 'Enter street' } ,
2752+ } ,
2753+ } ,
2754+ } ;
2755+
2756+ const { node } = createFormComponent ( { schema, uiSchema } ) ;
2757+
2758+ expect ( node . querySelectorAll ( "input[placeholder='Enter street']" ) ) . toHaveLength ( 2 ) ;
2759+ } ) ;
2760+
2761+ it ( 'should allow local uiSchema to override ui:definitions' , ( ) => {
2762+ const schema : RJSFSchema = {
2763+ type : 'object' ,
2764+ properties : {
2765+ address : { $ref : '#/$defs/address' } ,
2766+ } ,
2767+ $defs : {
2768+ address : {
2769+ type : 'object' ,
2770+ properties : {
2771+ street : { type : 'string' } ,
2772+ } ,
2773+ } ,
2774+ } ,
2775+ } ;
2776+ const uiSchema : UiSchema = {
2777+ 'ui:definitions' : {
2778+ '#/$defs/address' : {
2779+ street : { 'ui:placeholder' : 'Default street' } ,
2780+ } ,
2781+ } ,
2782+ address : {
2783+ street : { 'ui:placeholder' : 'Custom street' } ,
2784+ } ,
2785+ } ;
2786+
2787+ const { node } = createFormComponent ( { schema, uiSchema } ) ;
2788+
2789+ expect ( node . querySelector ( "input[placeholder='Custom street']" ) ) . toBeInTheDocument ( ) ;
2790+ expect ( node . querySelector ( "input[placeholder='Default street']" ) ) . not . toBeInTheDocument ( ) ;
2791+ } ) ;
2792+
2793+ it ( 'should apply ui:definitions to array items with $ref' , ( ) => {
2794+ const schema : RJSFSchema = {
2795+ type : 'object' ,
2796+ properties : {
2797+ people : {
2798+ type : 'array' ,
2799+ items : { $ref : '#/$defs/person' } ,
2800+ } ,
2801+ } ,
2802+ $defs : {
2803+ person : {
2804+ type : 'object' ,
2805+ properties : {
2806+ name : { type : 'string' } ,
2807+ } ,
2808+ } ,
2809+ } ,
2810+ } ;
2811+ const uiSchema : UiSchema = {
2812+ 'ui:definitions' : {
2813+ '#/$defs/person' : {
2814+ name : { 'ui:placeholder' : 'Enter name' } ,
2815+ } ,
2816+ } ,
2817+ } ;
2818+
2819+ const { node } = createFormComponent ( {
2820+ schema,
2821+ uiSchema,
2822+ formData : { people : [ { name : 'Alice' } , { name : 'Bob' } ] } ,
2823+ } ) ;
2824+
2825+ expect ( node . querySelectorAll ( "input[placeholder='Enter name']" ) ) . toHaveLength ( 2 ) ;
2826+ } ) ;
2827+
2828+ it ( 'should apply ui:definitions at 5 levels of recursive $ref depth' , ( ) => {
2829+ const schema : RJSFSchema = {
2830+ $ref : '#/$defs/node' ,
2831+ $defs : {
2832+ node : {
2833+ type : 'object' ,
2834+ properties : {
2835+ name : { type : 'string' } ,
2836+ children : {
2837+ type : 'array' ,
2838+ items : { $ref : '#/$defs/node' } ,
2839+ } ,
2840+ } ,
2841+ } ,
2842+ } ,
2843+ } ;
2844+ const uiSchema : UiSchema = {
2845+ 'ui:definitions' : {
2846+ '#/$defs/node' : {
2847+ name : { 'ui:placeholder' : 'Node name' } ,
2848+ } ,
2849+ } ,
2850+ } ;
2851+
2852+ // Build 5-level deep formData: each level has one child
2853+ const formData : GenericObjectType = {
2854+ name : 'L0' ,
2855+ children : [
2856+ {
2857+ name : 'L1' ,
2858+ children : [ { name : 'L2' , children : [ { name : 'L3' , children : [ { name : 'L4' , children : [ ] } ] } ] } ] ,
2859+ } ,
2860+ ] ,
2861+ } ;
2862+
2863+ const { node } = createFormComponent ( { schema, uiSchema, formData } ) ;
2864+
2865+ expect ( node . querySelectorAll ( "input[placeholder='Node name']" ) ) . toHaveLength ( 5 ) ;
2866+ } ) ;
2867+ } ) ;
26992868} ) ;
0 commit comments