Skip to content

Commit fcf168c

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

File tree

1 file changed

+60
-68
lines changed

1 file changed

+60
-68
lines changed

resources/js/Components/ItemIdSearchTextarea.vue

Lines changed: 60 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,67 @@ CdxTextArea.compatConfig = {
2830
COMPONENT_V_MODEL: false,
2931
};
3032
31-
export const MAX_NUM_IDS = 600;
33+
// TODO: export or see where to store this const
34+
const MAX_NUM_IDS = 600;
3235
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-
}
36+
// TODO: const { t } = useI18n();
37+
38+
const validationError: Ref<object> = ref(null);
39+
40+
const store = useStore();
41+
const textareaInputValue = ref(store.lastSearchedIds);
42+
43+
const props = defineProps<{
44+
loading: boolean
45+
}>();
46+
47+
function splitInput(): Array<string> {
48+
return textareaInputValue.value.split( '\n' );
49+
};
50+
function sanitizeArray(): Array<string> {
51+
// this filter function removes all falsy values
52+
// see: https://stackoverflow.com/a/281335/1619792
53+
return splitInput().filter(x => x);
54+
};
55+
function serializeInput(): string {
56+
return sanitizeArray().join('|');
57+
};
58+
function validate(): void {
59+
validationError.value = null;
60+
61+
const typeError = 'error';
62+
63+
const rules = [{
64+
check: (ids: Array<string>) => ids.length < 1,
65+
type: typeError,
66+
// message: { [typeError]: this.$i18n('item-form-error-message-empty') }
67+
message: { [typeError]: 'empty' }
5268
},
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-
},
69+
{
70+
check: (ids: Array<string>) => ids.length > MAX_NUM_IDS,
71+
type: 'error',
72+
// message: { [typeError]: i18n('item-form-error-message-max', MAX_NUM_IDS) }
73+
message: { [typeError]: 'max' }
9574
},
96-
data() {
97-
return {
98-
validationError: null
75+
{
76+
check: (ids: Array<string>) => !ids.every(value => /^[Qq]\d+$/.test( value.trim() )),
77+
type: 'error',
78+
// message: { [typeError]: i18n('item-form-error-message-invalid') }
79+
message: { [typeError]: 'invalid' }
80+
}];
81+
82+
const sanitized = sanitizeArray();
83+
84+
for(const {check, type, message} of rules){
85+
if(check(sanitized)){
86+
validationError.value = { type, message };
87+
return;
88+
}
9989
}
100-
},
101-
});
90+
};
91+
92+
defineExpose({validate, serializeInput, validationError});
93+
10294
</script>
10395

10496
<style lang="scss">

0 commit comments

Comments
 (0)