Skip to content

Commit 2233942

Browse files
committed
[WiP] rewrite component using Composition API
1 parent 97f4f8e commit 2233942

File tree

1 file changed

+59
-68
lines changed

1 file changed

+59
-68
lines changed

resources/js/Components/ItemIdSearchTextarea.vue

Lines changed: 59 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@
1616
</cdx-field>
1717
</template>
1818

19-
<script lang="ts">
19+
<script setup lang="ts">
2020
import { defineComponent, ref } from 'vue';
21+
import type {Ref} from 'vue';
22+
// TODO: import i18n, { useI18n } from 'vue-banana-i18n';
2123
import { useStore } from '../store';
2224
import { CdxTextArea, CdxField, CdxProgressBar } from "@wikimedia/codex";
2325
@@ -28,77 +30,66 @@ CdxTextArea.compatConfig = {
2830
COMPONENT_V_MODEL: false,
2931
};
3032
31-
export const MAX_NUM_IDS = 600;
33+
const MAX_NUM_IDS = 600;
3234
33-
export default defineComponent({
34-
components: {
35-
CdxField,
36-
CdxProgressBar,
37-
CdxTextArea,
38-
},
39-
setup() {
40-
const store = useStore();
41-
const textareaInputValue = ref(store.lastSearchedIds);
42-
43-
return {
44-
textareaInputValue
45-
};
46-
},
47-
props: {
48-
loading: {
49-
type: Boolean,
50-
default: false
51-
}
35+
// TODO: const { t } = useI18n();
36+
37+
const validationError: Ref<object> = ref(null);
38+
39+
const store = useStore();
40+
const textareaInputValue = ref(store.lastSearchedIds);
41+
42+
const props = defineProps<{
43+
loading: boolean
44+
}>();
45+
46+
function splitInput(): Array<string> {
47+
return textareaInputValue.value.split( '\n' );
48+
};
49+
function sanitizeArray(): Array<string> {
50+
// this filter function removes all falsy values
51+
// see: https://stackoverflow.com/a/281335/1619792
52+
return splitInput().filter(x => x);
53+
};
54+
function serializeInput(): string {
55+
return sanitizeArray().join('|');
56+
};
57+
function validate(): void {
58+
validationError.value = null;
59+
60+
const typeError = 'error';
61+
62+
const rules = [{
63+
check: (ids: Array<string>) => ids.length < 1,
64+
type: typeError,
65+
// message: { [typeError]: this.$i18n('item-form-error-message-empty') }
66+
message: { [typeError]: 'empty' }
5267
},
53-
methods: {
54-
splitInput: function(): Array<string> {
55-
return this.textareaInputValue.split( '\n' );
56-
},
57-
sanitizeArray: function(): Array<string> {
58-
// this filter function removes all falsy values
59-
// see: https://stackoverflow.com/a/281335/1619792
60-
return this.splitInput().filter(x => x);
61-
},
62-
serializeInput: function(): string {
63-
return this.sanitizeArray().join('|');
64-
},
65-
validate(): void {
66-
this.validationError = null;
67-
68-
const typeError = 'error';
69-
70-
const rules = [{
71-
check: (ids: Array<string>) => ids.length < 1,
72-
type: typeError,
73-
message: { [typeError]: this.$i18n('item-form-error-message-empty') }
74-
},
75-
{
76-
check: (ids: Array<string>) => ids.length > MAX_NUM_IDS,
77-
type: 'error',
78-
message: { [typeError]: this.$i18n('item-form-error-message-max', MAX_NUM_IDS) }
79-
},
80-
{
81-
check: (ids: Array<string>) => !ids.every(value => /^[Qq]\d+$/.test( value.trim() )),
82-
type: 'error',
83-
message: { [typeError]: this.$i18n('item-form-error-message-invalid') }
84-
}];
85-
86-
const sanitized = this.sanitizeArray();
87-
88-
for(const {check, type, message} of rules){
89-
if(check(sanitized)){
90-
this.validationError = { type, message };
91-
return;
92-
}
93-
}
94-
},
68+
{
69+
check: (ids: Array<string>) => ids.length > MAX_NUM_IDS,
70+
type: 'error',
71+
// message: { [typeError]: i18n('item-form-error-message-max', MAX_NUM_IDS) }
72+
message: { [typeError]: 'max' }
9573
},
96-
data() {
97-
return {
98-
validationError: null
74+
{
75+
check: (ids: Array<string>) => !ids.every(value => /^[Qq]\d+$/.test( value.trim() )),
76+
type: 'error',
77+
// message: { [typeError]: i18n('item-form-error-message-invalid') }
78+
message: { [typeError]: 'invalid' }
79+
}];
80+
81+
const sanitized = sanitizeArray();
82+
83+
for(const {check, type, message} of rules){
84+
if(check(sanitized)){
85+
validationError.value = { type, message };
86+
return;
87+
}
9988
}
100-
},
101-
});
89+
};
90+
91+
defineExpose({validate, serializeInput, validationError});
92+
10293
</script>
10394

10495
<style lang="scss">

0 commit comments

Comments
 (0)