Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
rhysb27 opened this issue Nov 30, 2022 · 1 comment
Open

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

rhysb27 opened this issue Nov 30, 2022 · 1 comment

Comments

@rhysb27
Copy link

rhysb27 commented Nov 30, 2022

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.

@borama
Copy link

borama commented Dec 2, 2023

I used to miss this too but this is not a SimpleForm feature but a Rails feature. The code that you refer to seems to deal with something else than attribute prefixing: it merges the default data / aria attributes from the configuration with the ones passed in view templates. The attribute prefixing is done by ActionView in the tag_options method. Long time ago, there actually was a Pull Request to Rails that would potentially allow to prefix more attributes like this but a simpler solution was chosen.

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

No branches or pull requests

2 participants