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.

[Feuture request] Custom control type

See original GitHub issue

Problem description

Consider the following example:

We have a component that takes an object as its argument:

function MyComponent(props) {
  const { text, ...restProps } = props.obj;
  doSomethingWithRestProps(restProps);
  
  return <div>{{ text }}</div>;
}

Let’s say that object that MyComponent takes as its argument has a lot of different properties:

class VeryComplicatedObject {

  constructor(text) {
    this.text = text;
    this.one = 1;
    this.two = 2;
    this.three = 3;
    /* even more properties */
  }
}

Now we want to create a story for MyComponent:

export default {
  title: 'MyComponent',
  component: MyComponent
}

export Default = () => <MyComponent obj={new VeryComplicatedObject('my text')} />;

Let’s now see how our generated control looks like: obraz

Although we really need to expose as control just a text property, we are exposing whole JSON representation of the object instead.

Only text property of this object affects how MyComponent looks and behaves, rest of them is used for something else, so we don’t always need to edit the whole object. It would be convenient to be able to edit just this one text property and hide everything else from controls UI.


My proposal

Create “custom” control type.

You would use it like this:

export default {
  title: 'MyComponent',
  component: MyComponent,
  argTypes: {
    obj: {
      control: {
        type: 'custom',
        subControls: {
          text: {
            type: 'text'
          }
        },
        resolve: ({text}) => new VeryComplicatedObject(text)
      }
    }
  }
}

Then it would be displayed in controls tab as: obraz


This solution would also allow to have multiple sub-controls, that resolve to a single argument:

argTypes: {
  obj: {
    control: {
      type: 'custom',
      subControls: {
        text: {
          type: 'text'
        },
        todaysDate: {
          type: 'date'
        }
      },
      resolve: ({text, todaysDate}) => new VeryComplicatedObject(text + todaysDate)
    }
  }
}

Would be displayed as: obraz

And also nesting:

argTypes: {
  obj: {
    control: {
      type: 'custom',
      subControls: {
        text: {
          type: 'text'
        },
        sum: {
          type: 'custom',
          subControls: {
            numberOne: 'number',
            numberTwo: 'number'
          },
         resolve: ({numberOne, numberTwo}) => numberOne + numberTwo
        }
      },
      resolve: ({text, sum}) => new VeryComplicatedObject(text + sum)
    }
  }
}

obraz

This should also work not only for objects, but also for any argument type:

export default {
  title: 'MyComponent',
  component: MyComponent,
  argTypes: {
    array: {
      control: {
        type: 'custom',
        subControls: {
          one: 'text',
          two: 'text',
          three: 'text'
        },
        resolve: ({one, two, three}) => [one, two, three]
      }
    }
  }
}

Type definitions:

type ControlType = 'array' | 'boolean' | 'number' | 'range' | 'object' | 'radio' | 'inline-radio' | 'check' | 'inline-check' | 'select' | 'multi-select' | 'text' | 'color' | 'date' | 'custom';

type SubControls = {[name: string]: Control};

type ResolveFn = (args: any) => any;

type Control = {type: ControlType, options?: string[], min?: number, max?: number, step?: number, subControls?: SubControls, resolve?: ResolveFn} | ControlType;

This solution is great, because how flexible it is.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

3reactions
thomasaullcommented, Sep 4, 2020

@shilman I think you closed this as a duplicate of itself 🙃

0reactions
VladimirCorescommented, Oct 22, 2020

I think having at least some kind of “update callback” from control which called before component re-render would be great.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Add a custom control to the work item form - Azure DevOps
A custom control on the work item form is another type of contribution, like group & page contribution. The main difference is that...
Read more >
OctoPod feature request: custom command strings - Plugins
OctoPrint allows you to add custom controls to the “Control” tab of its interface. Control types reach from simple buttons which trigger ...
Read more >
Custom control type - Octopus Server
I'm looking to create a custom control type, in particular an array of the same control. ... I can then also raise it...
Read more >
Feature request: configure required fields in custom event types
Please add ability to configure in each custom event type to control if fields Who and What are required to enter or not....
Read more >
Create a custom control definition - ServiceNow Docs
A custom control definition needs a custom component. For example, a slider input control uses a custom slider component. To begin, create the ......
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