Skip to content

Commit eef0cc9

Browse files
authored
feat: add property initializers (#8846)
Initial values are only set by property initializers and there are no longer any defaults per type from the framework Remove the defaultValue and related fields from the @Property decorator Remove all runtime checks and validations BREAKING CHANGE: @Property decorator must be adapted according to new type parameter
1 parent 3a02e3e commit eef0cc9

File tree

232 files changed

+2178
-2646
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

232 files changed

+2178
-2646
lines changed

docs/5-development/03-understanding-components-metadata.md

+53-66
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ Defines the HTML tag for the Web Component.
1515
tag: "my-component",
1616
})
1717
class MyComponent extends UI5Element {};
18-
```
18+
```
1919

2020

2121

@@ -94,10 +94,10 @@ class MyComponent extends UI5Element {
9494

9595
## Properties / Attributes
9696

97-
Defines the HTML properties for the Web Component.
97+
Defines the HTML properties for the Web Component.
9898

9999
**Note about attributes:** By default, for each property an equivalent attribute is supported. Attributes have the same names as properties, but in `kebab-case` rather than `camelCase`.
100-
Properties of type `Object`, properties with `multiple` set to`true` and properties with `noAttribute` set to `true` do not have an attribute equivalent.
100+
Properties of type `Object`, properties with `multiple` set to`true` and properties with `noAttribute` set to `true` do not have an attribute equivalent.
101101

102102
#### Example
103103

@@ -107,79 +107,66 @@ Properties of type `Object`, properties with `multiple` set to`true` and propert
107107
})
108108
class DemoComponent extends UI5Element {
109109

110-
@property({ type: String, defaultValue: "Hello" })
111-
message!: string;
110+
@property()
111+
message = "Hello"
112112

113113
@property({ type: Boolean, "noAttribute": true })
114-
shown!: boolean;
114+
shown = false
115115

116116
@property({ type: Object })
117-
settings!: object;
117+
settings: object = {};
118118

119-
@property({ validator: Integer })
120-
animationDuration!: number;
119+
@property({ type: Number })
120+
animationDuration = 1000;
121121

122-
@property({ validator: Integer, "multiple": true })
123-
nums!: Array<number>;
122+
@property({ type: Array })
123+
nums: Array<number> = []
124124
};
125-
```
125+
```
126126

127127

128128
#### @property configuration settings
129129

130-
| Setting | Type | Default | Description |
131-
|----------------|------------------------------|-----------|---------------------------------------------------------------------------------------------------------------|
132-
| `type` | Property type | String | The type of the property. For more information on types see the table below. |
133-
| `defaultValue` | Any valid value for the type | undefined | Default value of the property. Cannot be set for type "Boolean". Booleans are always false by default in HTML.|
134-
| `multiple` | Boolean | false | Indicates whether the property represents a single value or is an array of values of the given type. |
135-
| `noAttribute` | Boolean | false | No attribute equivalent will be created for that property. Always true for properties of type Object. |
136-
| `validator` | Validator type | N/A | The validator of the property. For more information on validators see the table below |
130+
| Setting | Type | Default | Description |
131+
| ------------- | ------------- | ------- | ------------------------------------------------------------------------------------------------------------------ |
132+
| `type` | Property type | String | The type of the property. For more information on types see the table below. |
133+
| `noAttribute` | Boolean | false | No attribute equivalent will be created for that property. Always true for properties of type Object. |
134+
| `converter` | Object | N/A | A custom converter with two methods - `fromAttribute` and `toAttribute` that will receive the `type` and the value |
137135

138-
**Note:** The `type` setting is required, except these two cases:
136+
Types are used for attribute conversion only. Converting from a property to a string, the framework can check the runtime type and convert to string, but coming an attribute, there is no way to know whether it is a boolean or a number unless a type is also given.
137+
**Note:** If `type` is omitted, it is implied to be `String`
139138

140-
- `string` properties (type: String is considered default)
139+
#### Types
141140

142-
```ts
143-
@property()
144-
name!: string;
145-
```
141+
| Type | Class to Use | Description |
142+
| ------- | ------------ | -------------------------------------------------------------------------- |
143+
| string | `String` | Implied as default, should be omitted |
144+
| boolean | `Boolean` | Boolean value, the presence of the attribute will set the property to true |
145+
| number | `Number` | Number value, the attribute will be converted using `parseFloat` |
146+
| object | `Object` | JS Object, equivalent to `noAttribute: true` |
147+
| [] | `Array` | JS Array, equivalent to `noAttribute: true` |
148+
| Enum | `String` | Enums are treated as strings, type does not accept enum types |
146149

147-
- `validator` is provided:
150+
#### Initial values
148151

152+
Use property initializers to pass initial values
149153
```ts
150-
@property({ validator: Integer })
151-
animationDuration!: number;
152-
```
153-
154-
155-
#### Types
156-
157-
| Type | Class to Use | Description |
158-
|-------------|---------------------------------------------------------|-----------------------------------------|
159-
| string | `String` | String value |
160-
| boolean | `Boolean` | Boolean value - always false by default |
161-
| object | `Object` | JS Object |
162-
| custom type | Extend `@ui5/webcomponents-base/dist/types/DataType.js` | Used mainly for enumerations |
163-
154+
@property()
155+
name = "user1";
164156

