@dbp-toolkit/form-elements
v0.2.19
Published
Form elements for the Digital Blueprint Toolkit
Readme
Form Element Web Components
This package offers form components to be used to create an HTML form.
You can install this component via npm:
npm i @dbp-toolkit/form-elementsUsage
<dbp-form-string-element></dbp-form-string-element>
<script type="module" src="node_modules/@dbp-toolkit/form-elements/dist/string.js"></script><dbp-form-number-element></dbp-form-number-element>
<script type="module" src="node_modules/@dbp-toolkit/form-elements/dist/number.js"></script><dbp-form-submission-select-element></dbp-form-submission-select-element>
<script
type="module"
src="node_modules/@dbp-toolkit/form-elements/dist/submission-select.js"></script>Or directly via CDN:
<dbp-form-string-element></dbp-form-string-element>
<script
type="module"
src="https://unpkg.com/@dbp-toolkit/[email protected]/dist/string.js"></script><dbp-form-number-element></dbp-form-number-element>
<script
type="module"
src="https://unpkg.com/@dbp-toolkit/[email protected]/dist/number.js"></script><dbp-form-submission-select-element></dbp-form-submission-select-element>
<script
type="module"
src="https://unpkg.com/@dbp-toolkit/[email protected]/dist/submission-select.js"></script>Attributes
Best look at demo.js for the example implementation.
General Attributes
lang(optional, default:de): Language setting- Type: String
- Accepts:
de(German) oren(English) - Example:
<element lang="en"></element>
name: Element name- Type: String
- Used for identifying the form element
description: Descriptive text for the element- Type: String
- Provides additional context or explanation
label: Display label for the element- Type: String
- Text shown alongside or near the form element
value: Current value of the element- Type: String
- Reflects current input state
required: Indicates if the element is mandatory- Type: Boolean
- Determines if the field must be filled
hidden: Indicates if the element is hidden- Type: Boolean
- Hides the element if set to
true
disabled: Indicates if the element is disabled- Type: Boolean
- Disables the element if set to
true
customValidator: Custom validation function- Type: Function
- Example:
<element .customValidator=${(value, evaluationData) => {return value === 'test' ? ['My message'] : []}}></element>
layout-type(optional, available value:inline): Renders label and value the same line ininlinestyle.- Type: String
- Example with list:
<dbp-form-enum-view layout-type="inline"></dbp-form-enum-view>
String Element
rows(optional, default:1): Number of rows for the text area (1 means a single-line input)- Type: Number
- Example:
<dbp-form-string-element rows="3"></dbp-form-string-element>
placeholder(optional): Placeholder text for the input field- Type: Text
- Example:
<dbp-form-string-element placeholder="Short phrase that hints at the expected data"></dbp-form-string-element>
word-count-limit(optional): Limit max word count for textarea- Type: Number
- Example:
<dbp-form-string-element word-count-limit="300"></dbp-form-string-element>
Number Element
placeholder(optional): Placeholder text for the number input field- Type: Text
- Example:
<dbp-form-number-element placeholder="Enter a number"></dbp-form-number-element>
min(optional): Minimum value allowed- Type: Number
- Example:
<dbp-form-number-element min="0"></dbp-form-number-element>
max(optional): Maximum value allowed- Type: Number
- Example:
<dbp-form-number-element max="100"></dbp-form-number-element>
step(optional): Increment/decrement step value- Type: Number
- Example:
<dbp-form-number-element step="0.5"></dbp-form-number-element>
Boolean Element
value: Use the stringtrueorfalseto set the value- Type: String
- Example:
<dbp-form-boolean-element value="true"></dbp-form-boolean-element>
state: Instead ofvalueyou can also usestateto set the state of the boolean element as a boolean- Type: Boolean
data-value: Returns the state of the boolean element- Type: Boolean
Examples
The change event is fired when the state changes. The event contains the new state in e.detail.state as a boolean.
Boolean Element with String Value
<dbp-form-boolean-element
subscribe="lang"
name="myBoolean"
label="My label"
description="My description"
.value=${this.myBoolean ? 'true' : 'false'}
@change="${(e) { console.log('state', e.detail.state); }}>
</dbp-form-boolean-element>Boolean Element with Boolean State
<dbp-form-boolean-element
subscribe="lang"
name="myBoolean"
label="My label"
description="My description"
.state=${this.myBoolean}
@change="${(e) { console.log('state', e.detail.state); }}>
</dbp-form-boolean-element>Enum Element
<dbp-form-enum-element
${ref(this.myComponentEnumRef)}
subscribe="lang"
name="myComponentEnum"
label="My enum"
.value=${data.myComponentEnum || ''}
.items=${{item1: 'Item 1', item2: 'Item 2'}}
multiple
required>
<span slot="label">
Label with HTML content. Can be used interchangeably the label property.
</span>
</dbp-form-enum-element>Please always use .value to set the value of the enum element, as it is a property and not an attribute.
items: List of items to select from- Type: Object
- Example:
<dbp-form-enum-element .items=${{item1: 'Item 1', item2: 'Item 2'}}></dbp-form-enum-element>
multiple(optional, default:false): Indicates if multiple items can be selected- Type: Boolean
- Example:
<dbp-form-enum-element multiple></dbp-form-enum-element>
display-mode(optional, default:dropdown): Render as checkbox or radiobutton inlistmode, select-element indropdownmode or as tags intagsmode- Type: String
- Example with list:
<dbp-form-enum-element display-mode="list"></dbp-form-enum-element> - Example with tags:
<dbp-form-enum-element display-mode="tags"></dbp-form-enum-element>
tagPlaceholder(optional, default:Please select an option): Placeholder text to display when usingdisplay-mode=tags- Type: Object
- Example:
<dbp-form-enum-element display-mode="tags" .tagPlaceholder=${{en: 'Select tags', de: 'Wähle Tags'}}></dbp-form-enum-element>
- CSS custom properties for styling:
--dbp-enum-label-gap
disabledItems: selectively disable individual options for display-mode list (checkbox/radio). Disabled items are visually greyed out and cannot be interacted with (disabled).
Date Element
min: Indicates the minimum value that can be entered- Type: String/Date
max: Indicates the maximum value that can be entered- Type: String/Date
DateTime Element
No additional attributes beyond the general ones. The datetime element uses an HTML datetime-local input.
Person Select Element (form element)
Lets the user search for persons and select one, or several with multiple.
<dbp-form-person-select-element
subscribe="lang auth"
name="myPerson"
label="My person"
entry-point-url="https://api.example.com"
required></dbp-form-person-select-element>Multiple selection, the value is an array of person identifiers:
<dbp-form-person-select-element
subscribe="lang auth"
name="myPeople"
label="My people"
entry-point-url="https://api.example.com"
.value=${['person-identifier-1', 'person-identifier-2']}
multiple></dbp-form-person-select-element>entry-point-url: Base URL of the API entry point- Type: String
- Required for fetching persons
multiple(optional, default:false): Indicates if several persons can be selected- Type: Boolean
- Example:
<dbp-form-person-select-element multiple></dbp-form-person-select-element>
value: The selected person identifier, or an array of identifiers withmultiple- Type: String/Array
- Identifiers are stored without the
/base/people/prefix, both notations are accepted as input - Always use the
.valueproperty formultiple, since an array cannot be passed as an attribute
The change event contains the selected person identifiers, values is always an array:
// {fieldName: 'myPeople', name: 'myPeople', value: ['person-1'], values: ['person-1']}
event.detail;Resource Select Element
<dbp-form-resource-select-element
subscribe="lang auth"
name="myResource"
label="My resource"
entry-point-url="https://api.example.com"
resource-path="/base/organizations"
required></dbp-form-resource-select-element>entry-point-url: Base URL of the API entry point- Type: String
- Required for fetching resources
resource-path: API path to the resource collection- Type: String
- Example:
<dbp-form-resource-select-element resource-path="/base/organizations"></dbp-form-resource-select-element>
include-local(optional): Whether to include local resources- Type: String
- Example:
<dbp-form-resource-select-element include-local="true"></dbp-form-resource-select-element>
per-page(optional, default:30): Number of results per page- Type: Number
- Example:
<dbp-form-resource-select-element per-page="50"></dbp-form-resource-select-element>
Submission Select Element
The submission select element loads all forms matching a Formalize frontendKey, then loads the submissions of those forms and renders them in a Select2 selector. The selected value is the selected submission identifier.
<dbp-form-submission-select-element
subscribe="lang auth"
name="selectedCompany"
label="Company"
entry-point-url="https://api.example.com"
frontend-key="bulletin-company"
submission-element-name="name"
required></dbp-form-submission-select-element>entry-point-url: Base URL of the API entry point- Type: String
- Required for fetching forms and submissions
frontend-key: Formalize frontend key used to find matching forms- Type: String
- Example:
<dbp-form-submission-select-element frontend-key="bulletin-company"></dbp-form-submission-select-element>
submission-element-name: Name of the element inside each submission'sdataFeedElementthat should be shown as option text- Type: String
- Example:
<dbp-form-submission-select-element submission-element-name="name"></dbp-form-submission-select-element>
per-page(optional, default:9999): Number of forms and submissions requested per API call- Type: Number
- Example:
<dbp-form-submission-select-element per-page="100"></dbp-form-submission-select-element>
Form Views
They are a little similar to the form element components. Best look at demo.js for the example implementation.
General Attributes
lang(optional, default:de): Language setting- Type: String
- Accepts:
de(German) oren(English) - Example:
<view lang="en"></view>
label: Display label for the view- Type: String
- Text shown alongside or near the form view
value: Current value of the view- Type: String
- Reflects current input state
hidden: Indicates if the view is hidden- Type: Boolean
- Hides the view if set to
true
layout-type(optional, available value:inline): Renders label and value the same line ininlinestyle.- Type: String
- Example with list:
<dbp-form-enum-view layout-type="inline"></dbp-form-enum-view>
Enum Element
<dbp-form-enum-view
subscribe="lang"
name="myComponentEnum"
label="My enum"
.value=${data.myComponentEnum || ''}
.items=${{item1: 'Item 1', item2: 'Item 2'}}>
<span slot="label">
Label with HTML content. Can be used interchangeably the label property.
</span>
</dbp-form-enum-view>display-mode(optional, available value:tags):tagsmode renders values in tag style with background color.- Type: String
- Example with list:
<dbp-form-enum-view display-mode="tags"></dbp-form-enum-view>
- CSS custom properties for styling:
--dbp-enum-label-gap--dbp-enum-fieldset-line-height--dbp-enum-tags-gap--dbp-enum-tag-padding--dbp-enum-tag-border-radius--dbp-enum-tag-line-height--dbp-enum-tag-bg-color
Date View
Displays a date value formatted as DD.MM.YYYY using the de-DE locale. No additional attributes beyond the general ones.
<dbp-form-date-view
subscribe="lang"
label="My date"
.value=${'2024-01-15'}></dbp-form-date-view>DateTime View
Displays a datetime value formatted with date, time, and timezone using the de-DE locale. No additional attributes beyond the general ones.
<dbp-form-datetime-view
subscribe="lang"
label="My datetime"
.value=${'2024-01-15T10:30:00'}></dbp-form-datetime-view>Person Select View
Fetches and displays a person's full name from the API using the stored person identifier.
With multiple all selected persons are resolved and shown one per line.
<dbp-form-person-select-view
subscribe="lang auth"
label="My person"
entry-point-url="https://api.example.com"
.value=${'person-identifier'}></dbp-form-person-select-view><dbp-form-person-select-view
subscribe="lang auth"
label="My people"
entry-point-url="https://api.example.com"
.value=${['person-identifier-1', 'person-identifier-2']}
multiple></dbp-form-person-select-view>entry-point-url: Base URL of the API entry point used to resolve the person name- Type: String
multiple(optional, default:false): Indicates if several persons should be displayed- Type: Boolean
- Without
multipleonly the first person of an array value is shown
value: The person identifier, or an array of identifiers withmultiple- Type: String/Array
- Always use the
.valueproperty formultiple, since an array cannot be passed as an attribute
Resource Select View
Fetches and displays a resource's name from the API using the stored resource identifier.
<dbp-form-resource-select-view
subscribe="lang auth"
label="My resource"
entry-point-url="https://api.example.com"
resource-path="/base/organizations"
.value=${'organization-identifier'}></dbp-form-resource-select-view>entry-point-url: Base URL of the API entry point used to resolve the resource name- Type: String
resource-path: Path of the resource collection the identifier belongs to (must match the correspondingdbp-form-resource-select-element)- Type: String
- Example:
<dbp-form-resource-select-view resource-path="/base/organizations"></dbp-form-resource-select-view>
Local development
# get the source
git clone [email protected]:digital-blueprint/toolkit.git
cd toolkit/packages/form-elements
# install dependencies
npm install
# constantly build dist/bundle.js and run a local web-server on port 8002
npm run watch
# run tests
npm test
# build local packages in dist directory
npm run buildJump to http://localhost:8002 and you should get a demo page.
