Can data-* attributes be created in Dash?

@mccalluc - This isn’t support right now, but we should support it.

The “issue” is that Dash components are currently generated from a fixed set of properties, the component generation code doesn’t have any support for arbitrary patterns of properties.

To support this, we need to:

  1. Introduce a “special” propType that denotes that this component supports data-* properties. This could either be a special name, like data-*
Div.propTypes = {
     'data-*': PropTypes.string,
     ...
}

or perhaps a special propType that signifies that this is a wildcard attribute, like

Div.propTypes = {
     'data-*': CustomPropTypes.wildcard
     ...
}
  1. Update the HTML component generator with this property: https://github.com/plotly/dash-html-components/blob/6d04a70f098bb8640545467255ae382e8e17cffb/scripts/generate-components.js#L43-L55
  2. Regenerate the HTML components: with $ npm run prepublish https://github.com/plotly/dash-html-components/blob/6d04a70f098bb8640545467255ae382e8e17cffb/package.json#L22
  3. That propType will then be encoded in the metadata.json file that describes all of the properties of all of the components. For example, see the properties of Divhere: https://github.com/plotly/dash-html-components/blob/master/lib/metadata.json#L5465-L5607
  4. Then, we’ll need to update the Dash component generation code to recognize the special attribute name / special attribute type. This will happen in two places:
    • In the base component which does the validation and serialization: https://github.com/plotly/dash/blob/600f532e1230678164479b0f7853f8a9f043e83e/dash/development/base_component.py#L15-L24.
      Validation happens by checking the supplied properties against the valid properties of that component. The list of valid properties are encoded in this self._prop_names list (for example, it might be like ['id', 'children', 'style', 'className', 'src', ...]). data-* properties don’t match that pattern, so we’ll have to have some exception to the logic that’s like:
      if (k not in self._props_names and 
          all([k.startsWith(wildcard_attr) 
                   for wildcard_attr in self._valid_wildcard_attribute_names])):
          raise Exception('Unexpected attribute `{}`...'.format(k))
      
    • Then, we’ll need to make sure that _valid_wildcard_attribute_names is encoded in the dynamic component generation code (https://github.com/plotly/dash/blob/600f532e1230678164479b0f7853f8a9f043e83e/dash/development/base_component.py#L205), something like
      self._valid_wildcard_attribute_names = {list_of_valid_wildcard_attribute_prefixes}
      
      where list_of_valid_wildcard_attribute_prefixes is created by filtering the metadata.json file and looking for either the "wildcard" type that was defined above or the special "data-*" property name.
  5. Add some tests to https://github.com/plotly/dash/blob/600f532e1230678164479b0f7853f8a9f043e83e/tests/development/test_base_component.py and https://github.com/plotly/dash/blob/600f532e1230678164479b0f7853f8a9f043e83e/tests/development/test_component_loader.py

Another thing to consider is how we should deal with the fact that these properties have a hyphen in them. Since they are hyphenated, we can’t do assignments like:

html.Div(data-label='')

however, I believe we could do

html.Div(**{'data-label': ''})

we could do some _ to - replacement too, like:

html.Div(data_label='')

but would introduce some complexity into the BaseComponent. If we do html.Div(**{'data-label': ''}), then we should probably patch the __repr__ in the BaseComponent: https://github.com/plotly/dash/blob/600f532e1230678164479b0f7853f8a9f043e83e/dash/development/base_component.py#L216-L229