OfficeDev / Office-Addin-TaskPane-React-JS

Template to get start started writing a TaskPane Office Add-in for the React framework using JavaScript
Other
25 stars 19 forks source link

React Hooks samples? #31

Closed sethachoi closed 3 years ago

sethachoi commented 5 years ago

Hi,

Was wondering if we could eventually get some samples for react hooks? Particularly, react hooks with event listeners if at all possible.

Thanks!

nagaozen commented 4 years ago

App.js

import React, { useState } from "react"
import { Button, ButtonType } from "office-ui-fabric-react"

import Header from "./Header"
import HeroList from "./HeroList"
import Progress from "./Progress"

const handleClick = (event) => Word.run(async context => {
  const paragraph = context.document.body.insertParagraph("Hello World", Word.InsertLocation.end)
  paragraph.font.color = "blue"
  await context.sync()
})

const App = ({ title, isOfficeInitialized }) => {
  const [listItems, setListItems] = useState([
    { icon: "Ribbon", primaryText: "Achieve more with Office integration" },
    { icon: "Unlock", primaryText: "Unlock features and functionality" },
    { icon: "Design", primaryText: "Create and visualize like a pro" }
  ])

  if (!isOfficeInitialized) {
    return (
      <Progress title={title} logo="assets/logo-filled.png" message="Please sideload your addin to see app body." />
    );
  }

  return (
    <div className="ms-welcome">
      <Header logo="assets/logo-filled.png" title={title} message="Welcome" />
      <HeroList message="Discover what Office Add-ins can do for you today!" items={listItems}>
        <p className="ms-font-l">
          Modify the source files, then click <b>Run</b>.
        </p>
        <Button
          className="ms-welcome__action"
          buttonType={ButtonType.hero}
          iconProps={{ iconName: "ChevronRight" }}
          onClick={handleClick}
        >Run</Button>
      </HeroList>
    </div>
  )
}

export default App

Header.js

import React from 'react'

const Header = ({ title, logo, message }) => (
  <section className='ms-welcome__header ms-bgColor-neutralLighter ms-u-fadeIn500'>
    <img width='90' height='90' src={logo} alt={title} title={title} />
    <h1 className='ms-fontSize-su ms-fontWeight-light ms-fontColor-neutralPrimary'>{message}</h1>
  </section>
)

export default Header

HeroList.js

import React from 'react'

const HeroList = ({ children, items, message }) => {
  const listItems = items.map((item, index) => (
    <li className='ms-ListItem' key={index}>
      <i className={`ms-Icon ms-Icon--${item.icon}`} />
      <span className='ms-font-m ms-fontColor-neutralPrimary'>{item.primaryText}</span>
    </li>
  ))

  return (
    <main className='ms-welcome__main'>
      <h2 className='ms-font-xl ms-fontWeight-semilight ms-fontColor-neutralPrimary ms-u-slideUpIn20'>{message}</h2>
      <p>Hello World</p>
      <ul className='ms-List ms-welcome__features ms-u-slideUpIn10'>{listItems}</ul>
      {children}
    </main>
  )
}

export default HeroList

Progress.js

import React from 'react'
import { Spinner, SpinnerType } from 'office-ui-fabric-react'

const Progress = ({ logo, message, title }) => (
  <section className='ms-welcome__progress ms-u-fadeIn500'>
    <img width='90' height='90' src={logo} alt={title} title={title} />
    <h1 className='ms-fontSize-su ms-fontWeight-light ms-fontColor-neutralPrimary'>{title}</h1>
    <Spinner type={SpinnerType.large} label={message} />
  </section>
)

export default Progress
igor-ribeiiro commented 3 years ago

Hey @sethachoi, I guess the commentary given gave some samples to help you if you are still facing the issue. I will be closing this issue for now but feel free to reopen it if you still need help.

Rick-Kirkham commented 1 year ago

@sethachoi The project generated from this repo's template has just been updated to use hooks.