18
18
@focus =" handleFocus"
19
19
@visible-change =" handleVisibleChange"
20
20
@clear =" handleClear"
21
+ @change =" handleChange"
21
22
:filterable =" filterable"
22
23
>
23
24
<slot name =" prefix" slot =" prefix" ></slot >
28
29
</template >
29
30
30
31
<script lang="ts">
31
- import { Component , Mixins , ModelSync , Prop , Watch , Ref , Inject } from ' vue-property-decorator'
32
+ import { Component , Mixins , Prop , Watch , Ref , Inject } from ' vue-property-decorator'
32
33
import ElSelect from ' element-ui/lib/select'
33
34
import { ElForm } from ' element-ui/types/form'
34
35
import { ElFormItem } from ' element-ui/types/form-item'
@@ -39,10 +40,18 @@ import DesignSystemInject from '../../DesignSystem/DesignSystemInject'
39
40
import { Autocomplete } from ' ../../Input/consts'
40
41
import { InputTypes } from ' ../consts'
41
42
43
+ type SingleSelectValue = boolean | string | number ;
44
+ type MultipleSelectValue = Array <string | number >;
45
+ type SelectValue = SingleSelectValue | MultipleSelectValue ;
46
+
42
47
@Component ({
43
48
components: { ElSelect }
44
49
})
45
50
export default class SSelect extends Mixins (SizeMixin , BorderRadiusMixin , DesignSystemInject ) {
51
+ /**
52
+ * Selected value. Can be used with `v-model`
53
+ */
54
+ @Prop ({ type: [Boolean , String , Number , Array ] }) readonly value! : SelectValue
46
55
/**
47
56
* Input type of the select component. Available values: `"input"`, `"select"`.
48
57
*
@@ -121,19 +130,24 @@ export default class SSelect extends Mixins(SizeMixin, BorderRadiusMixin, Design
121
130
* `false` by default
122
131
*/
123
132
@Prop ({ type: Boolean , default: false }) readonly filterable! : boolean
124
- /**
125
- * Selected value. Can be used with `v-model`
126
- */
127
- @ModelSync (' value' , ' input' )
128
- readonly model! : any
133
+
134
+ get model (): SelectValue {
135
+ return this .value
136
+ }
137
+
138
+ set model (value : SelectValue ) {
139
+ if (JSON .stringify (value ) !== JSON .stringify (this .value )) {
140
+ this .$emit (' input' , value )
141
+ }
142
+ }
129
143
130
144
@Ref (' select' ) select! : any
131
145
132
146
@Inject ({ default: ' ' , from: ' elForm' }) elForm! : ElForm
133
147
@Inject ({ default: ' ' , from: ' elFormItem' }) elFormItem! : ElFormItem
134
148
135
149
@Watch (' model' )
136
- private handleValueChange (value : any ): void {
150
+ private handleValueChange (): void {
137
151
this .updateInputValue ()
138
152
}
139
153
@@ -148,7 +162,7 @@ export default class SSelect extends Mixins(SizeMixin, BorderRadiusMixin, Design
148
162
tags .remove ()
149
163
}
150
164
const input = this .select .$el .getElementsByClassName (' el-input__inner' )[0 ] as HTMLInputElement
151
- input .value = this .model && this .model .length ? ` ${this .multipleTextPrefix || this .placeholder } (${this .model .length }) ` : ' '
165
+ input .value = Array . isArray ( this .model ) && ( this .model as MultipleSelectValue ) .length ? ` ${this .multipleTextPrefix || this .placeholder } (${this .model .length }) ` : ' '
152
166
}
153
167
154
168
mounted (): void {
@@ -193,7 +207,7 @@ export default class SSelect extends Mixins(SizeMixin, BorderRadiusMixin, Design
193
207
if (this .disabled ) {
194
208
cssClasses .push (' s-disabled' )
195
209
}
196
- if ((! this .multiple && this .model ) || (this .multiple && this .model .length !== 0 )) {
210
+ if ((! this .multiple && this .model ) || (this .multiple && Array . isArray ( this . model ) && this .model .length !== 0 )) {
197
211
cssClasses .push (' s-has-value' )
198
212
}
199
213
return cssClasses
@@ -233,6 +247,10 @@ export default class SSelect extends Mixins(SizeMixin, BorderRadiusMixin, Design
233
247
this .$emit (' clear' )
234
248
}
235
249
250
+ handleChange (value : SelectValue ): void {
251
+ this .$emit (' change' , value )
252
+ }
253
+
236
254
public focus (): void {
237
255
this .select .focus ()
238
256
}
0 commit comments