Skip to content

[feat] Refactor how random IDs are generated #1255

@kylebuch8

Description

@kylebuch8

Description of the requested feature

There is a randomId getter in pfelement.js.
https://github.com/patternfly/patternfly-elements/blob/master/elements/pfelement/src/pfelement.js#L168

I'd like to propose we create some utility files in that would export functions like creating a random ID that we could import into other components where needed. I'm picturing something along the lines of

function randomId(prefix = "", suffix = "") {
  return `${prefix}${Math.random().toString(36).substr(2, 9)}${suffix}`;
}

export { randomId }

And then use it like this in a component like pfe-accordion.js

import { randomId } from "some-utility-file.js"
...
_id: {
  type: String,
  default: el => `${randomId(el.tag)}`,
  prefix: false
}
...

The benefit of doing it this way is that we can remove the getter from pfelement.js since it's more of a utility function and then use it however we need to in any of the other components.

Impacted component(s)

  • pfelement
  • pfe-accordion
  • pfe-primary-detail
  • pfe-content-set
  • pfe-jump-links
  • pfe-modal
  • pfe-tabs

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions