mirror of
https://github.com/ionic-team/ionic-framework.git
synced 2025-08-16 10:01:59 +08:00
Vue inputs and v-model support. #14912
This commit is contained in:
57
vue/src/components/inputs/IonBaseInputVue.ts
Normal file
57
vue/src/components/inputs/IonBaseInputVue.ts
Normal file
@ -0,0 +1,57 @@
|
||||
import Vue from 'vue';
|
||||
|
||||
/**
|
||||
* Create a wrapped input component that captures typical ionic input events
|
||||
* and emits core ones so v-model works.
|
||||
* @param {} name the vue name of the component
|
||||
* @param {*} coreTag the actual tag to render (such as ion-datetime)
|
||||
*/
|
||||
export function createInputComponent(name: string, coreTag: string, modelEvent = 'ionChange', valueProperty = 'value') {
|
||||
return Vue.component(name, {
|
||||
model: {
|
||||
event: modelEvent,
|
||||
prop: valueProperty
|
||||
},
|
||||
render(createElement: any) {
|
||||
// Vue types have a bug accessing member properties:
|
||||
// https://github.com/vuejs/vue/issues/8721
|
||||
const cmp: any = this;
|
||||
|
||||
return createElement(coreTag, {
|
||||
'attrs': cmp.attrs,
|
||||
'on': {
|
||||
'ionChange': cmp.handleChange,
|
||||
'ionInput': cmp.handleInput,
|
||||
'ionBlur': cmp.handleBlur,
|
||||
'ionFocus': cmp.handleFocus
|
||||
}
|
||||
}, this.$slots.default);
|
||||
},
|
||||
methods: {
|
||||
handleChange($event: any) {
|
||||
if (modelEvent === 'ionChange') {
|
||||
// Vue expects the value to be sent as the argument for v-model, not the
|
||||
// actual event object
|
||||
this.$emit('ionChange', $event.target[valueProperty]);
|
||||
} else {
|
||||
this.$emit('ionChange', $event);
|
||||
}
|
||||
},
|
||||
handleInput($event: any) {
|
||||
if (modelEvent === 'ionInput') {
|
||||
// Vue expects the value to be sent as the argument for v-model, not the
|
||||
// actual event object
|
||||
this.$emit('ionInput', $event.target[valueProperty]);
|
||||
} else {
|
||||
this.$emit('ionInput', $event);
|
||||
}
|
||||
},
|
||||
handleBlur($event: any) {
|
||||
this.$emit('ionBlur', $event);
|
||||
},
|
||||
handleFocus($event: any) {
|
||||
this.$emit('ionFocus', $event);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
3
vue/src/components/inputs/IonCheckboxVue.ts
Normal file
3
vue/src/components/inputs/IonCheckboxVue.ts
Normal file
@ -0,0 +1,3 @@
|
||||
import { createInputComponent } from "./IonBaseInputVue";
|
||||
|
||||
export default createInputComponent('IonCheckboxVue', 'ion-checkbox', 'ionChange', 'checked');
|
3
vue/src/components/inputs/IonDatetimeVue.ts
Normal file
3
vue/src/components/inputs/IonDatetimeVue.ts
Normal file
@ -0,0 +1,3 @@
|
||||
import { createInputComponent } from "./IonBaseInputVue";
|
||||
|
||||
export default createInputComponent('IonDatetimeVue', 'ion-datetime');
|
3
vue/src/components/inputs/IonInputVue.ts
Normal file
3
vue/src/components/inputs/IonInputVue.ts
Normal file
@ -0,0 +1,3 @@
|
||||
import { createInputComponent } from "./IonBaseInputVue";
|
||||
|
||||
export default createInputComponent('IonInputVue', 'ion-input', 'ionInput');
|
3
vue/src/components/inputs/IonRadioVue.ts
Normal file
3
vue/src/components/inputs/IonRadioVue.ts
Normal file
@ -0,0 +1,3 @@
|
||||
import { createInputComponent } from "./IonBaseInputVue";
|
||||
|
||||
export default createInputComponent('IonRadioVue', 'ion-radio');
|
3
vue/src/components/inputs/IonRangeVue.ts
Normal file
3
vue/src/components/inputs/IonRangeVue.ts
Normal file
@ -0,0 +1,3 @@
|
||||
import { createInputComponent } from "./IonBaseInputVue";
|
||||
|
||||
export default createInputComponent('IonRangeVue', 'ion-range');
|
3
vue/src/components/inputs/IonSearchbarVue.ts
Normal file
3
vue/src/components/inputs/IonSearchbarVue.ts
Normal file
@ -0,0 +1,3 @@
|
||||
import { createInputComponent } from "./IonBaseInputVue";
|
||||
|
||||
export default createInputComponent('IonSearchbarVue', 'ion-searchbar', 'ionInput');
|
3
vue/src/components/inputs/IonSelectVue.ts
Normal file
3
vue/src/components/inputs/IonSelectVue.ts
Normal file
@ -0,0 +1,3 @@
|
||||
import { createInputComponent } from "./IonBaseInputVue";
|
||||
|
||||
export default createInputComponent('IonSelectVue', 'ion-select');
|
3
vue/src/components/inputs/IonTextareaVue.ts
Normal file
3
vue/src/components/inputs/IonTextareaVue.ts
Normal file
@ -0,0 +1,3 @@
|
||||
import { createInputComponent } from "./IonBaseInputVue";
|
||||
|
||||
export default createInputComponent('IonTextareaVue', 'ion-textarea');
|
3
vue/src/components/inputs/IonToggleVue.ts
Normal file
3
vue/src/components/inputs/IonToggleVue.ts
Normal file
@ -0,0 +1,3 @@
|
||||
import { createInputComponent } from "./IonBaseInputVue";
|
||||
|
||||
export default createInputComponent('IonToggleVue', 'ion-toggle', 'ionChange', 'checked');
|
21
vue/src/components/inputs/index.ts
Normal file
21
vue/src/components/inputs/index.ts
Normal file
@ -0,0 +1,21 @@
|
||||
import IonDatetimeVue from './IonDatetimeVue';
|
||||
import IonInputVue from './IonInputVue';
|
||||
import IonRadioVue from './IonRadioVue';
|
||||
import IonRangeVue from './IonRangeVue';
|
||||
import IonSelectVue from './IonSelectVue';
|
||||
import IonTextareaVue from './IonTextareaVue';
|
||||
import IonToggleVue from './IonToggleVue';
|
||||
import IonSearchbarVue from './IonSearchbarVue';
|
||||
import IonCheckboxVue from './IonCheckboxVue';
|
||||
|
||||
export {
|
||||
IonCheckboxVue,
|
||||
IonDatetimeVue,
|
||||
IonInputVue,
|
||||
IonRadioVue,
|
||||
IonRangeVue,
|
||||
IonSearchbarVue,
|
||||
IonSelectVue,
|
||||
IonTextareaVue,
|
||||
IonToggleVue
|
||||
};
|
Reference in New Issue
Block a user