reactjs / reactjs/react.dev

Is wanting a div to be focus-able a good use case for contentEditable?

Aperta
#1,550 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Lingua principale
JavaScript
Stelle
11.8k
Fork
7.9k
Merge medio
1g 11h
PR unite (30g)
11

Descrizione

The documentation says

suppressContentEditableWarning
Normally, there is a warning when an element with children is also marked as contentEditable, because it won’t work. This attribute suppresses that warning. Don’t use this unless you are building a library like Draft.js that manages contentEditable manually.

Well I'm not building a library which manages contentEditable manually, but I think I have a situation that contentEditable makes sense for that - if yall agree - might be good to update the documentation for.

I am building a sort of inline-editable component which will toggle between "view" and "editable" versions of their rendered contents. So my render method looks like this

  render = () => (
    <EditableContainer className={classnames({ 'has-spinner inline': this.state.saving })}>
      {this.state.editing ? (
        this.props.renderEdit({
          value: this.state.currentValue,
          onChange: (currentValue) => this.setState({currentValue}),
          onKeyDown: this.onKeyDown,
          onBlur: this.save,
          ref: this.editRef,
        })
      ) : (
        <div contentEditable suppressContentEditableWarning onFocus={this.transitionToEdit}>
          {this.props.renderView({value: this.props.value})}
        </div>
      )}
    </EditableContainer>
  );

  transitionToEdit = () => {
    this.setState({editing: true, currentValue: this.props.value}, () => {
      if(this.editRef.current)
        this.editRef.current.focus();
    });
  }

Note that I wrap the "view" component in a contentEditable div, this is primarily so that it can obtain focus and is in the natural tab order. Once it is tabbed to, I actually no longer render it, instead switching directly to the "editable view". So I'm pretty sure the warning is moot since - nothing can actually happen within the contentEditable, it can trigger and...that's about all, however it is needed in order to properly allow focus without manually managing tabIndex.

Is this a valid use case? If so, I would recommend updating the documentation to say something like

Don’t use this unless you are building a library like Draft.js that manages contentEditable manually. You may also use it to allow focus to shift to otherwise unfocusable element but beware that actually using the contentEditable component might step you outside the bounds of what React can properly control.

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia dalla sezione suppressContentEditableWarning della documentazione React DOM elements collegata nell’issue e verifica la formulazione proposta rispetto al caso d’uso descritto relativo al focus e all’ordine di tabulazione. Conferma le indicazioni della documentazione e aggiorna la sezione se appropriato; il lavoro è completato quando spiega accuratamente il caso d’uso supportato e la relativa limitazione.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
javascript, react
Ambito
documentation
Tipo di issue
Documentazione
Difficoltà
2/5
Tempo stimato
1-3 ore
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
35/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.