feat(select): initial commit
This commit is contained in:
parent
ce4671c2ba
commit
6008ff4eed
|
@ -0,0 +1,6 @@
|
|||
{
|
||||
"presets": [
|
||||
"@babel/preset-env",
|
||||
"@babel/preset-react"
|
||||
]
|
||||
}
|
|
@ -0,0 +1,108 @@
|
|||
# Others
|
||||
package-lock.json
|
||||
build*
|
||||
|
||||
# Logs
|
||||
logs
|
||||
*.log
|
||||
npm-debug.log*
|
||||
yarn-debug.log*
|
||||
yarn-error.log*
|
||||
lerna-debug.log*
|
||||
|
||||
# Diagnostic reports (https://nodejs.org/api/report.html)
|
||||
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
|
||||
|
||||
# Runtime data
|
||||
pids
|
||||
*.pid
|
||||
*.seed
|
||||
*.pid.lock
|
||||
|
||||
# Directory for instrumented libs generated by jscoverage/JSCover
|
||||
lib-cov
|
||||
|
||||
# Coverage directory used by tools like istanbul
|
||||
coverage
|
||||
*.lcov
|
||||
|
||||
# nyc test coverage
|
||||
.nyc_output
|
||||
|
||||
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
|
||||
.grunt
|
||||
|
||||
# Bower dependency directory (https://bower.io/)
|
||||
bower_components
|
||||
|
||||
# node-waf configuration
|
||||
.lock-wscript
|
||||
|
||||
# Compiled binary addons (https://nodejs.org/api/addons.html)
|
||||
build/Release
|
||||
|
||||
# Dependency directories
|
||||
node_modules/
|
||||
jspm_packages/
|
||||
|
||||
# TypeScript v1 declaration files
|
||||
typings/
|
||||
|
||||
# TypeScript cache
|
||||
*.tsbuildinfo
|
||||
|
||||
# Optional npm cache directory
|
||||
.npm
|
||||
|
||||
# Optional eslint cache
|
||||
.eslintcache
|
||||
|
||||
# Microbundle cache
|
||||
.rpt2_cache/
|
||||
.rts2_cache_cjs/
|
||||
.rts2_cache_es/
|
||||
.rts2_cache_umd/
|
||||
|
||||
# Optional REPL history
|
||||
.node_repl_history
|
||||
|
||||
# Output of 'npm pack'
|
||||
*.tgz
|
||||
|
||||
# Yarn Integrity file
|
||||
.yarn-integrity
|
||||
|
||||
# dotenv environment variables file
|
||||
.env
|
||||
.env.test
|
||||
|
||||
# parcel-bundler cache (https://parceljs.org/)
|
||||
.cache
|
||||
|
||||
# Next.js build output
|
||||
.next
|
||||
|
||||
# Nuxt.js build / generate output
|
||||
.nuxt
|
||||
dist
|
||||
|
||||
# Gatsby files
|
||||
.cache/
|
||||
# Comment in the public line in if your project uses Gatsby and *not* Next.js
|
||||
# https://nextjs.org/blog/next-9-1#public-directory-support
|
||||
# public
|
||||
|
||||
# vuepress build output
|
||||
.vuepress/dist
|
||||
|
||||
# Serverless directories
|
||||
.serverless/
|
||||
|
||||
# FuseBox cache
|
||||
.fusebox/
|
||||
|
||||
# DynamoDB Local files
|
||||
.dynamodb/
|
||||
|
||||
# TernJS port file
|
||||
.tern-port
|
|
@ -0,0 +1,15 @@
|
|||
# React.js Example
|
||||
|
||||
## Table of Contents
|
||||
0. [General](#general)
|
||||
1. [Links](#links)
|
||||
|
||||
# General
|
||||
|
||||
# Links
|
||||
|
||||
* [React setup with webpack for beginners](https://dev.to/deepanjangh/react-setup-with-webpack-for-beginners-2a8k)
|
||||
* [Production](https://webpack.js.org/guides/production/)
|
||||
* [Setup Development and Production Environment for React App](https://medium.com/freestoneinfotech/setup-development-and-production-environment-for-react-app-397c4cc9e382)
|
||||
* [HtmlWebpackPlugin](https://webpack.js.org/plugins/html-webpack-plugin/)
|
||||
* [React Dynamic Dropdown List with HTTP Request Tutorial](https://remotestack.io/react-dynamic-dropdown-list-with-http-request-tutorial/)
|
|
@ -0,0 +1,36 @@
|
|||
//generate an HTML5 file including all webpack bundles in the body using script tags
|
||||
const HtmlWebpackPlugin = require('html-webpack-plugin');
|
||||
//path is used to resolve properly across the OS
|
||||
const path = require('path');
|
||||
module.exports = {
|
||||
//bundle *.js from this entry point
|
||||
entry: path.resolve(__dirname, '../src/index.js'),
|
||||
//create output file to be linked to index.html
|
||||
output: {
|
||||
filename: '[name].bundle.js',
|
||||
path: path.resolve(__dirname, '../dist'),
|
||||
clean: true,
|
||||
},
|
||||
module: {
|
||||
rules: [
|
||||
{
|
||||
//test all *.js using babel-loader
|
||||
//test all *.jsx (e.g. React.js) using babel-loader
|
||||
test: /\.(js|jsx)$/,
|
||||
exclude: /node_modules/,
|
||||
include: path.resolve(__dirname, '../src'),
|
||||
use: ['babel-loader'],
|
||||
}
|
||||
]
|
||||
},
|
||||
resolve: {
|
||||
extensions: ['*', '.js', '.jsx'],
|
||||
},
|
||||
plugins: [
|
||||
// create an plugin instance so that you can use it several times anywhere
|
||||
new HtmlWebpackPlugin({
|
||||
title: 'Production',
|
||||
template: path.resolve(__dirname, "../public/index.html")
|
||||
}),
|
||||
],
|
||||
};
|
|
@ -0,0 +1,14 @@
|
|||
//path is used to resolve properly across the OS
|
||||
const path = require('path');
|
||||
const { merge } = require('webpack-merge');
|
||||
const common = require('./webpack.common.js');
|
||||
//merge() calls in the environment-specific configuration to include commons
|
||||
module.exports = merge(common, {
|
||||
//set development mode
|
||||
mode: 'development',
|
||||
//enable strong source mapping
|
||||
devtool: 'inline-source-map',
|
||||
devServer: {
|
||||
static: path.resolve(__dirname, '../dist'),
|
||||
},
|
||||
});
|
|
@ -0,0 +1,8 @@
|
|||
const { merge } = require('webpack-merge');
|
||||
const common = require('./webpack.common.js');
|
||||
module.exports = merge(common, {
|
||||
mode: 'production',
|
||||
//source maps encouraged in production
|
||||
//choose mapping with fairly quick build speed like source-map
|
||||
devtool: 'source-map',
|
||||
});
|
|
@ -0,0 +1,37 @@
|
|||
{
|
||||
"private": true,
|
||||
"name": "react-example",
|
||||
"description": "React.js example",
|
||||
"version": "0.0.1",
|
||||
"main": "index.js",
|
||||
"keywords": [
|
||||
"react",
|
||||
"webpack"
|
||||
],
|
||||
"author": "Software Ingenieur Begerad <dialog@SwIngBe.de>",
|
||||
"license": "GPL-3.0-or-later",
|
||||
"engines": {
|
||||
"node": ">=10"
|
||||
},
|
||||
"scripts": {
|
||||
"start": "webpack serve --open --config config/webpack.dev.js",
|
||||
"build": "webpack --config config/webpack.prod.js"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@babel/core": "^7.18.2",
|
||||
"@babel/preset-env": "^7.18.2",
|
||||
"@babel/preset-react": "^7.17.12",
|
||||
"babel-loader": "^8.2.5",
|
||||
"html-webpack-plugin": "^5.5.0",
|
||||
"webpack": "^5.73.0",
|
||||
"webpack-cli": "^4.9.2",
|
||||
"webpack-dev-server": "^4.9.2",
|
||||
"webpack-merge": "^5.8.0"
|
||||
},
|
||||
"dependencies": {
|
||||
"axios": "^0.27.2",
|
||||
"prop-types": "^15.8.1",
|
||||
"react": "^18.1.0",
|
||||
"react-dom": "^18.1.0"
|
||||
}
|
||||
}
|
|
@ -0,0 +1,10 @@
|
|||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>React Example</title>
|
||||
</head>
|
||||
<body>
|
||||
<div id="root"></div>
|
||||
</body>
|
||||
</html>
|
|
@ -0,0 +1,40 @@
|
|||
import React from 'react'
|
||||
import PropTypes from 'prop-types';
|
||||
|
||||
const DropDownSelect = (props) => {
|
||||
if(props.options){
|
||||
console.log('options len: '+props.options.length);
|
||||
return(
|
||||
<div>
|
||||
<strong>{props.name}</strong>:
|
||||
<select
|
||||
name="{props.name}"
|
||||
onChange={props.onChange}
|
||||
>
|
||||
<option defaultValue
|
||||
>
|
||||
Select {props.name}
|
||||
</option>
|
||||
{props.options.map((item, index) => (
|
||||
<option key={index} value={item.id}>
|
||||
{item.name}
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
</div>
|
||||
);
|
||||
}else{
|
||||
return (
|
||||
<div>
|
||||
<p>Select failed.</p>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
export default DropDownSelect
|
||||
|
||||
DropDownSelect.propTypes = {
|
||||
name: PropTypes.string,
|
||||
options: PropTypes.array,
|
||||
};
|
|
@ -0,0 +1,13 @@
|
|||
import React from 'react';
|
||||
import ReactDOM from 'react-dom';
|
||||
import Home from './pages/home';
|
||||
//TODO remove debugging
|
||||
if (process.env.NODE_ENV !== 'production') {
|
||||
console.log('development mode');
|
||||
}
|
||||
//since react 18
|
||||
import { createRoot } from 'react-dom/client';
|
||||
//create root container
|
||||
const root = createRoot(document.getElementById("root"));
|
||||
//render root app
|
||||
root.render(<Home />);
|
|
@ -0,0 +1,38 @@
|
|||
import React, { useEffect, useState } from 'react';
|
||||
import axios from 'axios';
|
||||
import DropDownSelect from '../components/drop-down-select';
|
||||
|
||||
const Home = () => {
|
||||
//handle state
|
||||
const [list, setList] = useState([]);
|
||||
const [chosenValue, setChosenValue] = useState('');
|
||||
/*fetch data in a JavaScript function*/
|
||||
const getData = async () => {
|
||||
try {
|
||||
/*TODO handle errors: https://www.valentinog.com/blog/await-react/*/
|
||||
const rsp = await axios.get('http://universities.hipolabs.com/search?country=United+Kingdom');
|
||||
|
||||
/*set state*/
|
||||
setList(rsp.data);
|
||||
} catch (err) {
|
||||
console.log('err.message: ' + err.message);
|
||||
}
|
||||
};
|
||||
|
||||
/*this hook is run after a DOM update. Changing state migh result in an infinite loop*/
|
||||
/*hook need to be placed in body of the function component in which it is used*/
|
||||
useEffect(() => {
|
||||
/*effect goes here*/
|
||||
getData();
|
||||
/*use an empty dependency array to ensure the hook is running only once*/
|
||||
/*TODO study dependency array: https://reactjs.org/docs/hooks-effect.html*/
|
||||
}, []);
|
||||
|
||||
return (
|
||||
<>
|
||||
<DropDownSelect name="University" options={list}/>
|
||||
</>
|
||||
);
|
||||
}
|
||||
|
||||
export default Home
|
Loading…
Reference in New Issue