├── public ├── favicon.ico ├── logo192.png ├── logo512.png ├── robots.txt ├── manifest.json └── index.html ├── src ├── index.js ├── components │ ├── Header.js │ ├── ColorSelect.js │ ├── Link.js │ ├── Route.js │ ├── Translate.js │ ├── Convert.js │ ├── Accordion.js │ ├── Search.js │ └── Dropdown.js └── App.js ├── .gitignore ├── package.json └── README.md /public/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ncoughlin/react-widgets/HEAD/public/favicon.ico -------------------------------------------------------------------------------- /public/logo192.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ncoughlin/react-widgets/HEAD/public/logo192.png -------------------------------------------------------------------------------- /public/logo512.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ncoughlin/react-widgets/HEAD/public/logo512.png -------------------------------------------------------------------------------- /public/robots.txt: -------------------------------------------------------------------------------- 1 | # https://www.robotstxt.org/robotstxt.html 2 | User-agent: * 3 | Disallow: 4 | -------------------------------------------------------------------------------- /src/index.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import ReactDOM from 'react-dom'; 3 | import App from './App'; 4 | 5 | ReactDOM.render(, document.querySelector('#root')); -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # See https://help.github.com/articles/ignoring-files/ for more about ignoring files. 2 | 3 | # dependencies 4 | /node_modules 5 | /.pnp 6 | .pnp.js 7 | 8 | # testing 9 | /coverage 10 | 11 | # production 12 | /build 13 | 14 | # misc 15 | .DS_Store 16 | .env.local 17 | .env.development.local 18 | .env.test.local 19 | .env.production.local 20 | 21 | npm-debug.log* 22 | yarn-debug.log* 23 | yarn-error.log* 24 | -------------------------------------------------------------------------------- /public/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "short_name": "React App", 3 | "name": "Create React App Sample", 4 | "icons": [ 5 | { 6 | "src": "favicon.ico", 7 | "sizes": "64x64 32x32 24x24 16x16", 8 | "type": "image/x-icon" 9 | }, 10 | { 11 | "src": "logo192.png", 12 | "type": "image/png", 13 | "sizes": "192x192" 14 | }, 15 | { 16 | "src": "logo512.png", 17 | "type": "image/png", 18 | "sizes": "512x512" 19 | } 20 | ], 21 | "start_url": ".", 22 | "display": "standalone", 23 | "theme_color": "#000000", 24 | "background_color": "#ffffff" 25 | } 26 | -------------------------------------------------------------------------------- /src/components/Header.js: -------------------------------------------------------------------------------- 1 | import React from "react"; 2 | 3 | import Link from "./Link"; 4 | 5 | const Header = () => { 6 | return ( 7 |
8 | 9 | Accordion 10 | 11 | 12 | Color Select 13 | 14 | 15 | Translate 16 | 17 | 18 | Wiki Search 19 | 20 | 21 | All Widgets 22 | 23 |
24 | ); 25 | }; 26 | 27 | export default Header; 28 | -------------------------------------------------------------------------------- /src/components/ColorSelect.js: -------------------------------------------------------------------------------- 1 | import React, { useState } from "react"; 2 | 3 | import Dropdown from "./Dropdown"; 4 | 5 | const colorOptions = [ 6 | { 7 | label: "The Color Red", 8 | value: "red", 9 | }, 10 | { 11 | label: "The Color Green", 12 | value: "green", 13 | }, 14 | { 15 | label: "The Color Blue", 16 | value: "blue", 17 | }, 18 | ]; 19 | 20 | const ColorSelect = () => { 21 | const [selected, setSelected] = useState(colorOptions[0]); 22 | 23 | return ( 24 |
25 | 31 |
32 | ); 33 | }; 34 | 35 | export default ColorSelect; 36 | -------------------------------------------------------------------------------- /src/components/Link.js: -------------------------------------------------------------------------------- 1 | import React from "react"; 2 | 3 | const Link = ({ className, href, children }) => { 4 | 5 | const onClick = (event) => { 6 | // if ctrl or meta key are held on click, allow default behavior of opening link in new tab 7 | if (event.metaKey || event.ctrlKey) { 8 | return; 9 | } 10 | 11 | // prevent full page reload 12 | event.preventDefault(); 13 | // update url 14 | window.history.pushState({}, "", href); 15 | 16 | // communicate to Routes that URL has changed 17 | const navEvent = new PopStateEvent('popstate'); 18 | window.dispatchEvent(navEvent); 19 | }; 20 | 21 | return ( 22 | 23 | {children} 24 | 25 | ); 26 | }; 27 | 28 | export default Link; 29 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "widgets", 3 | "version": "0.1.0", 4 | "private": true, 5 | "dependencies": { 6 | "@testing-library/jest-dom": "^4.2.4", 7 | "@testing-library/react": "^9.3.2", 8 | "@testing-library/user-event": "^7.1.2", 9 | "axios": "^0.19.2", 10 | "react": "^16.13.1", 11 | "react-dom": "^16.13.1", 12 | "react-scripts": "3.4.3" 13 | }, 14 | "scripts": { 15 | "start": "react-scripts start", 16 | "build": "react-scripts build", 17 | "test": "react-scripts test", 18 | "eject": "react-scripts eject" 19 | }, 20 | "eslintConfig": { 21 | "extends": "react-app" 22 | }, 23 | "browserslist": { 24 | "production": [ 25 | ">0.2%", 26 | "not dead", 27 | "not op_mini all" 28 | ], 29 | "development": [ 30 | "last 1 chrome version", 31 | "last 1 firefox version", 32 | "last 1 safari version" 33 | ] 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /src/App.js: -------------------------------------------------------------------------------- 1 | import React from "react"; 2 | 3 | import Header from "./components/Header"; 4 | import Route from "./components/Route"; 5 | import ColorSelect from "./components/ColorSelect"; 6 | import Translate from "./components/Translate"; 7 | import Accordion from "./components/Accordion"; 8 | import Search from "./components/Search"; 9 | 10 | export default () => { 11 | return ( 12 |
13 |
14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 |
33 | ); 34 | }; 35 | -------------------------------------------------------------------------------- /src/components/Route.js: -------------------------------------------------------------------------------- 1 | import { useEffect, useState } from 'react'; 2 | 3 | const Route = ({ path, children }) => { 4 | // state to track URL and force component to re-render on change 5 | const [currentPath, setCurrentPath] = useState(window.location.pathname); 6 | 7 | useEffect(() => { 8 | // define callback as separate function so it can be removed later with cleanup function 9 | const onLocationChange = () => { 10 | // update path state to current window URL 11 | setCurrentPath(window.location.pathname); 12 | } 13 | 14 | // listen for popstate event 15 | window.addEventListener('popstate', onLocationChange); 16 | 17 | // clean up event listener 18 | return () => { 19 | window.removeEventListener('popstate', onLocationChange) 20 | }; 21 | }, []) 22 | 23 | return currentPath === path 24 | ? children 25 | : null; 26 | } 27 | 28 | export default Route; -------------------------------------------------------------------------------- /src/components/Translate.js: -------------------------------------------------------------------------------- 1 | import React, { useState } from "react"; 2 | 3 | import Dropdown from "./Dropdown"; 4 | import Convert from './Convert'; 5 | 6 | const languages = [ 7 | { 8 | label: "Afrikaans", 9 | value: "af", 10 | }, 11 | { 12 | label: "Arabic", 13 | value: "ar", 14 | }, 15 | { 16 | label: "Hindi", 17 | value: "hi", 18 | }, 19 | ]; 20 | 21 | const Translate = () => { 22 | const [language, setLanguage] = useState(languages[0]); 23 | const [text, setText] = useState(""); 24 | 25 | return ( 26 |
27 |
28 |
29 | 30 | setText(e.target.value)} /> 31 |
32 |
33 | 34 | 40 |
41 |

