@@ -5222,4 +5222,256 @@ describe('Form omitExtraData and liveOmit', () => {
52225222 expect ( testInput ) . eql ( null ) ;
52235223 } ) ;
52245224 } ) ;
5225+
5226+ describe ( 'nameGenerator' , ( ) => {
5227+ const { bracketNameGenerator, dotNotationNameGenerator } = require ( '@rjsf/utils' ) ;
5228+
5229+ it ( 'should generate bracket notation names for simple fields' , ( ) => {
5230+ const schema = {
5231+ type : 'object' ,
5232+ properties : {
5233+ firstName : { type : 'string' } ,
5234+ lastName : { type : 'string' } ,
5235+ } ,
5236+ } ;
5237+ const { node } = createFormComponent ( { schema, nameGenerator : bracketNameGenerator } ) ;
5238+
5239+ const firstNameInput = node . querySelector ( '#root_firstName' ) ;
5240+ const lastNameInput = node . querySelector ( '#root_lastName' ) ;
5241+
5242+ expect ( firstNameInput . getAttribute ( 'name' ) ) . eql ( 'root[firstName]' ) ;
5243+ expect ( lastNameInput . getAttribute ( 'name' ) ) . eql ( 'root[lastName]' ) ;
5244+ } ) ;
5245+
5246+ it ( 'should generate dot notation names for simple fields' , ( ) => {
5247+ const schema = {
5248+ type : 'object' ,
5249+ properties : {
5250+ firstName : { type : 'string' } ,
5251+ lastName : { type : 'string' } ,
5252+ } ,
5253+ } ;
5254+ const { node } = createFormComponent ( { schema, nameGenerator : dotNotationNameGenerator } ) ;
5255+
5256+ const firstNameInput = node . querySelector ( '#root_firstName' ) ;
5257+ const lastNameInput = node . querySelector ( '#root_lastName' ) ;
5258+
5259+ expect ( firstNameInput . getAttribute ( 'name' ) ) . eql ( 'root.firstName' ) ;
5260+ expect ( lastNameInput . getAttribute ( 'name' ) ) . eql ( 'root.lastName' ) ;
5261+ } ) ;
5262+
5263+ it ( 'should generate bracket notation names for nested objects' , ( ) => {
5264+ const schema = {
5265+ type : 'object' ,
5266+ properties : {
5267+ person : {
5268+ type : 'object' ,
5269+ properties : {
5270+ firstName : { type : 'string' } ,
5271+ address : {
5272+ type : 'object' ,
5273+ properties : {
5274+ street : { type : 'string' } ,
5275+ city : { type : 'string' } ,
5276+ } ,
5277+ } ,
5278+ } ,
5279+ } ,
5280+ } ,
5281+ } ;
5282+ const { node } = createFormComponent ( { schema, nameGenerator : bracketNameGenerator } ) ;
5283+
5284+ const firstNameInput = node . querySelector ( '#root_person_firstName' ) ;
5285+ const streetInput = node . querySelector ( '#root_person_address_street' ) ;
5286+ const cityInput = node . querySelector ( '#root_person_address_city' ) ;
5287+
5288+ expect ( firstNameInput . getAttribute ( 'name' ) ) . eql ( 'root[person][firstName]' ) ;
5289+ expect ( streetInput . getAttribute ( 'name' ) ) . eql ( 'root[person][address][street]' ) ;
5290+ expect ( cityInput . getAttribute ( 'name' ) ) . eql ( 'root[person][address][city]' ) ;
5291+ } ) ;
5292+
5293+ it ( 'should generate bracket notation names for array items' , ( ) => {
5294+ const schema = {
5295+ type : 'object' ,
5296+ properties : {
5297+ tags : {
5298+ type : 'array' ,
5299+ items : { type : 'string' } ,
5300+ } ,
5301+ } ,
5302+ } ;
5303+ const formData = {
5304+ tags : [ 'foo' , 'bar' ] ,
5305+ } ;
5306+ const { node } = createFormComponent ( { schema, formData, nameGenerator : bracketNameGenerator } ) ;
5307+
5308+ const firstTagInput = node . querySelector ( '#root_tags_0' ) ;
5309+ const secondTagInput = node . querySelector ( '#root_tags_1' ) ;
5310+
5311+ expect ( firstTagInput . getAttribute ( 'name' ) ) . eql ( 'root[tags][0]' ) ;
5312+ expect ( secondTagInput . getAttribute ( 'name' ) ) . eql ( 'root[tags][1]' ) ;
5313+ } ) ;
5314+
5315+ it ( 'should generate bracket notation names for array of objects' , ( ) => {
5316+ const schema = {
5317+ type : 'object' ,
5318+ properties : {
5319+ tasks : {
5320+ type : 'array' ,
5321+ items : {
5322+ type : 'object' ,
5323+ properties : {
5324+ title : { type : 'string' } ,
5325+ done : { type : 'boolean' } ,
5326+ } ,
5327+ } ,
5328+ } ,
5329+ } ,
5330+ } ;
5331+ const formData = {
5332+ tasks : [
5333+ { title : 'Task 1' , done : false } ,
5334+ { title : 'Task 2' , done : true } ,
5335+ ] ,
5336+ } ;
5337+ const { node } = createFormComponent ( { schema, formData, nameGenerator : bracketNameGenerator } ) ;
5338+
5339+ const firstTaskTitleInput = node . querySelector ( '#root_tasks_0_title' ) ;
5340+ const firstTaskDoneInput = node . querySelector ( '#root_tasks_0_done' ) ;
5341+ const secondTaskTitleInput = node . querySelector ( '#root_tasks_1_title' ) ;
5342+ const secondTaskDoneInput = node . querySelector ( '#root_tasks_1_done' ) ;
5343+
5344+ expect ( firstTaskTitleInput . getAttribute ( 'name' ) ) . eql ( 'root[tasks][0][title]' ) ;
5345+ expect ( firstTaskDoneInput . getAttribute ( 'name' ) ) . eql ( 'root[tasks][0][done]' ) ;
5346+ expect ( secondTaskTitleInput . getAttribute ( 'name' ) ) . eql ( 'root[tasks][1][title]' ) ;
5347+ expect ( secondTaskDoneInput . getAttribute ( 'name' ) ) . eql ( 'root[tasks][1][done]' ) ;
5348+ } ) ;
5349+
5350+ it ( 'should generate bracket notation names for select widgets' , ( ) => {
5351+ const schema = {
5352+ type : 'object' ,
5353+ properties : {
5354+ color : {
5355+ type : 'string' ,
5356+ enum : [ 'red' , 'green' , 'blue' ] ,
5357+ } ,
5358+ } ,
5359+ } ;
5360+ const { node } = createFormComponent ( { schema, nameGenerator : bracketNameGenerator } ) ;
5361+
5362+ const selectInput = node . querySelector ( '#root_color' ) ;
5363+ expect ( selectInput . getAttribute ( 'name' ) ) . eql ( 'root[color]' ) ;
5364+ } ) ;
5365+
5366+ it ( 'should generate bracket notation names for radio widgets' , ( ) => {
5367+ const schema = {
5368+ type : 'object' ,
5369+ properties : {
5370+ option : {
5371+ type : 'string' ,
5372+ enum : [ 'foo' , 'bar' ] ,
5373+ } ,
5374+ } ,
5375+ } ;
5376+ const uiSchema = {
5377+ option : {
5378+ 'ui:widget' : 'radio' ,
5379+ } ,
5380+ } ;
5381+ const { node } = createFormComponent ( { schema, uiSchema, nameGenerator : bracketNameGenerator } ) ;
5382+
5383+ const radioInputs = node . querySelectorAll ( 'input[type="radio"]' ) ;
5384+ expect ( radioInputs [ 0 ] . getAttribute ( 'name' ) ) . eql ( 'root[option]' ) ;
5385+ expect ( radioInputs [ 1 ] . getAttribute ( 'name' ) ) . eql ( 'root[option]' ) ;
5386+ } ) ;
5387+
5388+ it ( 'should generate bracket notation names for checkboxes widgets' , ( ) => {
5389+ const schema = {
5390+ type : 'object' ,
5391+ properties : {
5392+ choices : {
5393+ type : 'array' ,
5394+ items : {
5395+ type : 'string' ,
5396+ enum : [ 'foo' , 'bar' , 'baz' ] ,
5397+ } ,
5398+ uniqueItems : true ,
5399+ } ,
5400+ } ,
5401+ } ;
5402+ const uiSchema = {
5403+ choices : {
5404+ 'ui:widget' : 'checkboxes' ,
5405+ } ,
5406+ } ;
5407+ const { node } = createFormComponent ( { schema, uiSchema, nameGenerator : bracketNameGenerator } ) ;
5408+
5409+ const checkboxInputs = node . querySelectorAll ( 'input[type="checkbox"]' ) ;
5410+ // Checkboxes for multi-value fields have [] appended to indicate multiple values
5411+ expect ( checkboxInputs [ 0 ] . getAttribute ( 'name' ) ) . eql ( 'root[choices][]' ) ;
5412+ expect ( checkboxInputs [ 1 ] . getAttribute ( 'name' ) ) . eql ( 'root[choices][]' ) ;
5413+ expect ( checkboxInputs [ 2 ] . getAttribute ( 'name' ) ) . eql ( 'root[choices][]' ) ;
5414+ } ) ;
5415+
5416+ it ( 'should generate bracket notation names for textarea widgets' , ( ) => {
5417+ const schema = {
5418+ type : 'object' ,
5419+ properties : {
5420+ description : { type : 'string' } ,
5421+ } ,
5422+ } ;
5423+ const uiSchema = {
5424+ description : {
5425+ 'ui:widget' : 'textarea' ,
5426+ } ,
5427+ } ;
5428+ const { node } = createFormComponent ( { schema, uiSchema, nameGenerator : bracketNameGenerator } ) ;
5429+
5430+ const textareaInput = node . querySelector ( '#root_description' ) ;
5431+ expect ( textareaInput . getAttribute ( 'name' ) ) . eql ( 'root[description]' ) ;
5432+ } ) ;
5433+
5434+ it ( 'should use default id if nameGenerator is not provided' , ( ) => {
5435+ const schema = {
5436+ type : 'object' ,
5437+ properties : {
5438+ firstName : { type : 'string' } ,
5439+ } ,
5440+ } ;
5441+ const { node } = createFormComponent ( { schema } ) ;
5442+
5443+ const firstNameInput = node . querySelector ( '#root_firstName' ) ;
5444+ expect ( firstNameInput . getAttribute ( 'name' ) ) . eql ( 'root_firstName' ) ;
5445+ } ) ;
5446+
5447+ it ( 'should handle nameGenerator with number fields' , ( ) => {
5448+ const schema = {
5449+ type : 'object' ,
5450+ properties : {
5451+ age : { type : 'number' } ,
5452+ count : { type : 'integer' } ,
5453+ } ,
5454+ } ;
5455+ const { node } = createFormComponent ( { schema, nameGenerator : bracketNameGenerator } ) ;
5456+
5457+ const ageInput = node . querySelector ( '#root_age' ) ;
5458+ const countInput = node . querySelector ( '#root_count' ) ;
5459+
5460+ expect ( ageInput . getAttribute ( 'name' ) ) . eql ( 'root[age]' ) ;
5461+ expect ( countInput . getAttribute ( 'name' ) ) . eql ( 'root[count]' ) ;
5462+ } ) ;
5463+
5464+ it ( 'should handle nameGenerator with boolean fields' , ( ) => {
5465+ const schema = {
5466+ type : 'object' ,
5467+ properties : {
5468+ active : { type : 'boolean' } ,
5469+ } ,
5470+ } ;
5471+ const { node } = createFormComponent ( { schema, nameGenerator : bracketNameGenerator } ) ;
5472+
5473+ const activeInput = node . querySelector ( '#root_active' ) ;
5474+ expect ( activeInput . getAttribute ( 'name' ) ) . eql ( 'root[active]' ) ;
5475+ } ) ;
5476+ } ) ;
52255477} ) ;
0 commit comments