-
Notifications
You must be signed in to change notification settings - Fork 84
/
Copy pathNewTimer.js
66 lines (61 loc) · 1.49 KB
/
NewTimer.js
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
import * as React from 'react';
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import { faPlay } from '@fortawesome/free-solid-svg-icons';
import { useMachine } from '@xstate/react';
import { newTimerMachine } from './newTimerMachine';
import { useRef } from 'react';
export const NewTimer = ({ onSubmit, onCancel }) => {
const inputRef = useRef();
const [state, send] = useMachine(newTimerMachine, {
actions: {
submit: (context) => {
onSubmit(context.duration);
},
},
});
React.useEffect(() => {
inputRef.current?.focus();
}, [inputRef]);
const { duration } = state.context;
return (
<form
className="screen"
data-screen="new-timer"
data-testid="new-timer"
onSubmit={(e) => {
e.preventDefault();
send(e);
}}
>
<input
type="number"
min={0}
step={1}
placeholder="00s"
onChange={send}
title="Duration"
ref={inputRef}
/>
<div className="actions">
{onCancel ? (
<button
type="button"
title="Cancel"
className="transparent"
onClick={() => {
onCancel();
}}
>
Cancel
</button>
) : null}
<button
title={`Start ${duration}-second timer`}
hidden={duration <= 0 || undefined}
>
<FontAwesomeIcon icon={faPlay} />
</button>
</div>
</form>
);
};