Skip to content

Commit 3e55632

Browse files
committed
feat(Dropdown): Support magic default values (~last, ~first)
fixes #199
1 parent a48acba commit 3e55632

File tree

3 files changed

+28
-2
lines changed

3 files changed

+28
-2
lines changed

src/components/dropdown/Dropdown.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,18 @@ const DropdownComponent = Component({
6969
.append('select').attr('class', 'ds--select')
7070
},
7171
'render': (args, selection, data, item) => {
72+
const value = args.state_handler.get_state()[args.variable]
73+
if (value === args.default && args.default === '~first') {
74+
args.state_handler.set_variable(args.variable, data[0].value)
75+
}
76+
if (value === args.default && args.default === '~last') {
77+
args.state_handler.set_variable(args.variable, data.slice(-1)[0].value)
78+
}
79+
7280
item
7381
.selectAll('option').data(data).call(update_pattern)
7482
item
75-
.property('value', args.state_handler.get_state()[args.variable])
83+
.property('value', value)
7684
item
7785
.on('change', function() {
7886
args.state_handler.set_variable(args.variable, d3.select(this)

src/components/dropdown/Dropdown.test.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,5 +183,23 @@ describe('Text component', function() {
183183
'Correct number of items after change')
184184
})
185185

186+
const magic_default_values = [
187+
{default_value: '~first', actual_value: 'fo'},
188+
{default_value: '~last', actual_value: 'bar'},
189+
]
190+
magic_default_values.forEach(
191+
({default_value, actual_value}) => it(
192+
`automatic default variable ${default_value}`, () => {
193+
const { set_variable } = call_render_with({'args': {'variable': 'var',
194+
'default': default_value},
195+
'state': {'var': default_value},
196+
'data': [
197+
{ 'text': 'foo', 'value': 'fo' },
198+
{ 'text': 'bar', 'value': 'bar' },
199+
]})
200+
set_variable.should.be.calledWith('var', actual_value)
201+
}
202+
))
203+
186204
})
187205

src/manual_test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ dashboard "Cereals":
1616
- attr:query: '.[0].a'
1717
- data: file:///foo/bar.csv
1818
- h2 text: "By calories"
19-
- dropdown my_var=foo:
19+
- dropdown my_var=~last:
2020
- {"value": "foo", "text": "Foo"}
2121
- {"value": "bar", "text": "Bar"}
2222
- 2 columns:

0 commit comments

Comments
 (0)