|
| 1 | +/* eslint-disable no-unused-vars */ |
| 2 | +import React from 'react'; |
| 3 | +import {pick} from 'ramda'; |
| 4 | +import {PersistedProps, PersistenceTypes, TextAreaProps} from '../types'; |
| 5 | +import './css/textarea.css'; |
| 6 | + |
| 7 | +const textAreaProps = [ |
| 8 | + 'id', |
| 9 | + 'form', |
| 10 | + 'name', |
| 11 | + 'placeholder', |
| 12 | + 'wrap', |
| 13 | + 'accessKey', |
| 14 | + 'contextMenu', |
| 15 | + 'dir', |
| 16 | + 'draggable', |
| 17 | + 'lang', |
| 18 | + 'spellCheck', |
| 19 | + 'style', |
| 20 | + 'title', |
| 21 | +] as const; |
| 22 | + |
| 23 | +const asNumber = (value?: string | number): number | undefined => { |
| 24 | + return typeof value === 'string' |
| 25 | + ? isNaN(parseInt(value, 10)) |
| 26 | + ? undefined |
| 27 | + : parseInt(value, 10) |
| 28 | + : value; |
| 29 | +}; |
| 30 | +const asBool = (value?: string | boolean): boolean | undefined => { |
| 31 | + if (typeof value === 'string') { |
| 32 | + if (['true', 'TRUE', 'True'].includes(value)) { |
| 33 | + return true; |
| 34 | + } |
| 35 | + if (['false', 'FALSE', 'False'].includes(value)) { |
| 36 | + return false; |
| 37 | + } |
| 38 | + return undefined; |
| 39 | + } |
| 40 | + return value; |
| 41 | +}; |
| 42 | + |
| 43 | +/** |
| 44 | + * A basic HTML textarea for entering multiline text. |
| 45 | + * |
| 46 | + */ |
| 47 | +const Textarea = ({ |
| 48 | + setProps, |
| 49 | + n_blur = 0, |
| 50 | + n_blur_timestamp = -1, |
| 51 | + n_clicks = 0, |
| 52 | + n_clicks_timestamp = -1, |
| 53 | + ...props |
| 54 | +}: TextAreaProps) => { |
| 55 | + const ctx = window.dash_component_api.useDashContext(); |
| 56 | + const isLoading = ctx.useLoading(); |
| 57 | + |
| 58 | + return ( |
| 59 | + <textarea |
| 60 | + data-dash-is-loading={isLoading || undefined} |
| 61 | + className={'dash-textarea ' + props.className} |
| 62 | + value={props.value} |
| 63 | + cols={asNumber(props.cols)} |
| 64 | + rows={asNumber(props.rows)} |
| 65 | + disabled={asBool(props.disabled)} |
| 66 | + minLength={asNumber(props.minLength)} |
| 67 | + maxLength={asNumber(props.maxLength)} |
| 68 | + readOnly={asBool(props.readOnly)} |
| 69 | + required={asBool(props.required)} |
| 70 | + autoFocus={asBool(props.autoFocus)} |
| 71 | + contentEditable={asBool(props.contentEditable)} |
| 72 | + hidden={asBool(props.hidden)} |
| 73 | + tabIndex={asNumber(props.tabIndex)} |
| 74 | + onChange={e => { |
| 75 | + setProps({value: e.target.value}); |
| 76 | + }} |
| 77 | + onBlur={() => { |
| 78 | + setProps({ |
| 79 | + n_blur: n_blur + 1, |
| 80 | + n_blur_timestamp: Date.now(), |
| 81 | + }); |
| 82 | + }} |
| 83 | + onClick={() => { |
| 84 | + setProps({ |
| 85 | + n_clicks: n_clicks + 1, |
| 86 | + n_clicks_timestamp: Date.now(), |
| 87 | + }); |
| 88 | + }} |
| 89 | + {...pick(textAreaProps, props)} |
| 90 | + /> |
| 91 | + ); |
| 92 | +}; |
| 93 | + |
| 94 | +Textarea.dashPersistence = { |
| 95 | + persisted_props: [PersistedProps.value], |
| 96 | + persistence_type: PersistenceTypes.local, |
| 97 | +}; |
| 98 | + |
| 99 | +export default Textarea; |
0 commit comments