55 * https://opensource.org/licenses/MIT
66 */
77
8- import { ElOption , ElSelect } from "element-plus"
9- import { ref , Ref , h , defineComponent , PropType } from "vue"
8+ import type { PropType } from "vue"
9+ import type { HabitMessage } from "@app/locale/components/habit"
10+
11+ import { ref , Ref , h , defineComponent } from "vue"
1012import { daysAgo } from "@util/time"
1113import { t } from "@app/locale"
12- import { HabitMessage } from "@app/locale/components/habit"
1314import SwitchFilterItem from "@app/components/common/switch-filter-item"
1415import { ElementDatePickerShortcut } from "@src/element-ui/date"
1516import DateRangeFilterItem from "@app/components/common/date-range-filter-item"
17+ import SelectFilterItem from "@app/components/common/select-filter-item"
1618
1719export type HabitFilterOption = {
1820 periodSize : number
@@ -46,17 +48,17 @@ const DATE_RANGE_END_PLACEHOLDER = t(msg => msg.trend.endDate)
4648// [value, label]
4749type _SizeOption = [ number , keyof HabitMessage [ 'sizes' ] ]
4850
49- const SIZE_OPTIONS : _SizeOption [ ] = [
50- [ 1 , 'fifteen' ] ,
51- [ 2 , 'halfHour' ] ,
52- [ 4 , 'hour ' ] ,
53- [ 8 , 'twoHour' ]
54- ]
55-
56- const renderSizeOption = ( [ size , msg ] : _SizeOption ) => h ( ElOption , {
57- label : t ( root => root . habit . sizes [ msg ] ) ,
58- value : size
59- } )
51+ function allOptions ( ) : Record < number , string > {
52+ const allOptions = { }
53+ const allSizes : _SizeOption [ ] = [
54+ [ 1 , 'fifteen ' ] ,
55+ [ 2 , 'halfHour' ] ,
56+ [ 4 , 'hour' ] ,
57+ [ 8 , 'twoHour' ] ,
58+ ]
59+ allSizes . forEach ( ( [ size , msg ] ) => allOptions [ size ] = t ( root => root . habit . sizes [ msg ] ) )
60+ return allOptions
61+ }
6062
6163const _default = defineComponent ( {
6264 name : "HabitFilter" ,
@@ -67,7 +69,6 @@ const _default = defineComponent({
6769 } ,
6870 emits : [ "change" ] ,
6971 setup ( props , ctx ) {
70- const trendSearching : Ref < boolean > = ref ( false )
7172 const periodSize : Ref < number > = ref ( props . periodSize || 1 )
7273 // @ts -ignore
7374 const dateRange : Ref < Date [ ] > = ref ( props . dateRange || [ ] )
@@ -81,21 +82,19 @@ const _default = defineComponent({
8182 }
8283 return ( ) => [
8384 // Size select
84- h ( ElSelect , {
85- placeholder : t ( msg => msg . trend . hostPlaceholder ) ,
86- class : 'filter-item' ,
87- modelValue : periodSize . value ,
88- filterable : true ,
89- loading : trendSearching . value ,
90- onChange : ( val : string ) => {
85+ h ( SelectFilterItem , {
86+ historyName : 'periodSize' ,
87+ defaultValue : periodSize . value ?. toString ?.( ) ,
88+ options : allOptions ( ) ,
89+ onSelect ( val : string ) {
9190 const newPeriodSize = parseInt ( val )
9291 if ( isNaN ( newPeriodSize ) ) {
9392 return
9493 }
9594 periodSize . value = newPeriodSize
9695 handleChange ( )
9796 } ,
98- } , ( ) => SIZE_OPTIONS . map ( renderSizeOption ) ) ,
97+ } ) ,
9998 // Date range picker
10099 h ( DateRangeFilterItem , {
101100 startPlaceholder : DATE_RANGE_START_PLACEHOLDER ,
@@ -111,6 +110,7 @@ const _default = defineComponent({
111110 } ) ,
112111 // Average by date
113112 h ( SwitchFilterItem , {
113+ historyName : 'average' ,
114114 label : AVERAGE_LABEL ,
115115 defaultValue : averageByDate . value ,
116116 onChange : ( newVal : boolean ) => {
0 commit comments