Skip to content

Better support for hyphenated HTML attributes akin to data and aria #1798

Open
@rhysb27

Description

Current behavior

Currently it's possible to add data and aria attributes to inputs created with SimpleForm by supplying a keword argument (named either data or aria) with a hash. This is extremely useful for avoiding the ugly string-based keys we'd otherwise need to use for these types of HTML attrubutes. For example:

# Without this support
f.input(:foo, input_html: { "aria-controls": "options" })

# With this support
f.input(:foo, input_html: { aria: { controls: "options" } })

becomes

<input aria-controls="options"...>

However this only works for the aforementioned aria and data attribute types. Any other hyphenated attributes must be entered in strings, leading to a patchwork of symbols vs strings and skewer-case vs snake_case being passed in these calls.

Expected Desired behavior

It would be really nice if this behaviour was expanded to support all hyphenated attributes, or at the very least a configuration option was added to be able to add to the list keys that this behaviour applies to.

Without diving too deep into SimpleForm's code, there appears to be an array of strings at lib/simple_form/inputs/base.rb:200 which is responsible for distinguishing data and aria from everything else. Would making this configurable be viable?

As an example of a use case for this desired behaviour, my team makes heavy use of Alpine.js, which relies on many x-prefixed HTML attributes - often several per element. When mixing the string-key syntax required for these attributes with data and aria attributes, the input_html hashes we pass get quite unwieldy. Setting aside cosmetics, this inconsistent behaviour can also be confusing for developers new to SimpleForm.

I appreciate this is mostly an issue of cosmetics/usability rather than an outright bug or lack of support. I figured "don't ask, don't get" applies. Thank you for taking the time to read this and for building such a useful gem.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions