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.

Suggestion: Move component identifiers to common constants

See original GitHub issue

Description

Going through v1-alpha branch code I’ve noticed that in some places (like a few, but I’m sure this will grow with time) there are ‘hardcoded’ component names/identifiers, which in my opinion don’t scale well. To improve that I suggest to move all component identifiers (values assigned as muiName) to a separate file as importable constants (eg. in src/internal/identifiers.js or constants.js).

Alternative approach would be to export them directly from components, but I feel it’s more handy to just

import { identifier1, identifier2 } from '../internal/identifiers';

rather than

import { identifier1 } from '../Component1';
import { identifier2 } from '../Component2/SubComponent';

Also muiName itself could be stored there:

import { MUI_NAME_PROP, MY_COMPONENT } from '../internal/identifiers';

// ...

MyComponent[MUI_NAME_PROP] = MY_COMPONENT;

Or event abstracted away to helpers:

import { setMuiName, MY_COMPONENT } from '../internal/identifiers';

// sets static property on MyComponent
setMuiName(MyComponent, MY_COMPONENT);
import { isMuiComponent, OTHER_COMPONENT } from '../internal/identifiers';

// isMuiComponent could be something simple as:
const isMuiComponent = (component, type) =>
  component && component.type && component.type.muiName === type;

if (isMuiComponent(component, OTHER_COMPONENT)) {
  // do magic
}

Same approach could/should be most likely used for default style sheet names (eg. MuiCard, MuiListItem etc.) as currently these keys are duplicated in component file (createStyleSheet) as well as in src/styles/muiThemeProviderFactory.js. I’d suggest something storing them in src/internal directory as jssIdentifiers.js or styleSheetIdentifiers.js, but to me it’s not as important as clearing component logic (muiNames).

References

Current code:

// material-ui/src/List/ListItem.js

// line ~138
const hasAvatar = children.some(value => {
  return value.type && value.type.muiName === 'ListItemAvatar';
});

// line ~168
if (
  children.length &&
  children[children.length - 1].type &&
  children[children.length - 1].type.muiName === 'ListItemSecondaryAction'
) {

Suggestion:

import { LIST_ITEM_AVATAR, LIST_ITEM_SECONDARY_ACTION } from '../internal/identifiers';

const hasAvatar = children.some(value => {
  return value.type && value.type.muiName === LIST_ITEM_AVATAR;
});

if (
  children.length &&
  children[children.length - 1].type &&
  children[children.length - 1].type.muiName === LIST_ITEM_SECONDARY_ACTION
) {

// or
const childrenLength = children.length;
if (childrenLength && isMuiComponent(children[childrenLength - 1], LIST_ITEM_SECONDARY_ACTION)) {

Current:

// src/IconButton/IconButton.js

if (child.type && child.type.muiName === 'Icon') {
  return cloneElement(child, {
    className: classNames(classes.icon, child.props.className),
  });
}

Suggestion:

if (isMuiComponent(child, ICON)) {
  return cloneElement(child, {
    className: classNames(classes.icon, child.props.className),
  });
}

Current:

// src/form/FormControl.js

Children.forEach(this.props.children, child => {
  if (child && child.type && child.type.muiName === 'Input' && isDirty(child.props, true)) {
    this.setState({ dirty: true });
  }
});

Suggestion:

import { isMuiComponent, INPUT } from '../internal/identifiers';

Children.forEach(this.props.children, child => {
  if (isMuiComponent(child, INPUT) && isDirty(child.props, true)) {
    this.setState({ dirty: true });
  }
});

I’d love to submit a PR, but first I wanted to make sure this is something you guys see useful as well 😃

Thanks!

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:10 (10 by maintainers)

github_iconTop GitHub Comments

2reactions
oliviertassinaricommented, Jul 11, 2017

+1 for the isMuiComponent.

0reactions
katzoocommented, Aug 2, 2017

What’s the use case for a null second parameter?

Hmm. I couldn’t think of any valid use case, I guess I slightly misunderstood your previous comment (The property is already prefixed with mui in order to avoid that issue, isn't that enough?).

Read more comments on GitHub >

github_iconTop Results From Across the Web

Where should you put constants and why?
Constants that are specific to a class should go in that class's interface. Constants that are really configuration options should be part ......
Read more >
Python Constants: Improve Your Code's Maintainability
In programming, the term constant refers to names representing values that don't change during a program's execution.
Read more >
What is the best way to implement constants in Java? [closed]
If you have access to Java 5+, use enums to define your specific constants for an application area. All parts of the application...
Read more >
Best practices for unique identifiers - Android Developers
This document provides guidance for selecting appropriate identifiers for your app based on your use case. For a general look at Android permissions, ......
Read more >
Angular Development Best Practices - Code Maze
In this article, we are going to show what we consider to be the best practices in Angular while develop the client side...
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