import React from 'react'; import { ContentRect, default as Measure } from 'react-measure'; import { VariableSizeList } from 'react-window'; import classNames from 'classnames'; import { formatDexNumber } from 'app/utils/formatter'; import { IPokemon } from 'app/models/Pokemon'; import * as styles from './styles/PokemonSelectList.scss'; export interface IPokemonSelectListProps { isLoading : boolean; activePokemonId : string | null; pokemonList : Array; filterTerm : string; handleActivatePokemon : (pokemonId : string) => void; handleChangeFilter : (filterTerm : string) => Promise; } interface IState { dimensions : { width : number; height : number; }; } interface IRowFactory { index : number; style : React.CSSProperties; } export class PokemonSelectList extends React.Component { private listRef : React.RefObject; constructor(props : IPokemonSelectListProps) { super(props); this.state = { dimensions: { width: -1, height: -1, } }; this.listRef = React.createRef(); } public render() { const { width, height } = this.state.dimensions; const listLength = this.props.pokemonList.length; const onResize = (contentRect : ContentRect) => { if (typeof contentRect.bounds !== 'undefined') { this.setState({ dimensions: contentRect.bounds }); } }; const wrapperCss = classNames( styles.leftPanel, { loading: this.props.isLoading, } ); const listWrapperCss = classNames( 'nes-container', styles.listWrapper, { [ styles.emptyList ]: listLength === 0 } ); const inputTextCss = classNames( 'nes-input', styles.filterInput ); return (
{ listLength > 0 && { ({ measureRef }) => (
{ this.rowFactory.bind(this) }
) }
} { listLength === 0 &&

MissingNo.

}
); } private readonly getListItemKey = (index : number) => { return index + this.props.pokemonList[index].id; } private readonly calculateRowHeight = (index : number) => { return this.props.pokemonList[index].form !== null ? 40 : 25; } private rowFactory({ index, style } : IRowFactory) { const pokemon = this.props.pokemonList[index]; const dex = formatDexNumber(pokemon.dex); const anchorCss = classNames( 'list-item', { active: this.props.activePokemonId === pokemon.id } ); const dexCss = classNames( 'de-emphasize', styles.dex ); const formCss = classNames( 'de-emphasize', styles.form ); const onClick = () => this.props.handleActivatePokemon(pokemon.id); return ( { pokemon.name } #{ dex } { pokemon.form && { pokemon.form.toLowerCase().replace('_', ' ') } Forme } ); } private readonly handleChangeFilter = (event : React.ChangeEvent) => { this.props.handleChangeFilter(event.currentTarget.value) .then(() => { if (this.listRef.current !== null) { this.listRef.current.resetAfterIndex(0, true); } }); } }