-
Notifications
You must be signed in to change notification settings - Fork 535
/
Copy pathTodoItemView.tsx
76 lines (67 loc) · 2.09 KB
/
TodoItemView.tsx
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
/*!
* Copyright (c) Microsoft Corporation and contributors. All rights reserved.
* Licensed under the MIT License.
*/
import {
CollaborativeInput,
CollaborativeTextArea,
SharedStringHelper,
} from "@fluid-example/example-utils";
import React, { useEffect, useState } from "react";
import { TodoItem } from "./TodoItem.js";
// eslint-disable-next-line import/no-unassigned-import
import "./style.css";
interface TodoItemViewProps {
readonly todoItemModel: TodoItem;
readonly className?: string;
}
export const TodoItemView: React.FC<TodoItemViewProps> = (props: TodoItemViewProps) => {
const { todoItemModel, className } = props;
const itemText = todoItemModel.getText();
const [checked, setChecked] = useState<boolean>(todoItemModel.getCheckedState());
const [detailsVisible, setDetailsVisible] = useState<boolean>(false);
useEffect(() => {
const refreshCheckedStateFromModel = () => {
setChecked(todoItemModel.getCheckedState());
};
todoItemModel.on("checkedStateChanged", refreshCheckedStateFromModel);
refreshCheckedStateFromModel();
return () => {
todoItemModel.off("checkedStateChanged", refreshCheckedStateFromModel);
};
}, [todoItemModel]);
const checkChangedHandler = (e: React.ChangeEvent<HTMLInputElement>): void => {
todoItemModel.setCheckedState(e.target.checked);
};
return (
<div className={`todo-item${className !== undefined ? ` ${className}` : ""}`}>
<h2 className="todo-item-header">
<input
type="checkbox"
className="todo-item-checkbox"
checked={checked}
onChange={checkChangedHandler}
/>
<button
className="todo-item-expand-button"
name="toggleDetailsVisible"
onClick={() => {
setDetailsVisible(!detailsVisible);
}}
>
{detailsVisible ? "▲" : "▼"}
</button>
<CollaborativeInput sharedString={itemText} className="todo-item-input" />
</h2>
{
// The details can be shown or hidden
detailsVisible && (
<CollaborativeTextArea
className="todo-item-details"
sharedStringHelper={new SharedStringHelper(todoItemModel.getDetailedText())}
/>
)
}
</div>
);
};