Skip to content

feat(text-area)!: integrate TextArea with v11 #1967

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 30, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
22 changes: 9 additions & 13 deletions COMPONENT_INDEX.md
Original file line number Diff line number Diff line change
Expand Up @@ -4110,6 +4110,7 @@ None.
| cols | No | <code>let</code> | No | <code>number</code> | <code>undefined</code> | Specify the number of cols |
| rows | No | <code>let</code> | No | <code>number</code> | <code>4</code> | Specify the number of rows |
| maxCount | No | <code>let</code> | No | <code>number</code> | <code>undefined</code> | Specify the max character count |
| counterMode | No | <code>let</code> | No | <code>"character" &#124; "word"</code> | <code>"character"</code> | Specify the counter mode |
| light | No | <code>let</code> | No | <code>boolean</code> | <code>false</code> | Set to `true` to enable the light variant |
| disabled | No | <code>let</code> | No | <code>boolean</code> | <code>false</code> | Set to `true` to disable the input |
| readonly | No | <code>let</code> | No | <code>boolean</code> | <code>false</code> | Set to `true` to use the read-only variant |
Expand All @@ -4118,23 +4119,23 @@ None.
| hideLabel | No | <code>let</code> | No | <code>boolean</code> | <code>false</code> | Set to `true` to visually hide the label text |
| invalid | No | <code>let</code> | No | <code>boolean</code> | <code>false</code> | Set to `true` to indicate an invalid state |
| invalidText | No | <code>let</code> | No | <code>string</code> | <code>""</code> | Specify the text for the invalid state |
| warn | No | <code>let</code> | No | <code>boolean</code> | <code>false</code> | Set to `true` to indicate an warning state |
| warnText | No | <code>let</code> | No | <code>string</code> | <code>""</code> | Specify the warning state text |
| id | No | <code>let</code> | No | <code>string</code> | <code>"ccs-" + Math.random().toString(36)</code> | Set an id for the textarea element |
| name | No | <code>let</code> | No | <code>string</code> | <code>undefined</code> | Specify a name attribute for the input |

### Slots

| Slot name | Default | Props | Fallback |
| :-------- | :------ | :---- | :----------------------- |
| labelText | No | -- | <code>{labelText}</code> |
| Slot name | Default | Props | Fallback |
| :---------- | :------ | :---- | :------------------------- |
| invalidText | No | -- | <code>{invalidText}</code> |
| labelText | No | -- | <code>{labelText}</code> |
| warnText | No | -- | <code>{warnText}</code> |

### Events

| Event name | Type | Detail |
| :--------- | :-------- | :----- |
| click | forwarded | -- |
| mouseover | forwarded | -- |
| mouseenter | forwarded | -- |
| mouseleave | forwarded | -- |
| change | forwarded | -- |
| input | forwarded | -- |
| keydown | forwarded | -- |
Expand All @@ -4157,12 +4158,7 @@ None.

### Events

| Event name | Type | Detail |
| :--------- | :-------- | :----- |
| click | forwarded | -- |
| mouseover | forwarded | -- |
| mouseenter | forwarded | -- |
| mouseleave | forwarded | -- |
None.

## `TextInput`

