Uniforms: Custom AutoField fails in nested fields

Created on 3 Aug 2018  路  10Comments  路  Source: vazco/uniforms

Example:

const schema=new SimpleSchema({
    key1: String,//works
    key2: Number, //works
    key3: new SimpleSchema({
        key4: String, //fails, it used Original AutoField
    })
})

Thanks

question

All 10 comments

I found the problem,. never mind

Well, looking the code.
AutoField fails in nested fields

in NestField.js
AutoField is imported from the src.

May be a solution is pass autoField to the context.

NestField.js ->

import React          from 'react';
import connectField   from 'uniforms/connectField';
import filterDOMProps from 'uniforms/filterDOMProps';
import injectName     from 'uniforms/injectName';
import joinName       from 'uniforms/joinName';

import AutoField from './AutoField';

Hi @cesarve77. If you have a custom AutoField and want it to flow to the nested fields, create a custom NestField too - there's no such option right now and we probably won't add it.

If is suitable the solution to pass autoField to the context I can work in a pull request

Somehow that's what I meant: I don't think it's a good idea. Data passed in the context should be as small as possible. Also, in this case, either we need to introduce a new argument to connectField, explicitly use context in the components or pass it to every field - none of these three sounds good to me. And as always, I like to encourage custom themes.

OK, no promblem

Other solution can be create a configuration function for uniforms where we can pass a default AutoField to be used along of the app.

import config from 'uniforms'

config({defaultAutoField: myCustomAutoField})

and AutoFields, ListFields, ListItemField and NestField instead of import AutoField, they will get it from previous config.

Well, it won鈥檛 work on this level, more probably on the theme level, as some people (including me) are using few themes in one app, each with a different AutoField. I鈥檒l think about it and let you know.

I can't think about anything, that won't involve some weird API or a lot of custom code. I'll close it for now, but I'm open to ideas.

Some kind of configuration would indeed be helpful.

Maybe along the lines of what @cesarve77 suggested, with a bit more configurability, like making it a function that has access to the context of the form:

import config from 'uniforms';
import AutoField from 'uniforms/AutoField';
import AutoFieldCustom1 from './AutoFieldCustom1';
import AutoFieldCustom2 from './AutoFieldCustom2';

config({ getAutoField: ({ context: { theme }) => {
  if (theme === 'custom-theme-1') {  
    return AutoFieldCustom1;
  }
  if (theme === 'custom-theme-2') {  
    return AutoFieldCustom2;
  }
 return AutoField;
} 
});

const MyForm = () => <AutoForm theme="custom-theme-2" />

Right now I just want to make ListItemField use our own AutoField, which makes me copy-paste both ListField and ListItemField in my code base, edit 2 lines in each file. It's more like a fork, not really a theme. (and I also have both versions imported in my client, so bundle-size wise it's not great)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

dacioromero picture dacioromero  路  5Comments

danilomiranda picture danilomiranda  路  4Comments

thearabbit picture thearabbit  路  8Comments

radekmie picture radekmie  路  8Comments

cesarve77 picture cesarve77  路  4Comments