Skip to content

Commit

Permalink
Merge pull request rancher#10565 from rak-phillip/chore/10104-defineC…
Browse files Browse the repository at this point in the history
…omponent

Reintroduce usage of `defineComponent`
  • Loading branch information
rak-phillip authored Mar 13, 2024
2 parents 4a9ce08 + 57e4430 commit 5782b70
Show file tree
Hide file tree
Showing 32 changed files with 464 additions and 140 deletions.
5 changes: 3 additions & 2 deletions pkg/rancher-components/src/components/Accordion/Accordion.vue
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<script lang="ts">
import Vue from 'vue';
import { defineComponent } from 'vue';
import { mapGetters } from 'vuex';
export default Vue.extend({
export default defineComponent({
props: {
title: {
type: String,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<script lang="ts">
import Vue, { PropType } from 'vue';
import { PropType, defineComponent } from 'vue';
interface Badge {
stateBackground: string;
Expand All @@ -11,7 +11,7 @@ interface Badge {
* <p>Represents a badge whose label and color is either taken from the value property or
* from the label and color properties. The state property takes precedence.</p>
*/
export default Vue.extend({
export default defineComponent({
props: {
/**
* A value having the properties `stateBackground` and `stateDisplay`
Expand Down Expand Up @@ -59,7 +59,7 @@ export default Vue.extend({
</script>

<template>
<span :class="{'badge-state': true, [bg]: true}">
<span :class="['badge-state', bg]">
<i
v-if="icon"
class="icon"
Expand Down
4 changes: 2 additions & 2 deletions pkg/rancher-components/src/components/Banner/Banner.vue
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<script lang="ts">
import Vue from 'vue';
import { defineComponent } from 'vue';
import { nlToBr } from '@shell/utils/string';
import { stringify } from '@shell/utils/error';
export default Vue.extend({
export default defineComponent({
props: {
/**
* A color class that represents the color of the banner.
Expand Down
6 changes: 3 additions & 3 deletions pkg/rancher-components/src/components/Card/Card.vue
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<script lang="ts">
import Vue from 'vue';
import { defineComponent, PropType } from 'vue';
export default Vue.extend({
export default defineComponent({
name: 'Card',
props: {
/**
Expand All @@ -22,7 +22,7 @@ export default Vue.extend({
* The function to invoke when the default action button is clicked.
*/
buttonAction: {
type: Function,
type: Function as PropType<(event: MouseEvent) => void>,
default: (): void => { }
},
/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<script lang="ts">
import Vue, { PropType } from 'vue';
import { PropType, defineComponent } from 'vue';
import { _EDIT, _VIEW } from '@shell/config/query-params';
import { addObject, removeObject } from '@shell/utils/array';
import cloneDeep from 'lodash/cloneDeep';
export default Vue.extend({
export default defineComponent({
name: 'Checkbox',
props: {
Expand Down Expand Up @@ -140,7 +140,7 @@ export default Vue.extend({
/**
* Toggles the checked state for the checkbox and emits an 'input' event.
*/
clicked(event: MouseEvent): boolean | void {
clicked(event: MouseEvent | KeyboardEvent): boolean | void {
if ((event.target as HTMLLinkElement).tagName === 'A' && (event.target as HTMLLinkElement).href) {
// Ignore links inside the checkbox label so you can click them
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ describe('component: LabeledInput', () => {
it('should emit input only once', () => {
const value = '2';
const delay = 1;
const wrapper = mount(LabeledInput as any, {
const wrapper = mount(LabeledInput, {
propsData: { delay },
mocks: { $store: { getters: { 'i18n/t': jest.fn() } } }
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,27 @@
<script lang="ts">
import Vue, { VueConstructor } from 'vue';
import CompactInput from '@shell/mixins/compact-input';
import LabeledFormElement from '@shell/mixins/labeled-form-element';
import { defineComponent } from 'vue';
import TextAreaAutoGrow from '@components/Form/TextArea/TextAreaAutoGrow.vue';
import LabeledTooltip from '@components/LabeledTooltip/LabeledTooltip.vue';
import { escapeHtml } from '@shell/utils/string';
import cronstrue from 'cronstrue';
import { isValidCron } from 'cron-validator';
import { debounce } from 'lodash';
import { useLabeledFormElement, labeledFormElementProps } from '@shell/composables/useLabeledFormElement';
import { useCompactInput } from '@shell/composables/useCompactInput';
export default (
Vue as VueConstructor<Vue & InstanceType<typeof LabeledFormElement> & InstanceType<typeof CompactInput>>
).extend({
declare module 'vue/types/vue' {
interface Vue {
onInput: (event: Event) => void | ((event: Event) => void);
}
}
export default defineComponent({
components: { LabeledTooltip, TextAreaAutoGrow },
mixins: [LabeledFormElement, CompactInput],
inheritAttrs: false,
props: {
...labeledFormElementProps,
/**
* The type of the Labeled Input.
* @values text, cron, multiline, multiline-password
Expand Down Expand Up @@ -93,21 +99,36 @@ export default (
},
},
setup(props, { emit }) {
const {
focused,
onFocusLabeled,
onBlurLabeled,
isDisabled,
validationMessage,
requiredField
} = useLabeledFormElement(props, emit);
const { isCompact } = useCompactInput(props);
return {
focused,
onFocusLabeled,
onBlurLabeled,
isDisabled,
validationMessage,
requiredField,
isCompact,
};
},
data() {
return {
updated: false,
validationErrors: ''
validationErrors: '',
};
},
computed: {
/**
* Determines if the Labeled Input @input event should be debounced.
*/
onInput(): ((value: string) => void) | void {
return this.delay ? debounce(this.delayInput, this.delay) : this.delayInput;
},
/**
* Determines if the Labeled Input should display a label.
*/
Expand All @@ -122,7 +143,7 @@ export default (
return !!this.tooltip || !!this.tooltipKey;
},
tooltipValue(): string | undefined {
tooltipValue(): string | Record<string, unknown> | undefined {
if (this.hasTooltip) {
return this.tooltipKey ? this.t(this.tooltipKey) : this.tooltip;
}
Expand All @@ -144,7 +165,7 @@ export default (
if (this.type !== 'cron' || !this.value) {
return;
}
if (!isValidCron(this.value)) {
if (typeof this.value === 'string' && !isValidCron(this.value)) {
return this.t('generic.invalidCron');
}
try {
Expand Down Expand Up @@ -173,15 +194,22 @@ export default (
/**
* The max length for the Labeled Input.
*/
_maxlength(): number | null {
_maxlength(): number | undefined {
if (this.type === 'text' && this.maxlength) {
return this.maxlength;
}
return null;
return undefined;
},
},
created() {
/**
* Determines if the Labeled Input @input event should be debounced.
*/
this.onInput = this.delay ? debounce(this.delayInput, this.delay) : this.delayInput;
},
methods: {
/**
* Attempts to give the Labeled Input focus.
Expand Down Expand Up @@ -220,7 +248,9 @@ export default (
* NOTE: In multiline, TextAreaAutoGrow emits a string with the value
* https://github.com/rancher/dashboard/issues/10249
*/
delayInput(value: string): void {
delayInput(val: string | Event): void {
const value = typeof val === 'string' ? val : (val?.target as HTMLInputElement)?.value;
this.$emit('input', value);
},
Expand All @@ -237,7 +267,7 @@ export default (
* event.
* @see labeled-form-element.ts mixin for onBlurLabeled()
*/
onBlur(event: string): void {
onBlur(event: string | FocusEvent): void {
this.$emit('blur', event);
this.onBlurLabeled();
},
Expand Down Expand Up @@ -289,7 +319,7 @@ export default (
:placeholder="_placeholder"
autocapitalize="off"
:class="{ conceal: type === 'multiline-password' }"
@input="onInput($event)"
@input="onInput"
@focus="onFocus"
@blur="onBlur"
/>
Expand All @@ -306,7 +336,7 @@ export default (
autocomplete="off"
autocapitalize="off"
:data-lpignore="ignorePasswordManagers"
@input="onInput($event.target.value)"
@input="onInput"
@focus="onFocus"
@blur="onBlur"
@change="onChange"
Expand Down
20 changes: 13 additions & 7 deletions pkg/rancher-components/src/components/Form/Radio/RadioButton.vue
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
<script lang="ts">
import Vue from 'vue';
import { defineComponent } from 'vue';
import { _VIEW } from '@shell/config/query-params';
import { randomStr } from '@shell/utils/string';
export default Vue.extend({
export default defineComponent({
props: {
/**
* The name of the input, for grouping.
Expand Down Expand Up @@ -71,7 +72,10 @@ export default Vue.extend({
},
data() {
return { isChecked: this.value === this.val };
return {
isChecked: this.value === this.val,
randomString: `${ randomStr() }-radio`,
};
},
computed: {
Expand Down Expand Up @@ -115,13 +119,15 @@ export default Vue.extend({
/**
* Emits the input event.
*/
clicked({ target }: { target?: HTMLElement }) {
if (this.isDisabled || target?.tagName === 'A') {
clicked(event: MouseEvent | KeyboardEvent) {
const target = event.target;
if (this.isDisabled || (target instanceof HTMLElement && target.tagName === 'A')) {
return;
}
this.$emit('input', this.val);
}
},
}
});
</script>
Expand All @@ -134,7 +140,7 @@ export default Vue.extend({
@click.stop="clicked($event)"
>
<input
:id="_uid+'-radio'"
:id="randomString"
:disabled="isDisabled"
:name="name"
:value="''+val"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
<script lang="ts">
import Vue, { PropType } from 'vue';
import { PropType, defineComponent } from 'vue';
import { _VIEW } from '@shell/config/query-params';
import RadioButton from '@components/Form/Radio/RadioButton.vue';
interface Option {
value: unknown,
label: string
label: string,
description?: string,
}
export default Vue.extend({
export default defineComponent({
components: { RadioButton },
props: {
/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<script lang="ts">
import Vue from 'vue';
import { defineComponent } from 'vue';
import debounce from 'lodash/debounce';
import { _EDIT, _VIEW } from '@shell/config/query-params';
Expand All @@ -10,7 +10,7 @@ declare module 'vue/types/vue' {
}
}
export default Vue.extend({
export default defineComponent({
inheritAttrs: false,
props: {
Expand Down Expand Up @@ -115,7 +115,9 @@ export default Vue.extend({
/**
* Emits the input event and resizes the Text Area.
*/
onInput(val: string): void {
onInput(event: Event): void {
const val = (event?.target as HTMLInputElement)?.value;
this.$emit('input', val);
this.queueResize();
},
Expand Down Expand Up @@ -163,7 +165,7 @@ export default Vue.extend({
v-bind="$attrs"
:spellcheck="spellcheck"
@paste="$emit('paste', $event)"
@input="onInput($event.target.value)"
@input="onInput($event)"
@focus="$emit('focus', $event)"
@blur="$emit('blur', $event)"
/>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
<script lang="ts">
import Vue from 'vue';
export default Vue.extend({
import { defineComponent } from 'vue';
type StateType = boolean | 'true' | 'false' | undefined;
export default defineComponent({
props: {
value: {
type: [Boolean, String, Number],
Expand Down Expand Up @@ -28,7 +31,7 @@ export default Vue.extend({
},
},
data() {
return { state: false as boolean | string | number };
return { state: false as StateType };
},
watch: {
Expand All @@ -41,7 +44,7 @@ export default Vue.extend({
},
methods: {
toggle(neu: boolean | string | number) {
toggle(neu: StateType | null) {
this.state = neu === null ? !this.state : neu;
this.$emit('input', this.state ? this.onValue : this.offValue);
}
Expand Down
Loading

0 comments on commit 5782b70

Please sign in to comment.