|
| 1 | +import React from 'react'; |
| 2 | +import PropTypes from 'prop-types'; |
| 3 | +import Waypoint from 'react-waypoint'; |
| 4 | +import OutputTemplate from './OutputTemplate.jsx'; |
| 5 | +import { breakpoint, breakpointsObj as bp } from '../utils/breakpoints'; |
| 6 | +import { colors } from '../utils/colors'; |
| 7 | +import { weight } from '../utils/typography'; |
| 8 | +import Table from '../Table.jsx'; |
| 9 | + |
| 10 | +const tableTheme = { |
| 11 | + row_two: { |
| 12 | + maxWidth: '300px', |
| 13 | + [breakpoint(bp.SMALL)]: { |
| 14 | + maxWidth: '450px', |
| 15 | + borderBottom: `1px solid ${colors.WARM_GRAY}`, |
| 16 | + paddingBottom: '1rem', |
| 17 | + marginBottom: '1rem', |
| 18 | + }, |
| 19 | + }, |
| 20 | + row_header_two: { |
| 21 | + [breakpoint(bp.SMALL)]: { |
| 22 | + borderBottom: 'none', |
| 23 | + }, |
| 24 | + }, |
| 25 | + cell_header_two: { |
| 26 | + fontWeight: weight.MEDIUM, |
| 27 | + color: colors.COOL_GRAY, |
| 28 | + ':first-child': { |
| 29 | + fontWeight: weight.MEDIUM, |
| 30 | + color: colors.COOL_GRAY, |
| 31 | + }, |
| 32 | + ':nth-of-type(2)': { |
| 33 | + fontWeight: weight.MEDIUM, |
| 34 | + color: colors.COOL_GRAY, |
| 35 | + }, |
| 36 | + }, |
| 37 | + cell_two: { |
| 38 | + ':first-child': { |
| 39 | + color: colors.PRIMARY_LIGHT, |
| 40 | + fontWeight: weight.MEDIUM, |
| 41 | + }, |
| 42 | + ':nth-of-type(2)': { |
| 43 | + fontWeight: weight.MEDIUM, |
| 44 | + color: colors.DARK_GRAY, |
| 45 | + }, |
| 46 | + ':before': { |
| 47 | + width: '5rem', |
| 48 | + fontWeight: weight.MEDIUM, |
| 49 | + color: colors.COOL_GRAY, |
| 50 | + }, |
| 51 | + }, |
| 52 | + inner_two: { |
| 53 | + width: 'calc(100% - 5rem)', |
| 54 | + }, |
| 55 | +}; |
| 56 | + |
| 57 | +export default React.createClass({ |
| 58 | + displayName: 'Syntax', |
| 59 | + |
| 60 | + propTypes: { |
| 61 | + data: PropTypes.arrayOf(PropTypes.shape({ |
| 62 | + text: PropTypes.string, |
| 63 | + part_of_speech: PropTypes.string, |
| 64 | + lemma: PropTypes.string, |
| 65 | + })), |
| 66 | + language: PropTypes.string, |
| 67 | + }, |
| 68 | + |
| 69 | + getInitialState() { |
| 70 | + return { |
| 71 | + showJson: false, |
| 72 | + visibleItems: 10, |
| 73 | + }; |
| 74 | + }, |
| 75 | + |
| 76 | + toggleJson() { |
| 77 | + this.setState({ showJson: !this.state.showJson }); |
| 78 | + }, |
| 79 | + |
| 80 | + onWaypoint() { |
| 81 | + if (this.state.visibleItems < this.props.data.length) { |
| 82 | + setTimeout(() => { |
| 83 | + this.setState({ |
| 84 | + visibleItems: this.state.visibleItems + 10, |
| 85 | + }); |
| 86 | + }, 150); |
| 87 | + } |
| 88 | + }, |
| 89 | + |
| 90 | + render() { |
| 91 | + return ( |
| 92 | + <div> |
| 93 | + <OutputTemplate |
| 94 | + description={<p className="base--p_small">Identify <a href="https://www.ibm.com/watson/developercloud/natural-language-understanding/api/v1/#syntax" target="_blank" rel="noopener noreferrer">tokens</a>, part of speech, sentence boundaries and lemmas in the text</p>} |
| 95 | + data={{ syntax: this.props.data }} |
| 96 | + showJson={this.state.showJson} |
| 97 | + onExitJson={this.toggleJson} |
| 98 | + onShowJson={this.toggleJson} |
| 99 | + > |
| 100 | + {console.log(this.props.data)} |
| 101 | + {this.props.data && this.props.data.length > 0 ? ( |
| 102 | + <div> |
| 103 | + <Table |
| 104 | + columns={['Token', 'Part of Speech', 'Lemma']} |
| 105 | + theme={tableTheme} |
| 106 | + data={this.props.data.reduce((acc, item) => { |
| 107 | + acc.push({ Token: item.text, |
| 108 | + 'Part of Speech': item.part_of_speech, |
| 109 | + Lemma: item.lemma }); |
| 110 | + return acc; |
| 111 | + }, []).filter((val, i) => i <= this.state.visibleItems)} |
| 112 | + /> |
| 113 | + <Waypoint onEnter={this.onWaypoint} /> |
| 114 | + </div> |
| 115 | + ) : ( |
| 116 | + <p>{`No Syntax results returned for ${this.props.language} input.`}</p> |
| 117 | + )} |
| 118 | + </OutputTemplate> |
| 119 | + </div> |
| 120 | + ); |
| 121 | + }, |
| 122 | +}); |
0 commit comments