-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Expand file tree
/
Copy pathTextarea.tsx
More file actions
99 lines (93 loc) · 2.62 KB
/
Textarea.tsx
File metadata and controls
99 lines (93 loc) · 2.62 KB
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
/* eslint-disable no-unused-vars */
import React from 'react';
import {pick} from 'ramda';
import {PersistedProps, PersistenceTypes, TextAreaProps} from '../types';
import './css/textarea.css';
const textAreaProps = [
'id',
'form',
'name',
'placeholder',
'wrap',
'accessKey',
'contextMenu',
'dir',
'draggable',
'lang',
'spellCheck',
'style',
'title',
] as const;
const asNumber = (value?: string | number): number | undefined => {
return typeof value === 'string'
? isNaN(parseInt(value, 10))
? undefined
: parseInt(value, 10)
: value;
};
const asBool = (value?: string | boolean): boolean | undefined => {
if (typeof value === 'string') {
if (['true', 'TRUE', 'True'].includes(value)) {
return true;
}
if (['false', 'FALSE', 'False'].includes(value)) {
return false;
}
return undefined;
}
return value;
};
/**
* A basic HTML textarea for entering multiline text.
*
*/
const Textarea = ({
setProps,
n_blur = 0,
n_blur_timestamp = -1,
n_clicks = 0,
n_clicks_timestamp = -1,
...props
}: TextAreaProps) => {
const ctx = window.dash_component_api.useDashContext();
const isLoading = ctx.useLoading();
return (
<textarea
data-dash-is-loading={isLoading || undefined}
className={'dash-textarea ' + props.className}
value={props.value}
cols={asNumber(props.cols)}
rows={asNumber(props.rows)}
disabled={asBool(props.disabled)}
minLength={asNumber(props.minLength)}
maxLength={asNumber(props.maxLength)}
readOnly={asBool(props.readOnly)}
required={asBool(props.required)}
autoFocus={asBool(props.autoFocus)}
contentEditable={asBool(props.contentEditable)}
hidden={asBool(props.hidden)}
tabIndex={asNumber(props.tabIndex)}
onChange={e => {
setProps({value: e.target.value});
}}
onBlur={() => {
setProps({
n_blur: n_blur + 1,
n_blur_timestamp: Date.now(),
});
}}
onClick={() => {
setProps({
n_clicks: n_clicks + 1,
n_clicks_timestamp: Date.now(),
});
}}
{...pick(textAreaProps, props)}
/>
);
};
Textarea.dashPersistence = {
persisted_props: [PersistedProps.value],
persistence_type: PersistenceTypes.local,
};
export default Textarea;