-
Notifications
You must be signed in to change notification settings - Fork 544
/
Copy pathTodoItemView.tsx
81 lines (70 loc) · 2.32 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
77
78
79
80
81
/*!
* Copyright (c) Microsoft Corporation and contributors. All rights reserved.
* Licensed under the MIT License.
*/
import {
CollaborativeInput,
CollaborativeTextArea,
SharedStringHelper,
} from "@fluid-example/example-utils";
import type { SharedString } from "@fluidframework/sequence/legacy";
import React, { useEffect, useState } from "react";
// eslint-disable-next-line import/no-unassigned-import
import "./style.css";
import { type TodoItem } from "../Todo/index.js";
import { useTree } from "../Utils/index.js";
interface TodoItemViewProps {
readonly todoItemModel: TodoItem;
readonly className?: string;
}
export const TodoItemView: React.FC<TodoItemViewProps> = (props: TodoItemViewProps) => {
const { todoItemModel, className } = props;
const [itemTitle, setItemTitle] = useState<SharedString | undefined>(undefined);
const [itemDescription, setItemDescription] = useState<SharedString | undefined>(undefined);
const [detailsVisible, setDetailsVisible] = useState<boolean>(false);
useTree(todoItemModel);
useEffect(() => {
void Promise.resolve(todoItemModel.title.get()).then((text) => {
setItemTitle(text as SharedString);
});
}, [todoItemModel.title]);
useEffect(() => {
void Promise.resolve(todoItemModel.description.get()).then((text) => {
setItemDescription(text as SharedString);
});
}, [todoItemModel.description]);
const checkChangedHandler = (e: React.ChangeEvent<HTMLInputElement>): void => {
todoItemModel.completed = e.target.checked;
};
if (itemTitle === undefined || itemDescription === undefined) {
return <div>Loading item...</div>;
}
return (
<div className={`todo-item${className !== undefined ? ` ${className}` : ""}`}>
<h2 className="todo-item-header">
<input
type="checkbox"
className="todo-item-checkbox"
checked={todoItemModel.completed}
onChange={checkChangedHandler}
/>
<button
className="todo-item-expand-button"
name="toggleDetailsVisible"
onClick={() => {
setDetailsVisible(!detailsVisible);
}}
>
{detailsVisible ? "▲" : "▼"}
</button>
<CollaborativeInput sharedString={itemTitle} className="todo-item-input" />
</h2>
{detailsVisible && (
<CollaborativeTextArea
className="todo-item-details"
sharedStringHelper={new SharedStringHelper(itemDescription)}
/>
)}
</div>
);
};