reactjs / reactjs/react.dev

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

オープン
#1,550 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

主要言語
JavaScript
スター
11.8k
フォーク
7.9k
平均マージ
1日 11時間
マージ済み PR(30日)
11

説明

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.

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

Issue にリンクされている React DOM elements ドキュメントの suppressContentEditableWarning セクションから始め、説明されているフォーカスとタブ順序のユースケースに照らして、提案された文言を確認します。ドキュメントのガイダンスを確認し、適切であればそのセクションを更新します。サポートされているユースケースと注意点を正確に説明できれば完了です。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
javascript, react
領域
documentation
issue の種類
ドキュメント
難易度
2/5
見積もり時間
1〜3時間
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
35/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。