File tree 3 files changed +76
-2
lines changed
lua/nui-components/signal
3 files changed +76
-2
lines changed Original file line number Diff line number Diff line change @@ -186,6 +186,69 @@ And here's the final result:
186
186
187
187
<Method name = " get_value" returns = " table" />
188
188
189
+ #### map
190
+
191
+ > Applies a mapping function to each value emitted by the ` Signal ` . The mapping function receives the current value of the ` Signal ` object.
192
+
193
+ <Method
194
+ name = " map"
195
+ args = { [
196
+ [' map_fn' , ' fun(value: T): R' ],
197
+ ]}
198
+ returns = " SignalValue"
199
+ />
200
+
201
+ Example:
202
+
203
+ ``` lua
204
+ local n = require ' nui-components'
205
+
206
+ local renderer = n .create_renderer {
207
+ width = 60 ,
208
+ height = 12 ,
209
+ }
210
+
211
+ local signal = n .create_signal {
212
+ name = ' ' ,
213
+ description = ' ' ,
214
+ }
215
+
216
+ local body = function ()
217
+ return n .rows (
218
+ n .paragraph {
219
+ is_focusable = false ,
220
+ lines = signal :map (function (val )
221
+ return {
222
+ n .line (n .text (' Name: ' , ' Keyword' ), val .name ),
223
+ n .line (n .text (' Description: ' , ' Keyword' ), val .description ),
224
+ }
225
+ end ),
226
+ },
227
+ n .gap (1 ),
228
+ n .form (
229
+ { id = ' form' },
230
+ n .text_input {
231
+ autofocus = true ,
232
+ max_lines = 1 ,
233
+ border_label = ' Name' ,
234
+ on_change = function (val )
235
+ signal .name = val
236
+ end ,
237
+ },
238
+ n .text_input {
239
+ max_lines = 1 ,
240
+ border_label = ' Description' ,
241
+ on_change = function (val )
242
+ signal .description = val
243
+ end ,
244
+ }
245
+ )
246
+ )
247
+ end
248
+
249
+ renderer :render (body )
250
+ ```
251
+
189
252
### Methods / Operators (Signal Value)
190
253
191
254
#### get_value
Original file line number Diff line number Diff line change @@ -30,6 +30,12 @@ function Signal.create(object)
30
30
end
31
31
end
32
32
33
+ if key == " map" then
34
+ return function (_ , ...)
35
+ return self :map (... )
36
+ end
37
+ end
38
+
33
39
if key == " get_value" then
34
40
return function (_ )
35
41
return self :get_value ()
@@ -72,4 +78,8 @@ function Signal:get_value()
72
78
return self ._private .subject :get_value ()
73
79
end
74
80
81
+ function Signal :map (map_fn )
82
+ return SignalValue .create (self ._private .subject ):map (map_fn )
83
+ end
84
+
75
85
return Signal
Original file line number Diff line number Diff line change @@ -11,7 +11,7 @@ function SignalValue.create(subject, key)
11
11
key = key ,
12
12
subject = subject ,
13
13
observable = subject :map (function (value )
14
- return value [key ]
14
+ return key ~= nil and value [key ] or value
15
15
end ),
16
16
},
17
17
}
@@ -20,7 +20,8 @@ function SignalValue.create(subject, key)
20
20
end
21
21
22
22
function SignalValue :get_value ()
23
- return self ._private .subject :get_value ()[self ._private .key ]
23
+ local val = self ._private .subject :get_value ()
24
+ return self ._private .key ~= nil and val [self ._private .key ] or val
24
25
end
25
26
26
27
function SignalValue :get_observer_value ()
You can’t perform that action at this time.
0 commit comments