|
| 1 | +import { Button, Classes, MenuItem } from '@blueprintjs/core'; |
| 2 | +import { IconNames } from '@blueprintjs/icons'; |
| 3 | +import { ItemRenderer, Select } from '@blueprintjs/select'; |
| 4 | + |
| 5 | +import * as React from 'react'; |
| 6 | + |
| 7 | +import { RouteComponentProps } from 'react-router'; |
| 8 | + |
| 9 | +import { Variant } from 'js-slang/dist/types'; |
| 10 | +import { ISourceLanguage, sourceLanguages, styliseChapter } from '../../reducers/states'; |
| 11 | + |
| 12 | +export interface IChapterProps extends IDispatchProps, IStateProps, RouteComponentProps<{}> {} |
| 13 | + |
| 14 | +export type IDispatchProps = { |
| 15 | + handleFetchChapter: () => void; |
| 16 | + handleUpdateChapter: (chapter: IChapter) => void; |
| 17 | + handleChapterSelect?: (i: IChapter, e: React.ChangeEvent<HTMLSelectElement>) => void; |
| 18 | +}; |
| 19 | + |
| 20 | +export interface IStateProps { |
| 21 | + sourceChapter: number; |
| 22 | + sourceVariant: Variant; |
| 23 | +} |
| 24 | + |
| 25 | +export interface IChapter { |
| 26 | + chapter: number; |
| 27 | + variant: Variant; |
| 28 | + displayName: string; |
| 29 | +} |
| 30 | + |
| 31 | +export function DefaultChapter(props: IChapterProps) { |
| 32 | + props.handleFetchChapter(); |
| 33 | + |
| 34 | + const chapters = sourceLanguages.map((lang: ISourceLanguage) => { |
| 35 | + return { |
| 36 | + chapter: lang.chapter, |
| 37 | + variant: lang.variant, |
| 38 | + displayName: styliseChapter(lang.chapter, lang.variant) |
| 39 | + }; |
| 40 | + }); |
| 41 | + |
| 42 | + const chapterRenderer: ItemRenderer<IChapter> = (lang, { handleClick }) => ( |
| 43 | + <MenuItem |
| 44 | + active={false} |
| 45 | + key={lang.chapter + lang.variant} |
| 46 | + onClick={handleClick} |
| 47 | + text={lang.displayName} |
| 48 | + /> |
| 49 | + ); |
| 50 | + |
| 51 | + const ChapterSelectComponent = Select.ofType<IChapter>(); |
| 52 | + |
| 53 | + const chapSelect = ( |
| 54 | + currentChap: number, |
| 55 | + currentVariant: Variant, |
| 56 | + handleSelect = (i: IChapter) => {} |
| 57 | + ) => ( |
| 58 | + <ChapterSelectComponent |
| 59 | + className={Classes.MINIMAL} |
| 60 | + items={chapters} |
| 61 | + onItemSelect={handleSelect} |
| 62 | + itemRenderer={chapterRenderer} |
| 63 | + filterable={false} |
| 64 | + > |
| 65 | + <Button |
| 66 | + className={Classes.MINIMAL} |
| 67 | + text={styliseChapter(currentChap, currentVariant)} |
| 68 | + rightIcon={IconNames.DOUBLE_CARET_VERTICAL} |
| 69 | + /> |
| 70 | + </ChapterSelectComponent> |
| 71 | + ); |
| 72 | + |
| 73 | + return ( |
| 74 | + <div> {chapSelect(props.sourceChapter, props.sourceVariant, props.handleUpdateChapter)} </div> |
| 75 | + ); |
| 76 | +} |
0 commit comments