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

Refs: Upgrade to React 16.3 version. #3

Merged
merged 1 commit into from Apr 1, 2018

Conversation

Projects
None yet
2 participants
@AlmeroSteyn
Contributor

AlmeroSteyn commented Apr 1, 2018

Really awesome example of using React to create an accessible HTML table!!

Thank you so much for taking the time to pusblish this. It is pure gold!

Spotted the use of a string ref. String refs should be treated as deprecated Legacy API: String Refs.

In this pull request I upgraded the example to use the new refs API from the freshly released React 16.3.

However, should this example be required for older versions of React the callback refs pattern should be used over string refs:

<div
    className="table-container"
    ref={container => { this.container = container }}
    tabIndex={this.state.tabindex}
    aria-labelledby={captionID}
>

Full code:

class Table extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      tabindex: null,
      rows: props.rows,
      sortedBy: null,
      sortDir: 'none'
    };
    this.sortBy = this.sortBy.bind(this);
  }

  sortBy(i) {
   //....
  }

  componentDidMount() {
    const {scrollWidth, clientWidth} = this.container;
    let scrollable = scrollWidth > clientWidth;
    this.setState({
      tabindex: scrollable ? '0' : null
    });
  }
  render() {
    const captionID = 'caption-' + Math.random().toString(36).substr(2, 9);
    return (
      <div>
        <div
          className="table-container"
          ref={container => { this.container = container }}
          tabIndex={this.state.tabindex}
          aria-labelledby={captionID}
        >
          <table>
            <caption id={captionID}>
              {this.props.caption}
              {this.state.tabindex === '0' &&
                <div>
                  <small>(scroll to see more)</small>
                </div>
              }
            </caption>
            <tbody>
              <tr>
                {this.props.headers.map((header, i) =>
                  <th
                    role="columnheader"
                    scope="col"
                    key={i}
                    aria-sort={this.state.sortedBy === i ? this.state.sortDir : 'none'}>
                    {header}
                    {this.props.sortable &&
                      <button
                        aria-label={`sort by ${header} in ${this.state.sortDir !== 'ascending' ? 'ascending' : 'descending'} order`}
                        onClick={() => this.sortBy(i)}>
                        <Arrow
                          sortDir={this.state.sortDir}
                          isCurrent={this.state.sortedBy === i}
                        />
                      </button>
                    }
                  </th>
                )}
              </tr>
              {this.state.rows.map((row, i) =>
                <tr key={i}>
                  {row.map((cell, i) =>
                    (this.props.rowHeaders && i < 1) ? (
                      <th scope="row" key={i}>{cell}</th>
                    ) : (
                      <td key={i}>{cell}</td>
                    )
                  )}
                </tr>
               )}
             </tbody>
          </table>
        </div>
        <div className="lists-container">
          <h2>{this.props.caption}</h2>
          {this.props.rows.map((row, i) =>
            <div key={i}>
              <h3>{row[0]}</h3>
              <dl>
                {this.props.headers.map((header, i) =>
                  i > 0 &&
                  <React.Fragment key={i}>
                    <dt>{header}</dt>
                    <dd>{row[i]}</dd>
                  </React.Fragment>
                )}
              </dl>
            </div>
          )}
        </div>
      </div>
    );
  }
}

@Heydon Heydon merged commit fc3d1a1 into Heydon:master Apr 1, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment