Page MenuHomePhabricator

Field: Implement characters counter
Closed, ResolvedPublic

Description

Background

User stories

  • As a user, I need to know how many characters I can type in an input.
  • As a user, I need to know when I'm exceeding the characters when typing in an input.

Previous implementations

  • See T338817#9133502 for description of existing MediaWiki implementations using wgCommentCodePointLimit
  • Wikifunctions implements a custom front-end-only character counter for TextInputs (see T331000#9155716).

Design explorations

Some design explorations are captured in this Figma file:

Captura de Pantalla 2023-03-02 a las 13.15.37.png (1×2 px, 631 KB)

Design spec

Once a component spec sheet has been created in Figma, remove the note stating the spec is missing and link to the spec below.

Component spec sheet

Open questions

  • We need to evaluate if we want to indicate the exceeded characters and, if yes, the style for these exceeded characters.
    Captura de Pantalla 2023-03-02 a las 13.18.43.png (664×2 px, 489 KB)
  • We need to define the right padding between the helper text and characters counter so both texts are not too close to each other when the helper text is too long. Also, we need to define the maximum case to know how both texts grow when they are long.
    Captura de Pantalla 2023-03-02 a las 13.17.03.png (698×2 px, 487 KB)
  • Do we want to implement the error validation message as part of the characters counter?
    Captura de Pantalla 2023-03-02 a las 14.07.45.png (444×834 px, 138 KB)

Acceptance criteria (or Done)

Design

  • Design the Figma spec sheet and add it in this task
  • Update the component in the Figma library. This step will be done by a DST member.

Code

  • Implement the component in Codex

Event Timeline

Once we decide to start working on this task, we will need @KieranMcCann to define the following items listed as open questions:

  • We need to evaluate if we want to indicate the exceeded characters and, if yes, the style for these exceeded characters.
    Captura de Pantalla 2023-03-02 a las 13.18.43.png (664×2 px, 489 KB)
  • We need to define the right padding between the helper text and characters counter so both texts are not too close to each other when the helper text is too long. Also, we need to define the maximum case to know how both texts grow when they are long.
    Captura de Pantalla 2023-03-02 a las 13.17.03.png (698×2 px, 487 KB)

You can check the Figma explorations.

AnneT renamed this task from Implement characters counter to Field: Implement characters counter.Mar 2 2023, 1:37 PM
kostajh updated the task description. (Show Details)
kostajh removed a subscriber: KieranMcCann.

@kostajh thank you for adding real use cases for this proposed improvement. I want to clarify if there is an expectation from Trust & Safety Tools for the character count to be implemented in the TextArea ahead of T337566: [EPIC]: Incident Reporting System - Minimal Testable Product (MTP). It wasn't totally clear to me based on this comment if the near-term plan is to implement the counter yourself or to implement it / have it implemented in Codex.

@kostajh thank you for adding real use cases for this proposed improvement. I want to clarify if there is an expectation from Trust & Safety Tools for the character count to be implemented in the TextArea ahead of T337566: [EPIC]: Incident Reporting System - Minimal Testable Product (MTP). It wasn't totally clear to me based on this comment if the near-term plan is to implement the counter yourself or to implement it / have it implemented in Codex.

I would be happy if it's something that DST wants to take on so that all consumers of the component benefit, but understand that it's unplanned work for your team. If you intend to work on it in the next month or two, then we'd wait for your implementation, otherwise we would figure out something ourselves.

@kostajh thank you for adding real use cases for this proposed improvement. I want to clarify if there is an expectation from Trust & Safety Tools for the character count to be implemented in the TextArea ahead of T337566: [EPIC]: Incident Reporting System - Minimal Testable Product (MTP). It wasn't totally clear to me based on this comment if the near-term plan is to implement the counter yourself or to implement it / have it implemented in Codex.

I would be happy if it's something that DST wants to take on so that all consumers of the component benefit, but understand that it's unplanned work for your team. If you intend to work on it in the next month or two, then we'd wait for your implementation, otherwise we would figure out something ourselves.

I know this is always my default response, but it sort of has to be 😁: ideally T&ST upstreams this to Codex if you plan on implementing it anyway? I think there is some challenge here based on what you're proposing in T338817#9133502, since the Codex implementation should be MediaWiki agnostic. I'm open to us setting up a time to at least chat through some options. In the worst case, your PIRS-specific implementation could be upstreamed later, or replaced with the eventual Codex version.

I would be happy if it's something that DST wants to take on so that all consumers of the component benefit, but understand that it's unplanned work for your team. If you intend to work on it in the next month or two, then we'd wait for your implementation, otherwise we would figure out something ourselves.

Hi @kostajh – we discussed this use-case today in the DST design/engineering meeting. I don't think that any changes are actually required in Codex to support the behavior that you need. Instead of us introducing some new functionality into specific Codex components, we can provide a demo (complete with code sample) that you all should be able to adapt to the specific needs of your project (and which may be useful to other teams in the future). Feel free to then CC one of the DST engineers on the relevant patch in your project and we can provide a little bit of code review assistance if necessary.

How does that sound? We should be able to publish such an example in a in a few days (maybe next week at some point). I can link here when it's live.

I would be happy if it's something that DST wants to take on so that all consumers of the component benefit, but understand that it's unplanned work for your team. If you intend to work on it in the next month or two, then we'd wait for your implementation, otherwise we would figure out something ourselves.

Hi @kostajh – we discussed this use-case today in the DST design/engineering meeting. I don't think that any changes are actually required in Codex to support the behavior that you need. Instead of us introducing some new functionality into specific Codex components, we can provide a demo (complete with code sample) that you all should be able to adapt to the specific needs of your project (and which may be useful to other teams in the future). Feel free to then CC one of the DST engineers on the relevant patch in your project and we can provide a little bit of code review assistance if necessary.

How does that sound? We should be able to publish such an example in a in a few days (maybe next week at some point). I can link here when it's live.

Sure, that works, thanks!

for what is worth, we have a (front-end only) implementation on wikifunctions, it would be generally helpful to have standard solution, e.g. a prop for character count for all valid text inputs.

CleanShot 2023-09-11 at 12.17.11@2x.png (1×1 px, 98 KB)

see also https://gerrit.wikimedia.org/r/plugins/gitiles/mediawiki/extensions/WikiLambda/+/refs/heads/master/resources/ext.wikilambda.edit/components/widgets/AboutEditMetadataDialog.vue#327

Ok – @kostajh and @AAlhazwani-WMF (and anyone else in need of similar functionality):

Demo

At this point in time, we recommend using the new Field component in Codex to implement this kind of a feature. You can use <CdxField> to wrap other components like <CdxTextArea>.

You can see an interactive example (complete with MediaWiki-compatible code samples) here: https://doc.wikimedia.org/codex/latest/components/demos/field.html#with-custom-help-text-content

Explanation

The Field component exposes a slot called #help-text which appears immediately below the content; by default any text here is a light gray color. Since this slot can hold essentially any kind of markup structure, you could place a message and a text counter side-by-side in a container that's styled with CSS Flexbox (so that the counter is always in the lower right corner, for example).

The Field component also accepts a status property, which can be set to either default or error. You can provide a messages prop to cause an error message to be displayed whenever the Field has the error status.

This example relies on two computed properties – the first computes the number of remaining characters (equal to max character limit minus current length of the user's text), and the second returns a status value ("default" if the remaining character count is zero or above, and "error" if it the number is negative).

In a real-world example you might disable a "submit" button or otherwise prevent submission when the Field is in the "error" state. You could also swap out the very basic character-counting logic with something more language-aware, but the basic structure would still hold.

Let me know if you have questions, and feel free to CC me on a patch if you implement this kind of a feature in your own code. At some point in the future we may introduce some kind of "official" counter into Codex, but for now this is probably the most straightforward way to add this functionality into your own applications.

Ok – @kostajh and @AAlhazwani-WMF (and anyone else in need of similar functionality):

Demo

At this point in time, we recommend using the new Field component in Codex to implement this kind of a feature. You can use <CdxField> to wrap other components like <CdxTextArea>.

You can see an interactive example (complete with MediaWiki-compatible code samples) here: https://doc.wikimedia.org/codex/latest/components/demos/field.html#with-custom-help-text-content

Thank you!

egardner claimed this task.

The Codex Field page now includes an interactive example (complete with copy/pastable code) of this character-counter pattern, so I'm going to mark this task as resolved.

Here's the demo: https://doc.wikimedia.org/codex/latest/components/demos/field.html#with-custom-help-text-content