Reputation: 1686
I need to enable user to show, Add, Update an object. I have to make the decision to have three components or one component for an Angular 2 project.
Option 1 : One component
Option 2 : Split into 3 components
How to handle such a situation ? which option I would better go with ? is there any 3rd Option ?
Upvotes: 2
Views: 872
Reputation: 4888
I'd just go with one component to trigger the action, and depending on how exactly you are presenting the edit UI, one, or three, components.
Button would send a "mode" property to handler, something like this:
if ( evt === 'edit' ) { do something edit-ish... }
if ( evt === 'add' ) { do something add-ish... }
if ( evt === 'delete' ) { do something delete-ish...}
And so on. The actual edit field itself, you could just toggle a few properties in the template (for instance, change the style of an input field to "editable", possibly using a directive), or if you are doing something really elaborate, then yes probably create a separate component.
Remember, the point is that another developer should be able to decipher it easily. If the "one component to rule them all" thing compromises that, then the choice is clear. Break it apart.
Upvotes: 1