You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -75,12 +75,15 @@ In the example below, we create `MySpecialButton` by extending `Neo.button.Base`
75
75
76
76
## Introducing New Configs
77
77
78
-
You can also add entirely new configuration properties to your custom components. Simply add them to the `static config`
79
-
block with a default value. Neo.mjs will automatically generate a getter and a setter for your new config, and you can
80
-
use it to control the behavior of your component.
78
+
You can also add entirely new configuration properties to your custom components. To make a config "reactive" – meaning
79
+
it automatically triggers a lifecycle method when its value changes – you **must** define it with a trailing underscore (`_`).
81
80
82
-
For example, if we wanted our `MySpecialButton` to have a `specialEffect` config, we could add
83
-
`specialEffect: 'glow'` to its config block.
81
+
For a reactive config like `myConfig_`, the framework provides this behavior:
82
+
-**Reading**: You can access the value directly: `this.myConfig`.
83
+
-**Writing**: Assigning a new value (`this.myConfig = 'new value'`) triggers a prototype-based setter. This is the core of Neo.mjs reactivity.
84
+
-**Hook**: After a value is set, the `afterSetMyConfig(value, oldValue)` method is automatically called.
85
+
86
+
If you define a config without the trailing underscore, it will simply be a static property on the class instance and will not trigger any lifecycle methods.
84
87
85
88
## Example: A Custom Button
86
89
@@ -101,17 +104,14 @@ class MySpecialButton extends Button {
101
104
iconCls:'far fa-face-grin-wide',
102
105
ui:'ghost',
103
106
104
-
// c. Add a new custom config
105
-
specialText:'I am special'
107
+
// c. Add a new reactive config (note the trailing underscore)
108
+
specialText_:'I am special'
106
109
}
107
110
108
111
// d. Hook into the component lifecycle
109
-
onConstructed() {
110
-
// Call the superclass method first
111
-
super.onConstructed();
112
-
113
-
// Access our new config property
114
-
console.log(this.specialText);
112
+
afterSetSpecialText(value, oldValue) {
113
+
// This method is called automatically when `specialText` is changed.
114
+
console.log(`specialText changed from "${oldValue}" to "${value}"`);
115
115
}
116
116
}
117
117
@@ -147,9 +147,108 @@ Neo.setupClass(MainView);
147
147
1.**Class Definition**: We define `MySpecialButton` which `extends` the framework's `Button` class.
148
148
2.**`className`**: We give our new class a unique `className`. This is important for the framework's class system.
149
149
3.**Overridden Configs**: We change the default `iconCls` and `ui` to style our button differently.
150
-
4.**New Config**: We add a `specialText` config. While this example doesn't use it to change the button's
151
-
appearance, the `onConstructed` method shows how you can access its value.
152
-
5.**Lifecycle Method**: We use `onConstructed`, which fires after the component is created, to log the value of our
153
-
new config. We call `super.onConstructed()` to ensure the parent class's logic is executed.
150
+
4.**New Reactive Config**: We add a `specialText_` config. The trailing underscore makes it reactive.
151
+
5.**Lifecycle Method**: We use `afterSetSpecialText()` to automatically run logic when the config changes. We also use
152
+
`onConstructed()` to run logic once when the component is created.
154
153
6.**Usage**: We use `MySpecialButton` in the `items` array of our `MainView` just like any other component, by
155
154
referencing its `module`.
155
+
156
+
## Extending `Component.Base`: Building VDom from Scratch
157
+
158
+
While extending specialized components like `Button` or `Container` is common for adding features (acting like a
159
+
Higher-Order Component), there are times when you need to define a component's HTML structure from the ground up. For
160
+
this, you extend the generic `Neo.component.Base`.
161
+
162
+
When you extend `component.Base`, you are responsible for defining the component's entire virtual DOM (VDom) structure
163
+
using the `vdom` config. This gives you complete control over the rendered output.
164
+
165
+
### Example: A Simple Profile Badge
166
+
167
+
Let's create a `ProfileBadge` component that displays a user's name and an online status indicator.
0 commit comments