-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathinline_editable_renderer.tsx
86 lines (76 loc) · 2.24 KB
/
inline_editable_renderer.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
82
83
84
85
86
import { Colors } from '@blueprintjs/core';
import styled from '@emotion/styled';
import type { KeyboardEvent, ReactNode } from 'react';
import { useCallback, useMemo, useState } from 'react';
export interface InlineRendererEditableProps<T extends HTMLElement> {
ref: (node: T | null) => void;
/**
* Function to exit the editable state and display the children content.
*/
exit: () => void;
onKeyDown: (event: KeyboardEvent<HTMLInputElement>) => void;
}
export interface InlineEditableProps<T extends HTMLElement> {
renderEditable: (props: InlineRendererEditableProps<T>) => ReactNode;
children: ReactNode;
}
export const InlineEditableInput = styled.input`
width: 100%;
height: 100%;
box-shadow: 0 0 1px 1px ${Colors.GRAY1};
position: absolute;
outline: none;
inset: 0;
`;
const Container = styled.div`
min-width: 100%;
width: 100%;
min-height: 21px;
:focus,
:hover {
box-shadow: 0 0 1px 1px ${Colors.GRAY1};
}
`;
/**
* The `InlineEditable` component allows for inline editing of its content.
* It renders a component with `renderEditable` when focused or clicked
* and toggles back to the original content when the input loses focus.
*/
export function InlineEditable<T extends HTMLElement>(
props: InlineEditableProps<T>,
) {
const { children, renderEditable } = props;
const [isInputRendered, setIsInputRendered] = useState(false);
const toggle = useCallback(() => {
return setIsInputRendered((old) => !old);
}, []);
const renderEditableProps = useMemo<InlineRendererEditableProps<T>>(() => {
return {
isRendered: isInputRendered,
onKeyDown: (event) => {
if (event.key === 'Enter') {
setIsInputRendered(false);
}
},
ref: (node) => {
if (!node) return;
node.focus();
},
exit: () => setIsInputRendered(false),
};
}, [isInputRendered]);
return (
<div style={{ position: 'relative' }}>
<div style={{ visibility: isInputRendered ? 'visible' : 'hidden' }}>
{renderEditable(renderEditableProps)}
</div>
<Container
tabIndex={isInputRendered ? -1 : 0}
onFocus={() => setIsInputRendered(true)}
onClick={toggle}
>
{children}
</Container>
</div>
);
}