File tree Expand file tree Collapse file tree 2 files changed +26
-2
lines changed
app/javascript/solidus_admin/web_components
spec/components/previews/solidus_admin/ui/forms/select Expand file tree Collapse file tree 2 files changed +26
-2
lines changed Original file line number Diff line number Diff line change @@ -105,9 +105,18 @@ class SolidusSelect extends HTMLSelectElement {
105
105
this . tomselect . settings . load = null ;
106
106
}
107
107
108
+ // Fetch options from remote source. If options data is nested in json response, specify path to it within "data-src"
109
+ // attribute after "::" separator.
110
+ // E.g. https://whatcms.org/API/List data is deep nested in json response: `{ result: { list: [...] } }`, so
111
+ // in order to access it, specify "data-src" as follows:
112
+ // "data-src"="https://whatcms.org/API/List::result.list"
108
113
async fetchOptions ( ) {
109
- const response = await fetch ( this . getAttribute ( "data-src" ) ) ;
110
- return await response . json ( ) ;
114
+ const [ url , dataPath ] = this . getAttribute ( "data-src" ) . split ( "::" ) ;
115
+ const response = await fetch ( url ) ;
116
+ const json = await response . json ( ) ;
117
+ if ( ! dataPath ) return json ;
118
+
119
+ return dataPath . split ( '.' ) . reduce ( ( acc , key ) => acc && acc [ key ] , json ) ;
111
120
}
112
121
}
113
122
Original file line number Diff line number Diff line change @@ -8,6 +8,21 @@ def overview
8
8
render_with_template
9
9
end
10
10
11
+ # @param multiple toggle
12
+ # @param latency toggle "Simulate request latency (2000ms)"
13
+ def remote ( multiple : false , latency : false )
14
+ args = { label : "Label" , name : "select" , multiple : }
15
+ delay_url = "app.requestly.io/delay/2000/" if latency
16
+ src = "https://#{ delay_url } whatcms.org/API/List::result.list"
17
+ args . merge! (
18
+ src :,
19
+ "data-option-value-field" : "id" ,
20
+ "data-option-label-field" : "label" ,
21
+ )
22
+
23
+ render component ( "ui/forms/select" ) . new ( **args )
24
+ end
25
+
11
26
# @param size select { choices: [s, m, l] }
12
27
# @param options number
13
28
# @param multiple toggle
You can’t perform that action at this time.
0 commit comments