Currently, HTML provides no mechanism to show & hide dependent fields. Sometimes you only want a field to show when certain other fields have a (particular) value. The form-show-if web component enables that.
- Ships with bundled
.d.tsdefinitions so editors and TypeScript builds fully understandFormShowIfElement. - The package export map exposes the types automatically; no extra configuration is required to consume them in TS projects.
HTMLElementTagNameMapis augmented soform-show-ifelements are correctly typed when using JSX/TSX or querying viadocument.querySelector.
npm install @aarongustafson/form-show-ifImport the class and define the custom element with your preferred tag name:
import { FormShowIfElement } from '@aarongustafson/form-show-if';
// Define with default name
customElements.define('form-show-if', FormShowIfElement);
// Or define with a custom name
customElements.define('my-conditional-field', FormShowIfElement);Use the guarded definition helper to register the element when customElements is available:
import '@aarongustafson/form-show-if/define.js';If you prefer to control when the element is registered, call the helper directly:
import { defineFormShowIf } from '@aarongustafson/form-show-if/define.js';
defineFormShowIf();You can also include the guarded script from HTML:
<script src="./node_modules/@aarongustafson/form-show-if/define.js" type="module"></script>You can also use the component directly from a CDN:
<script src="https://unpkg.com/@aarongustafson/form-show-if@latest/define.js" type="module"></script>conditions
A double pipe (||) separated list ofname/valuepairs. When fields with the providednamevalues are updated, the value of those fields will be compared against the values you provided (or * for anything). If any resolve to true, the field will be shown, otherwise it will be hidden.disabled-class(optional)
By default the field &labelwrapper will be markedhiddenwhen disabled, but you can apply a customclassto be used instead, when the field is disabled.enabled-class(optional)
By default the field &labelwrapper will not behiddenwhen enabled, but you can apply a customclassto the wrapper, when the field is enabled.
This web component assumes the fields you reference in conditions exist in the DOM when the component is loaded. If they don't, they will be ignored.
The "wrapper" mentioned below refers to the nearest mutual parent of the field & its label. It may be the form-show-if element itself.
-
Field markup changes. When the field is hidden (conditions not met), it will receive the
disabledattribute and all fields within the wrapper are disabled. -
Visual state management. If
disabled-classand/orenabled-classare not defined:- When no condition is met: The wrapper has a
hiddenattribute - When any condition is met: The
hiddenattribute is removed from the wrapper
- When no condition is met: The wrapper has a
-
Custom class management. If
disabled-classand/orenabled-classare defined:- When no condition is met:
- If
enabled-classis defined, it is removed from the wrapper - If
disabled-classis defined, it is added to the wrapper
- If
- When any condition is met:
- If
enabled-classis defined, it is added to the wrapper - If
disabled-classis defined, it is removed from the wrapper
- If
- When no condition is met:
<form>
<label for="email">Email</label>
<input type="email" id="email" name="email">
<form-show-if conditions="email=*">
<label for="phone">Phone (shown if email provided)</label>
<input type="tel" id="phone" name="phone">
</form-show-if>
<button type="submit">Submit</button>
</form><form-show-if conditions="email=*||phone=*">
<label for="name">Name (shown if email OR phone provided)</label>
<input type="text" id="name" name="name">
</form-show-if><form-show-if conditions="contact-method=email">
<label for="email">Email Address</label>
<input type="email" id="email" name="email">
</form-show-if><form-show-if conditions="newsletter=yes">
<label for="email">Email (shown for newsletter)</label>
<input type="email" id="email" name="email">
</form-show-if><form-show-if conditions="email=*" disabled-class="visually-hidden" enabled-class="is-visible">
<label for="phone">Phone</label>
<input type="tel" id="phone" name="phone">
</form-show-if>This web component works in all modern browsers that support:
- Custom Elements v1
- ES Modules (for module usage)
For older browsers, you may need polyfills for Custom Elements.
# Run tests
npm test
# Run tests once
npm run test:run
# Run tests with UI
npm run test:ui
# Run tests with coverage
npm run test:coverage# Lint code
npm run lint
# Format code
npm run format