reactjs / reactjs/react.dev

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

Offen
#1,550 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Vorherrschende Sprache
JavaScript
Sterne
11.8k
Forks
7.9k
Ø Merge
1 T. 11 Std.
Gemergte PRs (30 T.)
11

Beschreibung

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.

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginne mit dem Abschnitt suppressContentEditableWarning in der im Issue verlinkten React-DOM-elements-Dokumentation und prüfe die vorgeschlagene Formulierung anhand des beschriebenen Anwendungsfalls zu Fokus und Tab-Reihenfolge. Bestätige die Dokumentationshinweise und aktualisiere den Abschnitt, falls angebracht; abgeschlossen, wenn er den unterstützten Anwendungsfall und den Vorbehalt korrekt erklärt.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
javascript, react
Bereich
documentation
Issue-Typ
Dokumentation
Schwierigkeit
2/5
Geschätzter Aufwand
1-3 Stunden
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
35/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.