Output:

42 | 46 |
47 | ); 48 | }; 49 | 50 | export default Translate; 51 | -------------------------------------------------------------------------------- /src/components/Convert.js: -------------------------------------------------------------------------------- 1 | import React, { useState, useEffect } from "react"; 2 | import axios from "axios"; 3 | 4 | // de-structure language and text props 5 | const Convert = ({ language, text }) => { 6 | const [results, setResults] = useState([]); 7 | const [debouncedText, setDebouncedText] = useState(text); 8 | 9 | // de-bouncing the search term 10 | // runs every time the text changes 11 | useEffect(() => { 12 | const timerId = setTimeout(() => { 13 | setDebouncedText(text); 14 | }, 500); 15 | 16 | return () => { 17 | clearTimeout(timerId); 18 | }; 19 | }, [text]); 20 | 21 | // runs every time language or text updates 22 | useEffect(() => { 23 | const translate = async () => { 24 | const translation = await axios.post( 25 | "https://translation.googleapis.com/language/translate/v2", 26 | {}, 27 | { 28 | params: { 29 | q: debouncedText, 30 | target: language.value, 31 | key: "AIzaSyCHUCmpR7cT_yDFHC98CZJy2LTms-IwDlM", 32 | }, 33 | } 34 | ); 35 | 36 | setResults(translation.data.data.translations[0].translatedText); 37 | 38 | 39 | }; 40 | 41 | translate(); 42 | 43 | }, [language, debouncedText]); 44 | 45 | return ( 46 |
47 |

