fabiobiondi

Custom Components (HTML inherit)

Oct 21st, 2020
367
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React, { useState } from 'react';
  2.  
  3. export default function App() {
  4.   console.log('App: render')
  5.  
  6.   return (
  7.      <div className="container mt-2">
  8.        <Panel title="hello" style={{ width: 200}}>
  9.          <input type="text"/>
  10.          <input type="text"/>
  11.          <input type="text"/>
  12.        </Panel>
  13.  
  14.        <Panel title="main">
  15.          <GMap city="Trieste" width={300} style={{ border: '2px solid blue'}} alt="Trieste"/>
  16.  
  17.          <MyButton style={{ backgroundColor: 'red'}} disabled={true} onClick={() => console.log('ciao')}>CLICK ME</MyButton>
  18.        </Panel>
  19.      </div>
  20.   );
  21. }
  22.  
  23. const MyButton: React.FC<React.ButtonHTMLAttributes<HTMLButtonElement>> = ({  children, style, className, ...rest }) => {
  24.   const styles = {
  25.     backgroundColor: 'black', color: 'white', padding: 20,
  26.     ...style
  27.   }
  28.   return <button style={styles} {...rest}>{children}</button>
  29. }
  30.  
  31. // GMAP
  32. interface GMapProps {
  33.   city: string;
  34. }
  35.  
  36. const GMap: React.FC<GMapProps & React.ImgHTMLAttributes<HTMLImageElement>> = props => {
  37.   const url = `https://maps.googleapis.com/maps/api/staticmap?center=${props.city}&zoom=5&size=200x100&key=AIzaSyDSBmiSWV4-AfzaTPNSJhu-awtfBNi9o2k`
  38.   const { city, ...rest} = props;
  39.   return <img src={url} {...rest} />
  40. }
  41.  
  42.  
  43.  
  44. // PANEL COMPONENT
  45. interface PanelProps {
  46.   title: string;
  47. }
  48.  
  49. const Panel: React.FC<PanelProps & React.HTMLProps<HTMLDivElement>> = props => {
  50.   const { children, title, ...rest} = props;
  51.   const [opened, setOpened] = useState<boolean>(true);
  52.  
  53.   return (
  54.     <div className={'card'} {...rest}>
  55.       <div className={'card-header'} onClick={() => setOpened(!opened)}>{props.title}</div>
  56.       { opened && <div className="card-body">{props.children} </div>}
  57.     </div>
  58.   )
  59. }
  60.  
Add Comment
Please, Sign In to add comment