Expand Down
62 changes: 50 additions & 12 deletions docs/src/COMPONENT_API.json
Original file line number Diff line number Diff line change
Expand Up @@ -12562,6 +12562,18 @@
"constant": false,
"reactive": false
},
{
"name": "counterMode",
"kind": "let",
"description": "Specify the counter mode",
"type": "\"character\" | \"word\"",
"value": "\"character\"",
"isFunction": false,
"isFunctionDeclaration": false,
"isRequired": false,
"constant": false,
"reactive": false
},
{
"name": "light",
"kind": "let",
Expand Down Expand Up @@ -12658,6 +12670,30 @@
"constant": false,
"reactive": false
},
{
"name": "warn",
"kind": "let",
"description": "Set to `true` to indicate an warning state",
"type": "boolean",
"value": "false",
"isFunction": false,
"isFunctionDeclaration": false,
"isRequired": false,
"constant": false,
"reactive": false
},
{
"name": "warnText",
"kind": "let",
"description": "Specify the warning state text",
"type": "string",
"value": "\"\"",
"isFunction": false,
"isFunctionDeclaration": false,
"isRequired": false,
"constant": false,
"reactive": false
},
{
"name": "id",
"kind": "let",
Expand Down Expand Up @@ -12696,18 +12732,26 @@
],
"moduleExports": [],
"slots": [
{
"name": "invalidText",
"default": false,
"fallback": "{invalidText}",
"slot_props": "{}"
},
{
"name": "labelText",
"default": false,
"fallback": "{labelText}",
"slot_props": "{}"
},
{
"name": "warnText",
"default": false,
"fallback": "{warnText}",
"slot_props": "{}"
}
],
"events": [
{ "type": "forwarded", "name": "click", "element": "div" },
{ "type": "forwarded", "name": "mouseover", "element": "div" },
{ "type": "forwarded", "name": "mouseenter", "element": "div" },
{ "type": "forwarded", "name": "mouseleave", "element": "div" },
{ "type": "forwarded", "name": "change", "element": "textarea" },
{ "type": "forwarded", "name": "input", "element": "textarea" },
{ "type": "forwarded", "name": "keydown", "element": "textarea" },
Expand Down Expand Up @@ -12738,14 +12782,8 @@
],
"moduleExports": [],
"slots": [],
"events": [
{ "type": "forwarded", "name": "click", "element": "div" },
{ "type": "forwarded", "name": "mouseover", "element": "div" },
{ "type": "forwarded", "name": "mouseenter", "element": "div" },
{ "type": "forwarded", "name": "mouseleave", "element": "div" }
],
"typedefs": [],
"rest_props": { "type": "Element", "name": "div" }
"events": [],
"typedefs": []
},
{
"moduleName": "TextInput",
Expand Down
30 changes: 27 additions & 3 deletions docs/src/pages/components/TextArea.svx
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,20 @@ components: ["TextArea", "TextAreaSkeleton"]

<TextArea labelText="App description" placeholder="Enter a description..." />

## Maximum character count
## Counter (character)

Specify the max character count using the `maxCount` prop. A character counter will be displayed to the right of the label.
Specify a number using the `maxCount` prop to show a character counter. The default mode is `"charcacter"`.

You can always use the native `maxlength` attribute if you'd prefer that a counter not be shown.
The `maxlength` attribute is set to `maxCount` in this mode. You can override this by specifying `maxlength={undefined}`.

<TextArea labelText="App description" placeholder="Enter a description..." maxCount={100} />

## Counter (word)

Specify `counterMode="word"` for the counter to count words instead of characters.

<TextArea labelText="App description" placeholder="Enter a description..." maxCount={100} counterMode="word" />

## With helper text

<TextArea labelText="App description" helperText="A rich description helps us better recommend related products and services" placeholder="Enter a description..." />
Expand All @@ -31,14 +37,32 @@ You can always use the native `maxlength` attribute if you'd prefer that a count

<TextArea light labelText="App description" placeholder="Enter a description..." />

## Read-only variant

<TextArea readonly labelText="App description" placeholder="Enter a description..." />

## Custom rows

Specify `rows` to adjust the height of the textarea.

By default, `rows` is set to `4`.

<TextArea rows={10} labelText="App description" placeholder="Enter a description..." />

## Custom cols

If `cols` is a number, the `textarea` will not be resizeable.

<TextArea cols={50} labelText="App description" placeholder="Enter a description..." />

## Invalid state

<TextArea invalid invalidText="Only plain text characters are allowed" labelText="App description" placeholder="Enter a description..." />

## Warning state

<TextArea warn warnText="The app description requires additional approval." labelText="App description" placeholder="Enter a description..." />

## Disabled state

<TextArea disabled labelText="App description" placeholder="Enter a description..." />
Expand Down
98 changes: 72 additions & 26 deletions src/TextArea/TextArea.svelte
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<script>
// @ts-check

/** Specify the textarea value */
export let value = "";

Expand All @@ -20,6 +22,12 @@
*/
export let maxCount = undefined;

/**
* Specify the counter mode
* @type {"character" | "word"}
*/
export let counterMode = "character";

/** Set to `true` to enable the light variant */
export let light = false;

Expand All @@ -44,6 +52,12 @@
/** Specify the text for the invalid state */
export let invalidText = "";

/** Set to `true` to indicate an warning state */
export let warn = false;

/** Specify the warning state text */
export let warnText = "";

/** Set an id for the textarea element */
export let id = "ccs-" + Math.random().toString(36);

Expand All @@ -57,22 +71,26 @@
export let ref = null;

import WarningFilled from "../icons/WarningFilled.svelte";

import WarningAltFilled from "../icons/WarningAltFilled.svelte";

/** @type {(value: string) => number}*/
function getTextCount(value) {
if (counterMode === "character") {
return value.length;
} else {
return value.match(/\w+/g)?.length || 0;
}
}

$: error = invalid && !readonly;
$: errorId = `error-${id}`;
$: helperId = `helper-${id}`;
$: warnId = `warn-${id}`;
</script>

<!-- svelte-ignore a11y-mouse-events-have-key-events -->
<!-- svelte-ignore a11y-click-events-have-key-events -->
<!-- svelte-ignore a11y-no-static-element-interactions -->
<div
on:click
on:mouseover
on:mouseenter
on:mouseleave
class:bx--form-item="{true}"
>
{#if (labelText || $$slots.labelText) && !hideLabel}
<div class:bx--text-area__label-wrapper="{true}">
<div class:bx--form-item="{true}">
<div class:bx--text-area__label-wrapper="{true}">
{#if (labelText || $$slots.labelText) && !hideLabel}
<label
for="{id}"
class:bx--label="{true}"
Expand All @@ -83,25 +101,37 @@
{labelText}
</slot>
</label>
{#if maxCount}
<div class:bx--label="{true}" class:bx--label--disabled="{disabled}">
{value.length}/{maxCount}
</div>
{/if}
</div>
{/if}
{/if}
{#if maxCount}
<div class:bx--label="{true}" class:bx--label--disabled="{disabled}">
{getTextCount(value)}/{maxCount}
</div>
{/if}
</div>
<div
class:bx--text-area__wrapper="{true}"
class:bx--text-area__wrapper--readonly="{readonly}"
class:bx--text-area__wrapper--warn="{warn}"
data-invalid="{invalid || undefined}"
>
{#if invalid}
<WarningFilled class="bx--text-area__invalid-icon" />
{:else if warn}
<WarningAltFilled
class="bx--text-area__invalid-icon bx--text-area__invalid-icon--warning"
/>
{/if}
<textarea
bind:this="{ref}"
bind:value
bind:value="{value}"
aria-invalid="{invalid || undefined}"
aria-describedby="{invalid ? errorId : undefined}"
aria-describedby="{error
? errorId
: warn
? warnId
: helperText
? helperId
: undefined}"
disabled="{disabled}"
id="{id}"
name="{name}"
Expand All @@ -112,8 +142,12 @@
class:bx--text-area="{true}"
class:bx--text-area--light="{light}"
class:bx--text-area--invalid="{invalid}"
maxlength="{maxCount ?? undefined}"
style="{cols ? '' : 'width: 100%;'}"
class:bx--text-area--warn="{warn}"
maxlength="{typeof maxCount === 'number' && counterMode === 'character'
? maxCount
: undefined}"
style:width="{cols ? undefined : "100%"}"
style:resize="{cols ? "none" : undefined}"
{...$$restProps}
on:change
on:input
Expand All @@ -125,13 +159,25 @@
</div>
{#if !invalid && helperText}
<div
id="{helperId}"
class:bx--form__helper-text="{true}"
class:bx--form__helper-text--disabled="{disabled}"
>
{helperText}
</div>
{/if}
{#if invalid}
<div id="{errorId}" class:bx--form-requirement="{true}">{invalidText}</div>
{#if !readonly}
{#if invalid}
<div id="{errorId}" class:bx--form-requirement="{true}">
<slot name="invalidText">
{invalidText}
</slot>
</div>
{/if}
{#if !invalid && warn}
<div class:bx--form-requirement="{true}" id="{warnId}">
<slot name="warnText">{warnText}</slot>
</div>
{/if}
{/if}
</div>
13 changes: 3 additions & 10 deletions src/TextArea/TextAreaSkeleton.svelte
Original file line number Diff line number Diff line change
@@ -1,18 +1,11 @@
<script>
// @ts-check

/** Set to `true` to visually hide the label text */
export let hideLabel = false;
</script>

<!-- svelte-ignore a11y-mouse-events-have-key-events -->
<!-- svelte-ignore a11y-no-static-element-interactions -->
<div
class:bx--form-item="{true}"
{...$$restProps}
on:click
on:mouseover
on:mouseenter
on:mouseleave
>
<div class:bx--form-item="{true}">
{#if !hideLabel}
<span class:bx--label="{true}" class:bx--skeleton="{true}"></span>
{/if}
Expand Down
Loading