question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. ItĀ collects links to all the places you might be looking at while hunting down a tough bug.

And, if youā€™re still stuck at the end, weā€™re happy to hop on a call to see how we can help out.

Fill the gap for unified mode to handle complex structure like separated mode

See original GitHub issue

Hi there!

Sorry for the long issue, but itā€™s the only way I can explain what Iā€™m trying to do. šŸ˜„

Iā€™m trying to create a component that looks like this:

screenshot 2017-01-31 08 31 36

Iā€™ve chosen to use the ā€œunifiedā€ fields definition, because it seems to make more sense for large forms that have a lot of values versus keeping them separated. Hereā€™s what the definition looks like for the field.

  {
    name: 'sending_ips',
    label: 'Sending IPs',
    rules: 'required',
    value: [],
  },

Based on what Iā€™ve seen in the documentation, this is correct? If I look at the Form dev tools, it properly has captured the values as an array:

screenshot 2017-01-31 08 35 10

Iā€™ve created a component that I use to render the fields and Add/Remove them as needed. I simply pass in the field prop like so:

<InputMultiple
  field={form.$('sending_ips')}
  inlineLabel
/>

And the component itself:

const InputMultiple = ({ field }) => {
  return (
    <Box>
      <Label field={field} inlineLabel>
        {field.map((f) => {
          return f && (
            <Box className="pt-input-group" mb={1} key={f.id}>
              <Input field={f} key={f.key} showLabel={false} />
              <Button
                className="pt-minimal"
                intent={Intent.DANGER}
                onClick={f.onDel}
                iconName="cross"
              />
            </Box>
          );
        })}
        <Button iconName="plus" onClick={field.onAdd} text="Add Value" />
        <ErrorBlock field={field} />
      </Label>
    </Box>
  );
};

export default observer(InputMultiple);

Iā€™ve copied some of this code from your demo-examples (which are great, btw). The issue Iā€™m having is that:

  • {field.onAdd} does not do anything. The removal of individual fields works as expected inside of the map.
  • The validation rules are not applied to the individual fields
  • If I change the field definitions to use the ā€œseparateā€ style - and use sending_ips[], the field.onAdd works correctly.

Am I missing something, or is it not possible to achieve this with the unified field definition style? Iā€™ve read over the FAQs and Iā€™m not sure my question has been answered. I believe my issue is similar to https://github.com/foxhound87/mobx-react-form/issues/186, except Iā€™m just trying to create multiple standard input fields to render the values into.

Thanks again!

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:11 (7 by maintainers)

github_iconTop GitHub Comments

3reactions
foxhound87commented, Feb 21, 2017

HI @mwarger

Right now the big issue is that with the unified mode you cannot specify deep nested fields, for example, if I have an array of objects. But I will figure it out!

2reactions
sangallimarcocommented, Oct 6, 2017

Iā€™m using the unified props and converting it in to a separate props using something like:

class FormService {

//.....

  normalise(
    data,
    struct = { fields: [], labels: {}, extra: {}, rules: {} },
    prevName = ""
  ) {
    data.forEach(fieldDef => {
      const { name, label, extra, rules } = fieldDef;
      const key = this.getName(name, prevName);

      struct.fields.push(key);

      if (fieldDef.fields) {
        this.normalise(
          fieldDef.fields,
          struct,
          this.calculateName(name, prevName)
        );
      } else {
        struct.extra[key] = extra;
        struct.labels[key] = label;
        struct.rules[key] = rules;
      }
    });

    return struct;
  }

  getName(name, prevName) {
    return `${prevName}${name}`;
  }

  calculateName(name, prevName) {
    return `${prevName}${name}[].`;
  }

//...

}

export const formService = new FormService();

The result can be passed to the form:

const normalised = formService.normalise(unifiedProps);
// ...
const form = new Form({ ...normalised, values }, { plugins, hooks });
Read more comments on GitHub >

github_iconTop Results From Across the Web

Content Aware Patch and Move in Adobe Photoshop
Use the Move mode to place objects in different locations (most effectively when the background remains similar). Ā· Use the Extend mode to...
Read more >
An Introduction to R - The Comprehensive R Archive Network
An Introduction to R. This is an introduction to R (ā€œGNU Sā€), a language and environment for statistical computing and graphics. R is...
Read more >
Comparing Horizontal vs. Vertical Organizational Structures
Vertical organizational structure is a pyramid-like top-down management structure. These organizations have clearly defined roles with theĀ ...
Read more >
Reliability-Centered Maintenance (RCM) | WBDG
RCM Defines Failure as "Any Unsatisfactory Condition"ā€”Therefore, ... The program can be based on rigorous Failure Modes and Effects AnalysisĀ ...
Read more >
unified approach for relationships among Green's function, normal ...
A unified approach for relationships among Green's function, normal modes and dispersion spectrum in layered elastic half-space, with correctedĀ ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found