All files / src/dialog-examples Scrollable.js

62.5% Statements 10/16
100% Branches 0/0
20% Functions 1/5
62.5% Lines 10/16

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 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279                              2x                                                                                                                                                                                                                                                                                 2x 2x 2x   2x       2x         2x         2x       2x       2x                                                                                                                                                                                                        
// @flow strict
import React, { useState, useCallback } from 'react';
 
import MDCDialogFoundation from '@material/dialog/foundation';
 
import Example from '../Example';
 
import Dialog from '../material-toolbox/dialog';
import DialogContent from '../material-toolbox/dialog/Content';
import DialogActions from '../material-toolbox/dialog/Actions';
import DialogButton from '../material-toolbox/dialog/Button';
 
import Button from '../material-toolbox/button';
import ButtonLabel from '../material-toolbox/button/Label';
 
const source = `import React, { useState, useCallback } from 'react';
 
import MDCDialogFoundation from '@material/dialog/foundation';
 
import Dialog from 'material-toolbox/dialog';
import DialogContent from 'material-toolbox/dialog/Content';
import DialogActions from 'material-toolbox/dialog/Actions';
import DialogButton from 'material-toolbox/dialog/Button';
 
import Button from 'material-toolbox/button';
import ButtonLabel from 'material-toolbox/button/Label';
 
const ScrollableDialog = () => {
  const [isOpen, setOpen] = useState(false);
  const [selected, setSelected] = useState();
 
  const handleClosed = useCallback(() => {
    setOpen(false);
  }, []);
 
  const handleDeny = useCallback(() => {
    setSelected('Declined... Maybe next time?');
    setOpen(false);
  }, []);
 
  const handleAccept = useCallback(() => {
    setSelected('Accepted, thanks!');
    setOpen(false);
  }, []);
 
  const handleOpen = useCallback(() => {
    setOpen(true);
  }, []);
 
  const initialFocusAttr = {
    [MDCDialogFoundation.strings.INITIAL_FOCUS_ATTRIBUTE]: true,
  };
 
  return (
    <React.Fragment>
      <Button onClick={handleOpen}>Scrollable</Button>
      <p>{selected}</p>
      <Dialog
        title="The Wonderful Wizard of Oz"
        name="scrollable-dialog"
        open={isOpen}
        onClosed={handleClosed}>
        <DialogContent>
          <p>
            <a
              href="https://www.gutenberg.org/ebooks/55"
              target="_blank"
              rel="noopener noreferrer">
              Read the full book
            </a>
          </p>
          <p>
            Dorothy lived in the midst of the great Kansas prairies, with Uncle
            Henry, who was a farmer, and Aunt Em, who was the farmer&#39;s wife.
            Their house was small, for the lumber to build it had to be carried
            by wagon many miles. There were four walls, a floor and a roof,
            which made one room; and this room contained a rusty looking
            cookstove, a cupboard for the dishes, a table, three or four chairs,
            and the beds. Uncle Henry and Aunt Em had a big bed in one corner,
            and Dorothy a little bed in another corner. There was no garret at
            all, and no cellar--except a small hole dug in the ground, called a
            cyclone cellar, where the family could go in case one of those great
            whirlwinds arose, mighty enough to crush any building in its path.
            It was reached by a trap door in the middle of the floor, from which
            a ladder led down into the small, dark hole.
          </p>
          <p>
            When Dorothy stood in the doorway and looked around, she could see
            nothing but the great gray prairie on every side. Not a tree nor a
            house broke the broad sweep of flat country that reached to the edge
            of the sky in all directions. The sun had baked the plowed land into
            a gray mass, with little cracks running through it. Even the grass
            was not green, for the sun had burned the tops of the long blades
            until they were the same gray color to be seen everywhere. Once the
            house had been painted, but the sun blistered the paint and the
            rains washed it away, and now the house was as dull and gray as
            everything else.
          </p>
          <p>
            When Aunt Em came there to live she was a young, pretty wife. The
            sun and wind had changed her, too. They had taken the sparkle from
            her eyes and left them a sober gray; they had taken the red from her
            cheeks and lips, and they were gray also. She was thin and gaunt,
            and never smiled now. When Dorothy, who was an orphan, first came to
            her, Aunt Em had been so startled by the child&#39;s laughter that
            she would scream and press her hand upon her heart whenever
            Dorothy&#39;s merry voice reached her ears; and she still looked at
            the little girl with wonder that she could find anything to laugh
            at.
          </p>
          <p>
            Uncle Henry never laughed. He worked hard from morning till night
            and did not know what joy was. He was gray also, from his long beard
            to his rough boots, and he looked stern and solemn, and rarely
            spoke.
          </p>
          <p>
            It was Toto that made Dorothy laugh, and saved her from growing as
            gray as her other surroundings. Toto was not gray; he was a little
            black dog, with long silky hair and small black eyes that twinkled
            merrily on either side of his funny, wee nose. Toto played all day
            long, and Dorothy played with him, and loved him dearly.
          </p>
          <p>
            Today, however, they were not playing. Uncle Henry sat upon the
            doorstep and looked anxiously at the sky, which was even grayer than
            usual. Dorothy stood in the door with Toto in her arms, and looked
            at the sky too. Aunt Em was washing the dishes.
          </p>
          <p>
            From the far north they heard a low wail of the wind, and Uncle
            Henry and Dorothy could see where the long grass bowed in waves
            before the coming storm. There now came a sharp whistling in the air
            from the south, and as they turned their eyes that way they saw
            ripples in the grass coming from that direction also.
          </p>
        </DialogContent>
        <DialogActions>
          <DialogButton onClick={handleDeny}>
            <ButtonLabel>Cancel</ButtonLabel>
          </DialogButton>
          <DialogButton {...initialFocusAttr} onClick={handleAccept}>
            <ButtonLabel>Accept</ButtonLabel>
          </DialogButton>
        </DialogActions>
      </Dialog>
    </React.Fragment>
  );
};
 
export default ScrollableDialog;`;
 
