Skip to content

Commit

Permalink
feat(examples): add example React app to demonstrate how to use the c…
Browse files Browse the repository at this point in the history
…omponents #133
  • Loading branch information
fengelniederhammer committed Apr 10, 2024
1 parent 0a95979 commit 8ab3d57
Show file tree
Hide file tree
Showing 14 changed files with 192 additions and 6 deletions.
2 changes: 0 additions & 2 deletions components/.eslintignore
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
node_modules/*
docs/*
docs-src/*
rollup-config.js
custom-elements.json
web-dev-server.config.js
build/*
storybook-static/*
3 changes: 2 additions & 1 deletion components/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
"exports": {
".": "./dist/dashboard-components.js",
"./custom-elements.json": "./custom-elements.json",
"./package.json": "./package.json"
"./package.json": "./package.json",
"./style.css": "./dist/style.css"
},
"files": [
"dist",
Expand Down
6 changes: 5 additions & 1 deletion components/src/web-components/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ export class App extends LitElement {
lapis: string = '';

override render() {
return html`${this.childNodes}`;
const children = [];
for (const childNode of this.childNodes) {
children.push(html`${childNode}`);
}
return html`${children}`;
}

override createRenderRoot() {
Expand Down
1 change: 1 addition & 0 deletions examples/React/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package-lock.json
17 changes: 17 additions & 0 deletions examples/React/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# GenSpectrum components in React

This demonstrates how to use GenSpectrum components in React.

Install the packed version of the components:

```bash
npm run import-components
```

Start a test server to see the components in action:

```bash
npm run start
```

Then, open http://localhost:5173/ in your browser.
12 changes: 12 additions & 0 deletions examples/React/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>GenSpectrum Components with React</title>
</head>
<body>
<div id="root"></div>
<script type="module" src="/src/main.tsx"></script>
</body>
</html>
22 changes: 22 additions & 0 deletions examples/React/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"name": "react",
"private": true,
"version": "0.0.0",
"type": "module",
"scripts": {
"dev": "vite",
"build": "tsc && vite build",
"preview": "vite preview",
"import-components": "npm --prefix ../../components/ run build-and-pack && npm install ../../components/genspectrum-dashboard-components.tgz"
},
"dependencies": {
"@genspectrum/dashboard-components": "file:../../components/genspectrum-dashboard-components.tgz",
"@types/react": "^18.2.66",
"@types/react-dom": "^18.2.22",
"@vitejs/plugin-react": "^4.2.1",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"typescript": "^5.2.2",
"vite": "^5.2.0"
}
}
78 changes: 78 additions & 0 deletions examples/React/src/App.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import {useEffect, useState} from 'react'
import '@genspectrum/dashboard-components'
import '@genspectrum/dashboard-components/style.css'

function App() {
const [location, setLocation] = useState({
region: "Europe",
country: "Switzerland"
})
const [dateRange, setDateRange] = useState({
dateFrom: "2021-01-01",
dateTo: "2022-01-01"
})

useEffect(() => {
const handleLocationChange = (event: Event) => setLocation((event as CustomEvent).detail);
const handleDateRangeChange = (event: Event) => setDateRange((event as CustomEvent).detail);

const locationFilter = document.querySelector('gs-location-filter');
if (locationFilter) {
locationFilter.addEventListener('gs-location-changed', handleLocationChange);
}

const dateRangeSelector = document.querySelector('gs-date-range-selector');
if (dateRangeSelector) {
dateRangeSelector.addEventListener('gs-date-range-changed', handleDateRangeChange);
}

// Cleanup function to remove event listeners
return () => {
if (locationFilter) {
locationFilter.removeEventListener('gs-location-changed', handleLocationChange);
}
if (dateRangeSelector) {
dateRangeSelector.removeEventListener('gs-date-range-changed', handleDateRangeChange);
}
};
}, []);

const denominator = {
...dateRange,
...location,
};

const numerator = {
...denominator,
displayName: "My variant",
pangoLineage: "B.1.1.7"
};

return (
<gs-app lapis="https://lapis.cov-spectrum.org/open/v1/sample">
<gs-location-filter
value="Europe / Switzerland"
fields='["region", "country", "division", "location"]'
></gs-location-filter>
<gs-date-range-selector
selectedValue="last6Months"
></gs-date-range-selector>
<gs-prevalence-over-time
numerator={JSON.stringify(numerator)}
denominator={JSON.stringify(denominator)}
granularity="day"
smoothingWindow="7"
views='["line", "table"]'
></gs-prevalence-over-time>
<gs-prevalence-over-time
numerator={JSON.stringify(numerator)}
denominator={JSON.stringify(denominator)}
granularity="month"
smoothingWindow="0"
views='["bar", "table"]'
></gs-prevalence-over-time>
</gs-app>
);
}

export default App
9 changes: 9 additions & 0 deletions examples/React/src/main.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import React from 'react'
import ReactDOM from 'react-dom/client'
import App from './App.tsx'

ReactDOM.createRoot(document.getElementById('root')!).render(
<React.StrictMode>
<App />
</React.StrictMode>,
)
1 change: 1 addition & 0 deletions examples/React/src/vite-env.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/// <reference types="vite/client" />
25 changes: 25 additions & 0 deletions examples/React/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"compilerOptions": {
"target": "ES2020",
"useDefineForClassFields": true,
"lib": ["ES2020", "DOM", "DOM.Iterable"],
"module": "ESNext",
"skipLibCheck": true,

/* Bundler mode */
"moduleResolution": "bundler",
"allowImportingTsExtensions": true,
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,
"jsx": "react-jsx",

/* Linting */
"strict": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noFallthroughCasesInSwitch": true
},
"include": ["src"],
"references": [{ "path": "./tsconfig.node.json" }]
}
11 changes: 11 additions & 0 deletions examples/React/tsconfig.node.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"compilerOptions": {
"composite": true,
"skipLibCheck": true,
"module": "ESNext",
"moduleResolution": "bundler",
"allowSyntheticDefaultImports": true,
"strict": true
},
"include": ["vite.config.ts"]
}
7 changes: 7 additions & 0 deletions examples/React/vite.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'

// https://vitejs.dev/config/
export default defineConfig({
plugins: [react()],
})
4 changes: 2 additions & 2 deletions examples/plainJavascript/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@
selectedValue="last6Months"
></gs-date-range-selector>
<gs-prevalence-over-time
numerator='{"country":"Switzerland", "pangoLineage":"B.1.1.7", "dateTo":"2022-01-01"}'
numerator='{"displayName": "My variant","country":"Switzerland", "pangoLineage":"B.1.1.7", "dateTo":"2022-01-01"}'
denominator='{"country":"Switzerland", "dateTo":"2022-01-01"}'
granularity="day"
smoothingWindow="7"
views='["line", "table"]'
></gs-prevalence-over-time>
<gs-prevalence-over-time
numerator='{"country":"Switzerland", "pangoLineage":"B.1.1.7", "dateTo":"2022-01-01"}'
numerator='{"displayName": "My variant","country":"Switzerland", "pangoLineage":"B.1.1.7", "dateTo":"2022-01-01"}'
denominator='{"country":"Switzerland", "dateTo":"2022-01-01"}'
granularity="month"
smoothingWindow="0"
Expand Down

0 comments on commit 8ab3d57

Please sign in to comment.