Skip to content

Commit 7b75898

Browse files
committed
Support a default range value and improve logic when using steps. Closes #2328.
1 parent 424e3ac commit 7b75898

File tree

3 files changed

+27
-3
lines changed

3 files changed

+27
-3
lines changed

resources/js/components/fieldtypes/RangeFieldtype.vue

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,23 @@ export default {
3030
3131
data() {
3232
return {
33-
val: this.value || (this.config.max - this.config.min) / 2
33+
val: this.value || this.config.default || this.getDefault()
34+
}
35+
},
36+
37+
methods: {
38+
getDefault() {
39+
// Spec: https://html.spec.whatwg.org/multipage/input.html#range-state-(type=range)
40+
if (this.config.max < this.config.min) return this.config.min;
41+
42+
var val = this.config.min + (this.config.max - this.config.min) / 2;
43+
44+
// make sure on a valid step
45+
if (this.config.step) {
46+
val = Math.floor(val / this.config.step) * this.config.step;
47+
}
48+
49+
return val;
3450
}
3551
},
3652

resources/lang/en/fieldtypes.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22

33
return [
4+
'any.config.default' => 'Set the default value.',
45
'array.config.keys' => 'Set the array keys (variables) and optional labels.',
56
'array.config.mode' => 'Dynamic mode gives the user control of the data while keyed mode does not.',
67
'assets.config.allow_uploads' => 'Allow new file uploads.',

src/Fieldtypes/Range.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,21 @@ protected function configFieldItems(): array
2525
'instructions' => __('statamic::fieldtypes.range.config.min'),
2626
'type' => 'integer',
2727
'default' => 0,
28-
'width' => 50,
28+
'width' => 33,
2929
],
3030
'max' => [
3131
'display' => __('Max'),
3232
'instructions' => __('statamic::fieldtypes.range.config.max'),
3333
'type' => 'integer',
3434
'default' => 100,
35-
'width' => 50,
35+
'width' => 33,
36+
],
37+
'default' => [
38+
'display' => __('Default'),
39+
'instructions' => __('statamic::fieldtypes.any.config.default'),
40+
'type' => 'integer',
41+
'default' => null,
42+
'width' => 33,
3643
],
3744
'prepend' => [
3845
'display' => __('Prepend'),

0 commit comments

Comments
 (0)