https://github.com/bahrus/be-reformable
be-reformable is a web component that progressively enhances the built-in form element. It suggests when and what to fetch to other enhancements or components
https://github.com/bahrus/be-reformable
custom-element custom-elements form-element form-elements web-component web-components
Last synced: about 1 month ago
JSON representation
be-reformable is a web component that progressively enhances the built-in form element. It suggests when and what to fetch to other enhancements or components
- Host: GitHub
- URL: https://github.com/bahrus/be-reformable
- Owner: bahrus
- License: mit
- Created: 2021-10-16T21:22:29.000Z (over 4 years ago)
- Default Branch: baseline
- Last Pushed: 2025-11-09T17:01:22.000Z (5 months ago)
- Last Synced: 2025-11-09T18:22:48.144Z (5 months ago)
- Topics: custom-element, custom-elements, form-element, form-elements, web-component, web-components
- Language: JavaScript
- Homepage:
- Size: 288 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# be-reformable (🍺)
*be-reformable* is a custom element enhancement that (progressively) enhances the built-in form element, making attributes/properties like action dynamic. It does not do anything fetch related, leaving that for other components / enhancements. It provides the mechanics of specifying what the fetch parameters should be, and quietly suggests the appropriate time to perform said fetch.
It uses [be-enhanced](https://github.com/bahrus/be-enhanced) as the underpinning approach, as opposed to the controversial "is" extension.
[](https://github.com/bahrus/be-reformable/actions/workflows/CI.yml)
[](http://badge.fury.io/js/be-reformable)
[](https://bundlephobia.com/result?p=be-reformable)

## [Demo](https://codepen.io/bahrus/pen/eYEZOXm)
## Example 1: Making the action property dynamic
Let's see how we can use *be-reformable* to bind input elements to the action property. These input elements should not have a name attribute, as we don't want them to affect the query string. Instead we use a custom attribute starting with ":".
In this example, we bind to the [newton advanced math micro service](https://newton.vercel.app/), declaratively. By itself, this enhancement will not make the form fully functional for this service (as it doesn't do a fetch or anything).
```html
Operation:
Expression:
Submit
```
The "path" value follows the [URL Pattern syntax](https://developer.mozilla.org/en-US/docs/Web/API/URL_Pattern_API).
"base-link" is optional, but allows for easy management of common base API URL's across the application. The link tag should probably go in the head tag of index.html (typically).
What *be-reformable* does is:
1. By default, adds "input" event to the adorned form element.
2. If the form's checkValidity() is false, ignores the event.
2. When the event occurs, and checkValidity() is true, it uses the baseLink + path to set the action value of the form element.
1. It pulls in all the form associated custom elements and/or built-in input elements referenced by the path property.
2. Forms the compound string and sets the action property/attribute.
3. Triggers event "fetch-ready" which provides the recommended url and options parameters. Event is dispatched both from the form element as well as the enhancement.
## Editing JSON-in-HTML
> [!NOTE]
> A [VSCode plug-in](https://marketplace.visualstudio.com/items?itemName=andersonbruceb.json-in-html) is available to make editing json-in-html more pleasant. This extension works with the web interface of vscode.
*be-reformable* is a rather lengthy name, and if it appears frequently within an application, could get tiresome to have to type. It is the canonical name, but developers can easily define alternative names. This package provides one such alternative:
```html
...
```
## More semantic markup
This is also supported:
```html
...
```
## Support for headers and body
Hardcoded:
```html
(await import('trans-render/lib/weave.js'))
.weave({
Authorization: "sessionStorage://auth?.bearerToken",
"Content-Type": "indexedDB://db/store?.key",
"User-Agent": "globalThis://navigator?.userAgent",
Accept: "application/json"
})
.into('rPpwNLcYsUOjFcg+N8lmOA')
.andWeave({
baseURL: "globalThis://newton-microservice/href"
})
.into('qmywdO1vr0SwyuIe4fvzxQ');
submit
```
The baseURL and headers settings that are weaved in above make use of [Uniform Source Path](https://github.com/bahrus/trans-render/wiki/VIIII.--Uniform-Source-Path) syntax.
These are asynchronous and may not already be set when the rest of the form is ready for submitting. *be-reformable*, by default, won't issue the "fetch-ready" event until all the values have been retrieved (and are truthy).
To indicate that a header is optional, add a question mark at the end of the key:
```JavaScript
"Content-Type?": "indexedDB://db/store?.key",
```
> [!NOTE]
> Other components / enhancements that leverage this enhancement, and actually perform the fetch should consider use of [be-hashing-out](https://github.com/bahrus/be-hashing-out) or some other security mechanism if there's any sense of danger that justifies adding that security check.
## Support for emitting "fetch-ready" event only after a button click:
```html
Operation:
Expression:
Submit
Submit
```
## [Import Maps](https://github.com/bahrus/be-reformable/blob/baseline/imports.html)
## Viewing Locally
Any web server that serves static files with server-side includes will do but...
1. Install git.
2. Fork/clone this repo.
3. Install node.
4. Open command window to folder where you cloned this repo.
5. > npm install
6. > npm run serve
7. Open http://localhost:8000/ in a modern browser.
## Importing in ES Modules:
```JavaScript
import 'be-reformable/be-reformable.js';
```
## Using from CDN:
```html
import 'https://esm.run/be-reformable';
```