Skip to content
Open
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
287 changes: 0 additions & 287 deletions components/dash-core-components/src/components/Textarea.react.js

This file was deleted.

99 changes: 99 additions & 0 deletions components/dash-core-components/src/components/Textarea.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,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;
};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think those conversion are necessary, the props are transfered in json and bool and number. The prop type should be bool or number but the string interpolation makes it harder on the python type check.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like your idea here, and pushed up a change that removes string as an allowed type for these props.

However... these existed for API compatibility (the docs say string is allowed) so just want you to be aware of the backwards incompatibility that this introduces (especially for numeric props like cols and maxLength)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The html textarea cols/maxLength can accept string, but we didn't do the explicit conversion before. I think those props should follow what is in the react props for that html attributes.

    interface TextareaHTMLAttributes<T> extends HTMLAttributes<T> {
        autoComplete?: string | undefined;
        cols?: number | undefined;
        dirName?: string | undefined;
        disabled?: boolean | undefined;
        form?: string | undefined;
        maxLength?: number | undefined;
        minLength?: number | undefined;
        name?: string | undefined;
        placeholder?: string | undefined;
        readOnly?: boolean | undefined;
        required?: boolean | undefined;
        rows?: number | undefined;
        value?: string | readonly string[] | number | undefined;
        wrap?: string | undefined;

        onChange?: ChangeEventHandler<T> | undefined;
    }

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I think we're agreeing here.. Numeric props no longer accept a string value. And that is the current state of this PR.


/**
* 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;
Loading