-
Notifications
You must be signed in to change notification settings - Fork 8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Replace Wikit Textarea with Codex Textarea component #771
Changes from all commits
4dd4db7
e4e5ff7
0410cb1
b62c91c
9fd7600
22c83c6
9d70229
9aa3703
5021f51
242bf73
6028a8f
64e9048
54cd3f7
97f4f8e
fcf168c
5dffb0c
94b4505
fdb98aa
ea871ca
d50dd53
011a249
35af9ff
e6ea197
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
<template> | ||
<cdx-field | ||
:status="validationError ? validationError.type : 'default'" | ||
:messages="validationError ? validationError.message : null" | ||
> | ||
<div class="progress-bar-wrapper"> | ||
<cdx-progress-bar v-if="loading" :aria-label="$i18n('item-form-progress-bar-aria-label')" /> | ||
</div> | ||
<cdx-text-area | ||
:label="$i18n('item-form-id-input-label')" | ||
:placeholder="$i18n('item-form-id-input-placeholder')" | ||
:rows="8" | ||
:status="validationError ? validationError.type : 'default'" | ||
v-model="textareaInputValue" | ||
/> | ||
</cdx-field> | ||
</template> | ||
|
||
<script setup lang="ts"> | ||
import { ref, inject } from 'vue'; | ||
import { Ref } from 'vue'; | ||
import { useI18n } from 'vue-banana-i18n'; | ||
import { useStore } from '../store'; | ||
import { CdxTextArea, CdxField, CdxProgressBar } from "@wikimedia/codex"; | ||
import ValidationError from '../types/ValidationError'; | ||
|
||
// Run it with compat mode | ||
// https://v3-migration.vuejs.org/breaking-changes/v-model.html | ||
CdxTextArea.compatConfig = { | ||
...CdxTextArea.compatConfig, | ||
COMPONENT_V_MODEL: false, | ||
}; | ||
|
||
const validationError: Ref<ValidationError> = ref(null); | ||
|
||
const messages = useI18n(); | ||
|
||
const store = useStore(); | ||
const textareaInputValue = ref(store.lastSearchedIds); | ||
|
||
const MAX_NUM_IDS = inject('MAX_NUM_IDS'); | ||
|
||
defineProps<{loading: boolean}>(); | ||
|
||
function splitInput(): Array<string> { | ||
return textareaInputValue.value.split( '\n' ); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think some spaces are needed in this line and after the next couple of functions. So close together is a bit messy on the eyes. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've added some spaces. |
||
|
||
function sanitizeArray(): Array<string> { | ||
// this filter function removes all falsy values | ||
// see: https://stackoverflow.com/a/281335/1619792 | ||
return splitInput().filter(x => x); | ||
} | ||
|
||
function serializeInput(): string { | ||
return sanitizeArray().join('|'); | ||
} | ||
|
||
function validate(): void { | ||
validationError.value = null; | ||
|
||
const typeError = 'error'; | ||
|
||
const rules = [{ | ||
check: (ids: Array<string>) => ids.length < 1, | ||
type: typeError, | ||
message: { [typeError]: messages.i18n('item-form-error-message-empty') as string } | ||
}, | ||
{ | ||
check: (ids: Array<string>) => ids.length > (MAX_NUM_IDS as number), | ||
type: typeError, | ||
message: { [typeError]: messages.i18n('item-form-error-message-max', MAX_NUM_IDS) as string } | ||
}, | ||
{ | ||
check: (ids: Array<string>) => !ids.every(value => /^[Qq]\d+$/.test( value.trim() )), | ||
type: typeError, | ||
message: { [typeError]: messages.i18n('item-form-error-message-invalid') as string } | ||
}]; | ||
|
||
const sanitized = sanitizeArray(); | ||
|
||
for(const {check, type, message} of rules){ | ||
if(check(sanitized)){ | ||
validationError.value = { type, message }; | ||
return; | ||
} | ||
} | ||
} | ||
|
||
defineExpose({validate, serializeInput, validationError}); | ||
|
||
</script> | ||
|
||
<style lang="scss"> | ||
|
||
.cdx-field__control { | ||
position: relative; | ||
width: 100%; | ||
|
||
.progress-bar-wrapper { | ||
position: absolute; | ||
top: 50%; | ||
width: 100%; | ||
|
||
.cdx-progress-bar { | ||
width: 50%; | ||
margin: auto; | ||
} | ||
} | ||
} | ||
|
||
</style> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
type ValidationError = { | ||
type: string, | ||
message: { [key : string] : string } | ||
} | ||
|
||
export default ValidationError; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nice!