const ScrollableDialog = () => {
  const [isOpen, setOpen] = useState<boolean>(false);
  const [selected, setSelected] = useState<?string>();
 
  const handleClosed = useCallback(() => {
    setOpen(false);
  }, []);
 
  const handleDeny = useCallback(() => {
    setSelected('Declined... Maybe next time?');
    setOpen(false);
  }, []);
 
  const handleAccept = useCallback(() => {
    setSelected('Accepted, thanks!');
    setOpen(false);
  }, []);
 
  const handleOpen = useCallback(() => {
    setOpen(true);
  }, []);
 
  const initialFocusAttr = {
    [MDCDialogFoundation.strings.INITIAL_FOCUS_ATTRIBUTE]: true,
  };
 
  return (
    <Example title="Scrollable" source={source}>
      <Button onClick={handleOpen}>Scrollable</Button>
      <p>{selected}</p>
      <Dialog
        title="The Wonderful Wizard of Oz"
        name="scrollable-dialog"
        open={isOpen}
        onClosed={handleClosed}
      >
        <DialogContent>
          <p>
            <a
              href="https://www.gutenberg.org/ebooks/55"
              target="_blank"
              rel="noopener noreferrer"
            >
              Read the full book
            </a>
          </p>
          <p>
            Dorothy lived in the midst of the great Kansas prairies, with Uncle
            Henry, who was a farmer, and Aunt Em, who was the farmer&#39;s wife.
            Their house was small, for the lumber to build it had to be carried
            by wagon many miles. There were four walls, a floor and a roof,
            which made one room; and this room contained a rusty looking
            cookstove, a cupboard for the dishes, a table, three or four chairs,
            and the beds. Uncle Henry and Aunt Em had a big bed in one corner,
            and Dorothy a little bed in another corner. There was no garret at
            all, and no cellar--except a small hole dug in the ground, called a
            cyclone cellar, where the family could go in case one of those great
            whirlwinds arose, mighty enough to crush any building in its path.
            It was reached by a trap door in the middle of the floor, from which
            a ladder led down into the small, dark hole.
          </p>
          <p>
            When Dorothy stood in the doorway and looked around, she could see
            nothing but the great gray prairie on every side. Not a tree nor a
            house broke the broad sweep of flat country that reached to the edge
            of the sky in all directions. The sun had baked the plowed land into
            a gray mass, with little cracks running through it. Even the grass
            was not green, for the sun had burned the tops of the long blades
            until they were the same gray color to be seen everywhere. Once the
            house had been painted, but the sun blistered the paint and the
            rains washed it away, and now the house was as dull and gray as
            everything else.
          </p>
          <p>
            When Aunt Em came there to live she was a young, pretty wife. The
            sun and wind had changed her, too. They had taken the sparkle from
            her eyes and left them a sober gray; they had taken the red from her
            cheeks and lips, and they were gray also. She was thin and gaunt,
            and never smiled now. When Dorothy, who was an orphan, first came to
            her, Aunt Em had been so startled by the child&#39;s laughter that
            she would scream and press her hand upon her heart whenever
            Dorothy&#39;s merry voice reached her ears; and she still looked at
            the little girl with wonder that she could find anything to laugh
            at.
          </p>
          <p>
            Uncle Henry never laughed. He worked hard from morning till night
            and did not know what joy was. He was gray also, from his long beard
            to his rough boots, and he looked stern and solemn, and rarely
            spoke.
          </p>
          <p>
            It was Toto that made Dorothy laugh, and saved her from growing as
            gray as her other surroundings. Toto was not gray; he was a little
            black dog, with long silky hair and small black eyes that twinkled
            merrily on either side of his funny, wee nose. Toto played all day
            long, and Dorothy played with him, and loved him dearly.
          </p>
          <p>
            Today, however, they were not playing. Uncle Henry sat upon the
            doorstep and looked anxiously at the sky, which was even grayer than
            usual. Dorothy stood in the door with Toto in her arms, and looked
            at the sky too. Aunt Em was washing the dishes.
          </p>
          <p>
            From the far north they heard a low wail of the wind, and Uncle
            Henry and Dorothy could see where the long grass bowed in waves
            before the coming storm. There now came a sharp whistling in the air
            from the south, and as they turned their eyes that way they saw
            ripples in the grass coming from that direction also.
          </p>
        </DialogContent>
        <DialogActions>
          <DialogButton onClick={handleDeny}>
            <ButtonLabel>Cancel</ButtonLabel>
          </DialogButton>
          <DialogButton {...initialFocusAttr} onClick={handleAccept}>
            <ButtonLabel>Accept</ButtonLabel>
          </DialogButton>
        </DialogActions>
      </Dialog>
    </Example>
  );
};
 
export default ScrollableDialog;