Skip to content
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

Use Codex Field + TextInput instead of Wikit TextInput #765

Merged
merged 2 commits into from
Sep 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 40 additions & 28 deletions src/components/LemmaInput.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<script setup lang="ts">
import { TextInput } from '@wmde/wikit-vue-components';
import { CdxField, CdxTextInput, ValidationMessages, ValidationStatusType } from '@wikimedia/codex';
import RequiredAsterisk from '@/components/RequiredAsterisk.vue';
import { useMessages } from '@/plugins/MessagesPlugin/Messages';
import { useConfig } from '@/plugins/ConfigPlugin/Config';
Expand All @@ -21,23 +21,33 @@ const messages = useMessages();
const config = useConfig();
const exampleLemma = config.placeholderExampleData.lemma;
const store = useStore();
const error = computed( () => {
const error = computed( (): {
status: ValidationStatusType;
messages: ValidationMessages;
} => {
const inputLength = Array.from( props.modelValue ).length;
if ( inputLength > config.maxLemmaLength ) {
return {
type: 'error',
message: messages.getUnescaped(
'wikibaselexeme-newlexeme-lemma-too-long-error',
config.maxLemmaLength.toString(),
),
status: 'error',
messages: {
error: messages.getUnescaped(
'wikibaselexeme-newlexeme-lemma-too-long-error',
config.maxLemmaLength.toString(),
),
},
};
}
if ( !store.state.perFieldErrors.lemmaErrors.length ) {
return null;
return {
status: 'default',
messages: {},
};
}
return {
type: 'error',
message: messages.getUnescaped( store.state.perFieldErrors.lemmaErrors[ 0 ].messageKey ),
status: 'error',
messages: {
error: messages.getUnescaped( store.state.perFieldErrors.lemmaErrors[ 0 ].messageKey ),
},
};
} );
</script>
Expand All @@ -51,31 +61,33 @@ export default {
</script>

<template>
<text-input
<cdx-field
class="wbl-snl-lemma-input"
:label="messages.getUnescaped( 'wikibaselexeme-newlexeme-lemma' )"
:placeholder="messages.getUnescaped(
'wikibaselexeme-newlexeme-lemma-placeholder-with-example',
exampleLemma
)"
name="lemma"
aria-required="true"
:error="error"
:value="modelValue"
@input="$emit( 'update:modelValue', $event )"
:status="error.status"
:messages="error.messages"
>
<template #suffix>
<required-asterisk />
<!-- eslint-disable vue/v-on-event-hyphenation -->
<cdx-text-input
:placeholder="messages.getUnescaped(
'wikibaselexeme-newlexeme-lemma-placeholder-with-example',
exampleLemma
)"
name="lemma"
aria-required="true"
:model-value="modelValue"
@update:modelValue="$emit( 'update:modelValue', $event )"
/>
<!-- eslint-enable -->
<template #label>
{{ messages.getUnescaped( 'wikibaselexeme-newlexeme-lemma' ) }}<required-asterisk />
</template>
</text-input>
</cdx-field>
</template>

<style lang="scss">
@import '@wmde/wikit-tokens/variables';
codders marked this conversation as resolved.
Show resolved Hide resolved

/* stylelint-disable plugin/stylelint-bem-namics, selector-class-pattern */
.wbl-snl-lemma-input.wikit .wikit-TextInput__label-wrapper {
gap: $dimension-spacing-xsmall;
.wbl-snl-required-asterisk {
margin-inline-start: $dimension-spacing-xsmall;
}
/* stylelint-enable plugin/stylelint-bem-namics, selector-class-pattern */
</style>
4 changes: 2 additions & 2 deletions tests/integration/NewLexemeForm.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -309,11 +309,11 @@ describe( 'NewLexemeForm', () => {
await wrapper.trigger( 'submit' );

const lemmaInputWrapper = wrapper.get( '.wbl-snl-lemma-input' );
expect( lemmaInputWrapper.get( '.wikit-ValidationMessage--error' ).text() ).toBe( messagesPlugin.get( 'wikibaselexeme-newlexeme-lemma-empty-error' ) );
expect( lemmaInputWrapper.get( '.cdx-message--error' ).text() ).toBe( messagesPlugin.get( 'wikibaselexeme-newlexeme-lemma-empty-error' ) );

await lemmaInputWrapper.get( 'input' ).setValue( 'foo' );

expect( lemmaInputWrapper.find( '.wikit-ValidationMessage--error' ).exists() ).toBe( false );
expect( lemmaInputWrapper.find( '.cdx-message--error' ).exists() ).toBe( false );

expect( createLexeme ).not.toHaveBeenCalled();
} );
Expand Down
6 changes: 3 additions & 3 deletions tests/unit/components/LemmaInput.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ describe( 'LemmaInput', () => {
store.state.perFieldErrors.lemmaErrors.push( { messageKey: 'wikibaselexeme-newlexeme-lemma-empty-error' } );
await nextTick();

expect( lemmaInputWrapper.get( '.wikit-ValidationMessage--error' ).text() ).toContain( '⧼wikibaselexeme-newlexeme-lemma-empty-error⧽' );
expect( lemmaInputWrapper.get( '.cdx-message--error' ).text() ).toContain( '⧼wikibaselexeme-newlexeme-lemma-empty-error⧽' );
} );

it( 'displays an error message when input is too longer than configured', async () => {
Expand All @@ -82,8 +82,8 @@ describe( 'LemmaInput', () => {
global: { provide: { [ MessagesKey as symbol ]: { getUnescaped: messageGet } } },
} );

expect( lemmaInputWrapper.get( '.wikit-ValidationMessage--error' ).text() ).toContain( '⧼wikibaselexeme-newlexeme-lemma-too-long-error⧽' );
expect( messageGet ).toHaveBeenNthCalledWith( 3, 'wikibaselexeme-newlexeme-lemma-too-long-error', '8' );
expect( lemmaInputWrapper.get( '.cdx-message--error' ).text() ).toContain( '⧼wikibaselexeme-newlexeme-lemma-too-long-error⧽' );
expect( messageGet ).toHaveBeenNthCalledWith( 1, 'wikibaselexeme-newlexeme-lemma-too-long-error', '8' );
} );

it( 'counts multi-byte characters as code points', async () => {
Expand Down
Loading