165-
#### Examples of prebuilt custom types
157+
@property({ type: Boolean })
158+
collapsed = false;
166159

167-
| Type | Class to Use | Description |
168-
|------------|----------------------------------------------------|----------------------------------------------------------------|
169-
| Integer | `@ui5/webcomponents-base/dist/types/Integer.js` | Integer value |
170-
| ValueState | `@ui5/webcomponents-base/dist/types/ValueState.js` | Enumeration with: `None`, `Error`, `Warning`, `Success` values |
160+
@property({ type: Number })
161+
maxValue = 5;
171162

172-
#### Validators
163+
@property({ type: Object })
164+
accProperties = {};
173165

174-
The `validator` is a custom class that implements `isValid` function that validates the property's value whenever it changes. If value is not valid, the framework will set the proeprty `defaultValue`.
166+
@property({ type: Array })
167+
stars = [];
168+
```
175169

176-
| Type | Class to Use | Description |
177-
|-------------|----------------------------------------------------------|-----------------------------------------|
178-
| number | `Integer` | Validates the prop value to integer |
179-
| number | `Float` | Validates the prop value to float |
180-
| string | `CSSColor` | Validates the prop value to valid CSS color|
181-
| string | `CSSSize` | Validates the prop value to valid CSS size |
182-
| string or HTMLElement | `DomReference` | Validates the prop value is it's a string or HTMLElement |
183170

184171
## Slots
185172

@@ -205,18 +192,18 @@ class MyComponent extends UI5Element {
205192

206193
#### Slot configuration settings
207194

208-
| Setting | Type | Default | Description |
209-
|------------------------------|-------------------------|---------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------|
210-
| `type` * | `HTMLElement` or `Node` | N/A | The type of the children that can go into that slot. |
195+
| Setting | Type | Default | Description |
196+
| ---------------------------- | ----------------------- | ------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
197+
| `type` * | `HTMLElement` or `Node` | N/A | The type of the children that can go into that slot. |
211198
| `individualSlots` | `Boolean` | false | If set to `true`, each child will have its own slot, allowing you to arrange/wrap the children arbitrarily. |
212199
| `propertyName` | `String` | N/A | Allows to set the name of the property on the Web Component, where the children belonging to this slot will be stored. |
213200
| `invalidateOnChildChange` ** | `Boolean` or `Object` | false | **Experimental, do not use.** Defines whether every invalidation of a UI5 Web Component in this slot should trigger an invalidation of the parent UI5 Web Component. |
214201

215202
`*` The `type` setting is required.
216203

217-
`**`
218-
**Note:** `invalidateOnChildChange` is not meant to be used with standard DOM Elements and is not to be confused with `MutationObserver`-like functionality.
219-
It rather targets the use case of components that slot abstract items (`UI5Element` instances without a template) and require to be invalidated in turn whenever these items are invalidated.
204+
`**`
205+
**Note:** `invalidateOnChildChange` is not meant to be used with standard DOM Elements and is not to be confused with `MutationObserver`-like functionality.
206+
It rather targets the use case of components that slot abstract items (`UI5Element` instances without a template) and require to be invalidated in turn whenever these items are invalidated.
220207

221208
The `invalidateOnChildChange` setting can be either a `Boolean` (`true` meaning invalidate the component on any change of a child in this slot) or an `Object` with `properties` and `slots` fields. They in turn can be either of
222209
type `Boolean` (`true` meaning invalidate on any property change or any slot change) or `Array` of strings indicating exactly which properties or slots lead to invalidation.
@@ -246,7 +233,7 @@ class MyComponent extends UI5Element {
246233

247234
```js
248235
class MyComponent extends UI5Element {
249-
@slot({
236+
@slot({
250237
type: HTMLElement,
251238
invalidateOnChildChange: {
252239
"properties": true,
@@ -261,7 +248,7 @@ class MyComponent extends UI5Element {
261248

262249
```js
263250
class MyComponent extends UI5Element {
264-
@slot({
251+
@slot({
265252
type: HTMLElement,
266253
invalidateOnChildChange: {
267254
"properties": ["text", "selected", "disabled"],
@@ -273,14 +260,14 @@ class MyComponent extends UI5Element {
273260
```
274261

275262
Notes:
276-
- Children without a `slot` attribute will be assigned to the `default` slot.
263+
- Children without a `slot` attribute will be assigned to the `default` slot.
277264
- All HTML text nodes will be assigned to the `default` slot, as they cannot have a `slot` attribute.
278265
- For all slots the Web Component will have a property created, with the name of the slot, to hold a list of all children assigned to this slot.
279266
For example, if you have a slot named "rows", "this.rows" will be an array, holding references to all children with `slot="rows"`, or no slot, if rows was default.
280267
- For the `default` slot you can provide an accessor. In the following example, `this.items` will hold all children that were assigned to the default slot.
281268

282269
```ts
283-
@slot({
270+
@slot({
284271
type: HTMLElement
285272
"default": true,
286273
})
@@ -290,8 +277,8 @@ items!: Array<HTMLElement>;
290277

291278
#### Slot types
292279

293-
| Type | Description |
294-
|-------------|-------------------------------------------|
280+
| Type | Description |
281+
| ----------- | ----------------------------------------- |
295282
| Node | Accepts both Text nodes and HTML Elements |
296283
| HTMLElement | Accepts HTML Elements only |
297284

0 commit comments

Comments
 (0)