{results}

48 |
49 | ); 50 | }; 51 | 52 | export default Convert; 53 | -------------------------------------------------------------------------------- /public/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 12 | 13 | 14 | 18 | 19 | 28 | React App 29 | 30 | 31 | 32 |
33 | 43 | 44 | 45 | -------------------------------------------------------------------------------- /src/components/Accordion.js: -------------------------------------------------------------------------------- 1 | import React, { useState } from "react"; 2 | 3 | const items = [ 4 | { 5 | title: "Excepteur sint occaecat", 6 | content: 7 | "Morbi tristique senectus et netus et malesuada. Eleifend quam adipiscing vitae proin sagittis nisl rhoncus mattis rhoncus. Morbi tincidunt ornare massa eget egestas purus viverra. Ut tristique et egestas quis ipsum. Nunc faucibus a pellentesque sit amet porttitor eget. Praesent elementum facilisis leo vel fringilla est ullamcorper eget nulla. ", 8 | }, 9 | { 10 | title: "Duis aute irure dolor", 11 | content: 12 | "Augue interdum velit euismod in pellentesque massa placerat. Id aliquet lectus proin nibh nisl condimentum id venenatis. Non enim praesent elementum facilisis leo vel fringilla. Semper eget duis at tellus at urna condimentum mattis pellentesque. ", 13 | }, 14 | { 15 | title: "Ut enim ad minim", 16 | content: 17 | "Lacus luctus accumsan tortor posuere ac ut consequat semper. Suscipit adipiscing bibendum est ultricies integer quis auctor. Nunc sed velit dignissim sodales ut. Vestibulum morbi blandit cursus risus at ultrices mi. Netus et malesuada fames ac turpis egestas integer.", 18 | }, 19 | ]; 20 | 21 | const Accordion = () => { 22 | const [activeTab, setActiveTab] = useState(null); 23 | 24 | const onTitleClick = (index) => { 25 | setActiveTab(index); 26 | }; 27 | 28 | const renderedItems = items.map((item, index) => { 29 | // if current index is same as active tab, add class "active" 30 | const activeStatus = index === activeTab ? 'active' : ''; 31 | 32 | return ( 33 | 34 |
onTitleClick(index)}> 35 | 36 | {item.title} 37 |
38 |
39 |

{item.content}

40 |
41 |
42 | ); 43 | }); 44 | 45 | return ( 46 |
47 | {renderedItems} 48 |
49 | ); 50 | }; 51 | 52 | export default Accordion; 53 | -------------------------------------------------------------------------------- /src/components/Search.js: -------------------------------------------------------------------------------- 1 | import React, { useState, useEffect } from "react"; 2 | import axios from "axios"; 3 | 4 | const Search = () => { 5 | const [term, setTerm] = useState("React"); 6 | const [debouncedTerm, setDebouncedTerm] = useState(term); 7 | const [results, setResults] = useState([]); 8 | 9 | // de-bouncing the search term 10 | useEffect(() => { 11 | const timerId = setTimeout(() => { 12 | setDebouncedTerm(term); 13 | }, 500); 14 | return () => { 15 | clearTimeout(timerId); 16 | }; 17 | }, [term]); 18 | 19 | useEffect(() => { 20 | const search = async () => { 21 | const { data } = await axios.get("https://en.wikipedia.org/w/api.php", { 22 | params: { 23 | action: "query", 24 | list: "search", 25 | origin: "*", 26 | format: "json", 27 | srsearch: debouncedTerm, 28 | }, 29 | }); 30 | setResults(data.query.search); 31 | }; 32 | 33 | // do not search if no term 34 | if (debouncedTerm) { 35 | search(); 36 | } 37 | 38 | }, [debouncedTerm]); 39 | 40 | const searchResultsMapped = results.map((result) => { 41 | return ( 42 |
43 |
44 | 50 | Read Article 51 | 52 |
53 |
54 |
{result.title}
55 | 56 |
57 |
58 | ); 59 | }); 60 | 61 | return ( 62 |
63 |
64 |
65 | 66 | setTerm(e.target.value)} 70 | /> 71 |
72 |
73 |
{searchResultsMapped}
74 |
75 | ); 76 | }; 77 | 78 | export default Search; 79 | -------------------------------------------------------------------------------- /src/components/Dropdown.js: -------------------------------------------------------------------------------- 1 | import React, { useState, useEffect, useRef } from "react"; 2 | 3 | const Dropdown = ({ label, options, selected, onSelectedChange }) => { 4 | // state to manage toggle visibility 5 | const [open, setOpen] = useState(false); 6 | // set ref variable 7 | const ref = useRef(); 8 | 9 | // close dropdown if clicked anywhere outside of dropdown 10 | // on initial render, add click event listener 11 | useEffect(() => { 12 | const onBodyClick = (event) => { 13 | // check if element that was clicked is inside of ref'd component 14 | // if so no action is required from this event listener so exit 15 | if (ref.current.contains(event.target)) { 16 | return; 17 | } 18 | // else close the dropdown 19 | setOpen(false); 20 | }; 21 | 22 | document.body.addEventListener("click", onBodyClick); 23 | 24 | // CLEANUP 25 | // remove event listener 26 | return () => { 27 | document.body.removeEventListener("click", onBodyClick); 28 | }; 29 | }, []); 30 | 31 | // map options from props 32 | const renderedOptions = options.map((option) => { 33 | // if current selection is equal to option do not generate div 34 | if (option.value === selected.value) { 35 | return null; 36 | } 37 | 38 | return ( 39 |
onSelectedChange(option)} 44 | > 45 | {option.label} 46 |
47 | ); 48 | }); 49 | 50 | return ( 51 |
52 |
53 | 54 |
setOpen(!open)} 57 | className={`ui selection dropdown ${open ? "visible active" : ""}`} 58 | > 59 | 60 |
{selected.label}
61 |
setOpen(!open)} 64 | className={`menu ${open ? "visible transition" : ""}`} 65 | > 66 | {renderedOptions} 67 |
68 |
69 |
70 |
71 | ); 72 | }; 73 | 74 | export default Dropdown; 75 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app). 2 | 3 | ## Available Scripts 4 | 5 | In the project directory, you can run: 6 | 7 | ### `yarn start` 8 | 9 | Runs the app in the development mode.
10 | Open [http://localhost:3000](http://localhost:3000) to view it in the browser. 11 | 12 | The page will reload if you make edits.
13 | You will also see any lint errors in the console. 14 | 15 | ### `yarn test` 16 | 17 | Launches the test runner in the interactive watch mode.
18 | See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information. 19 | 20 | ### `yarn build` 21 | 22 | Builds the app for production to the `build` folder.
23 | It correctly bundles React in production mode and optimizes the build for the best performance. 24 | 25 | The build is minified and the filenames include the hashes.
26 | Your app is ready to be deployed! 27 | 28 | See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information. 29 | 30 | ### `yarn eject` 31 | 32 | **Note: this is a one-way operation. Once you `eject`, you can’t go back!** 33 | 34 | If you aren’t satisfied with the build tool and configuration choices, you can `eject` at any time. This command will remove the single build dependency from your project. 35 | 36 | Instead, it will copy all the configuration files and the transitive dependencies (webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point you’re on your own. 37 | 38 | You don’t have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldn’t feel obligated to use this feature. However we understand that this tool wouldn’t be useful if you couldn’t customize it when you are ready for it. 39 | 40 | ## Learn More 41 | 42 | You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started). 43 | 44 | To learn React, check out the [React documentation](https://reactjs.org/). 45 | 46 | ### Code Splitting 47 | 48 | This section has moved here: https://facebook.github.io/create-react-app/docs/code-splitting 49 | 50 | ### Analyzing the Bundle Size 51 | 52 | This section has moved here: https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size 53 | 54 | ### Making a Progressive Web App 55 | 56 | This section has moved here: https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app 57 | 58 | ### Advanced Configuration 59 | 60 | This section has moved here: https://facebook.github.io/create-react-app/docs/advanced-configuration 61 | 62 | ### Deployment 63 | 64 | This section has moved here: https://facebook.github.io/create-react-app/docs/deployment 65 | 66 | ### `yarn build` fails to minify 67 | 68 | This section has moved here: https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify 69 | --------------------------------------------------------------------------------