All files / src Example.js

100% Statements 5/5
66.67% Branches 4/6
100% Functions 2/2
100% Lines 5/5

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58                                        65x 90x   90x 90x     90x                                                            
// @flow strict
import React, { useCallback, useState } from 'react';
 
import type { Node } from 'react';
 
import TabBar from './material-toolbox/tab-bar';
import Tab from './material-toolbox/tab';
import TabText from './material-toolbox/tab/Text';
import TabScroller from './material-toolbox/tab-scroller';
import Text from './material-toolbox/typography/Text';
 
import Highlighter from './Highlighter';
 
type Props = {
  title: string,
  children: Node,
  source: string,
  scss?: string,
};
 
const Example = ({ title, children, source, scss }: Props) => {
  const [tabSelected, setTabSelected] = useState<number>(0);
 
  const handleChange = useCallback((index: number) => {
    setTabSelected(index);
  }, []);
 
  return (
    <div className="example">
      <Text text="subtitle1" as="h2">
        {title}
      </Text>
      <TabBar
        className="mt-tab"
        focusOnActivate={false}
        initialActive={0}
        onChange={handleChange}
      >
        <TabScroller align="center">
          <Tab fade minWidth>
            <TabText>Example</TabText>
          </Tab>
          <Tab fade minWidth>
            <TabText>Source</TabText>
          </Tab>
        </TabScroller>
      </TabBar>
      <section className={tabSelected === 0 ? '' : 'hide'}>{children}</section>
      <section className={tabSelected === 1 ? '' : 'hide'}>
        <Highlighter language="jsx">{source}</Highlighter>
        {scss != null && <Highlighter language="scss">{scss}</Highlighter>}
      </section>
    </div>
  );
};
 
export default Example;