You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm building a component library plugin, and many of the more complex components share similar features. I intended to use the Composition API as a means of sharing the logic for these features.
Because of the limitations described below, I can include part of each feature by calling the use<FeatureName> composable function within setup(), but other aspects of features, particularly those which require access to DOM elements or template refs, must be included in other component Options through various means. Is anyone aware of a cleaner way of accomplishing this? I'm open to any ideas.
I am aware that this cannot be accessed inside the setup() method (or <script setup>). I'm also aware that getCurrentInstance(), which had been available in the past, is no longer available due to it being an internal API. Some have said that getCurrentInstance() is fine to use for the sake of developing plugins. According to the source code, getCurrentInstance() is
For getting a hold of the internal instance in setup() - useful for advanced plugins
My code is below.
// TextField.vueimport{defineComponent,computed}from'vue';importAtxFieldHelperTextfrom'../field-helper-text/FieldHelperText.vue';importAtxIconfrom'../icon/Icon.vue';import{formField,id,aria,fieldHelperText}from'../../composables';const{ useAria }=aria;const{ useFormField }=formField;const{ useFieldHelperText }=fieldHelperText;const{ useId }=id;exportdefaultdefineComponent({name: 'TextField',components: {
AtxFieldHelperText,
AtxIcon,},emits: [...formField.emits],props: {
...aria.props,
...formField.props,
...fieldHelperText.props,
...id.props,},setup(props,context){// Component IdentifieruseId();// Form Fieldconst{ fieldValue, isFilled }=useFormField(props,context);// ARIA and Accessibilityconst{ resolveAriaLabel }=useAria();constresolvedAriaLabel=computed(()=>{returnresolveAriaLabel(props.label);});// Helper Textconst{ resolvedFieldHelperText }=useFieldHelperText(props);return{
isFilled,
resolvedAriaLabel,
fieldValue,
resolvedFieldHelperText,};},methods: {// This method cannot be defined in `useFormField()`, because it requires access to template refs.onClickLabel: formField.methods.onClickLabel.bind(this)},mounted(){// The mounted hook cannot be defined in `useFormField()`, because it references `this` when assigning an `id` attribute to the root element.id.mounted.bind(this)()},});
// composables/form-field.jsimport{defineProps,defineEmits,onMounted,ref,computed,watch}from'vue';exportconstformField={props: {label: {type: String,required: true},modelValue: {type: String,},placeholder: {type: String},disabled: {type: Boolean},},emits: ['update:modelValue','input'],useFormField(props,{ emit }){constfieldValue=ref('');constisFilled=computed(()=>{returnfieldValue.value.length>0});watch(fieldValue,(newValue)=>{emit('update:modelValue',newValue);emit('input',newValue);});return{
fieldValue,
isFilled
};},methods: {// This method cannot be defined in `useFormField()`, because it requires access to template refs.onClickLabel($event){$event.preventDefault();if(this.disabled)return;if(this.$refs.input){this.$refs.input.focus();}}}}
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
I'm building a component library plugin, and many of the more complex components share similar features. I intended to use the Composition API as a means of sharing the logic for these features.
Because of the limitations described below, I can include part of each feature by calling the
use<FeatureName>
composable function withinsetup()
, but other aspects of features, particularly those which require access to DOM elements or template refs, must be included in other component Options through various means. Is anyone aware of a cleaner way of accomplishing this? I'm open to any ideas.I am aware that
this
cannot be accessed inside thesetup()
method (or<script setup>
). I'm also aware thatgetCurrentInstance()
, which had been available in the past, is no longer available due to it being an internal API. Some have said thatgetCurrentInstance()
is fine to use for the sake of developing plugins. According to the source code,getCurrentInstance()
isMy code is below.
Beta Was this translation helpful? Give feedback.
All reactions