# Textarea

Kolom teks beberapa baris dengan label, petunjuk, dan pesan error yang terhubung lewat ARIA.

- Kategori: Form
- Status: stable
- Registry: https://markas.jenderalcorp.com/r/textarea.json
- CLI: `npx markasui@latest add textarea`

## Kapan dipakai

Isian teks panjang di form: bio, alamat, catatan, pesan.

- Selalu isi label, jangan hanya placeholder
- Pakai Input untuk teks satu baris

## Kode

### Blade

Simpan di `resources/views/components/markas/textarea.blade.php`. Tanpa dependensi tambahan.

```blade
@props([
    'label' => null,
    'hint' => null,
    'error' => null,
    'value' => null,
    'rows' => 4,
])

@php
    $wrapper = 'grid gap-1.5';
    $labelClass = 'text-sm font-medium text-ink';
    $field = 'block w-full resize-y rounded-md border border-line bg-surface px-3 py-2 text-sm text-ink transition-colors duration-150 ease-out placeholder:text-faint focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-primary disabled:cursor-not-allowed disabled:opacity-50 aria-invalid:border-danger motion-reduce:transition-none';
    $hintClass = 'text-sm text-muted';
    $errorClass = 'text-sm text-danger';

    // Same id on every render, so Livewire's morph (which matches elements by id) keeps the element. Pass `id` to set it.
    $idKey = 'markas-textarea|'.(isset($this) && $this instanceof \Livewire\Component ? $this->getId() : '').'|'.($attributes->get('wire:key') ?? $attributes->whereStartsWith('wire:model')->first() ?? $attributes->get('name')).'|';
    for ($idIndex = 0; $__env->hasRenderedOnce($idKey.$idIndex); $idIndex++);
    $__env->markAsRenderedOnce($idKey.$idIndex);
    $id = $attributes->get('id') ?? 'markas-textarea-'.hash('xxh32', $idKey.$idIndex);
    $describedBy = $error ? $id.'-error' : ($hint ? $id.'-hint' : null);
@endphp

<div class="{{ $wrapper }}">
    @if ($label)
        <label for="{{ $id }}" class="{{ $labelClass }}">{{ $label }}</label>
    @endif
    <textarea {{ $attributes->merge(['id' => $id, 'rows' => $rows, 'aria-invalid' => $error ? 'true' : null, 'aria-describedby' => $describedBy])->class([$field]) }}>{{ $value }}</textarea>
    @if ($error)
        <p id="{{ $id }}-error" class="{{ $errorClass }}">{{ $error }}</p>
    @elseif ($hint)
        <p id="{{ $id }}-hint" class="{{ $hintClass }}">{{ $hint }}</p>
    @endif
</div>
```

```blade
<x-markas.textarea name="bio" label="Bio singkat" hint="Maksimal 200 karakter." />
<x-markas.textarea name="alamat" label="Alamat" :rows="2" :value="old('alamat')" :error="$errors->first('alamat')" />
```

### React

Simpan di `src/components/markas/Textarea.tsx`. Tanpa dependensi tambahan.

```tsx
import { useId, type ComponentProps, type ReactNode } from "react";

const wrapper = "grid gap-1.5";
const labelClass = "text-sm font-medium text-ink";
const field =
  "block w-full resize-y rounded-md border border-line bg-surface px-3 py-2 text-sm text-ink transition-colors duration-150 ease-out placeholder:text-faint focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-primary disabled:cursor-not-allowed disabled:opacity-50 aria-invalid:border-danger motion-reduce:transition-none";
const hintClass = "text-sm text-muted";
const errorClass = "text-sm text-danger";
const cx = (...c: (string | false | null | undefined)[]) =>
  c.filter(Boolean).join(" ");

export type TextareaProps = ComponentProps<"textarea"> & {
  label?: ReactNode;
  hint?: ReactNode;
  error?: ReactNode;
};

export function Textarea({
  label,
  hint,
  error,
  id,
  rows = 4,
  className,
  ...props
}: TextareaProps) {
  const generatedId = useId();
  const textareaId = id ?? generatedId;
  const describedBy = error
    ? `${textareaId}-error`
    : hint
      ? `${textareaId}-hint`
      : undefined;

  return (
    <div className={wrapper}>
      {label && (
        <label htmlFor={textareaId} className={labelClass}>
          {label}
        </label>
      )}
      <textarea
        id={textareaId}
        rows={rows}
        aria-invalid={error ? true : undefined}
        aria-describedby={describedBy}
        className={cx(field, className)}
        {...props}
      />
      {error ? (
        <p id={`${textareaId}-error`} className={errorClass}>
          {error}
        </p>
      ) : hint ? (
        <p id={`${textareaId}-hint`} className={hintClass}>
          {hint}
        </p>
      ) : null}
    </div>
  );
}
```

```tsx
import { Textarea } from "@/components/markas/Textarea";

<Textarea name="bio" label="Bio singkat" hint="Maksimal 200 karakter." />
<Textarea name="alamat" label="Alamat" rows={2} error={errors.alamat} />
```

### Vue

Simpan di `src/components/markas/Textarea.vue`. Tanpa dependensi tambahan.

```vue
<script setup lang="ts">
import { computed, useId } from "vue";

defineOptions({ inheritAttrs: false });

const props = withDefaults(
  defineProps<{
    label?: string;
    hint?: string;
    error?: string;
    id?: string;
    rows?: number;
  }>(),
  {
    label: undefined,
    hint: undefined,
    error: undefined,
    id: undefined,
    rows: 4,
  },
);
const model = defineModel<string>();

const wrapper = "grid gap-1.5";
const labelClass = "text-sm font-medium text-ink";
const field =
  "block w-full resize-y rounded-md border border-line bg-surface px-3 py-2 text-sm text-ink transition-colors duration-150 ease-out placeholder:text-faint focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-primary disabled:cursor-not-allowed disabled:opacity-50 aria-invalid:border-danger motion-reduce:transition-none";
const hintClass = "text-sm text-muted";
const errorClass = "text-sm text-danger";

const generatedId = useId();
const textareaId = computed(() => props.id ?? generatedId);
const describedBy = computed(() =>
  props.error
    ? `${textareaId.value}-error`
    : props.hint
      ? `${textareaId.value}-hint`
      : undefined,
);
</script>

<template>
  <div :class="wrapper">
    <label v-if="props.label" :for="textareaId" :class="labelClass">{{
      props.label
    }}</label>
    <textarea
      :id="textareaId"
      v-model="model"
      :rows="props.rows"
      :aria-invalid="props.error ? 'true' : undefined"
      :aria-describedby="describedBy"
      :class="field"
      v-bind="$attrs"
    />
    <p v-if="props.error" :id="`${textareaId}-error`" :class="errorClass">
      {{ props.error }}
    </p>
    <p v-else-if="props.hint" :id="`${textareaId}-hint`" :class="hintClass">
      {{ props.hint }}
    </p>
  </div>
</template>
```

```vue
<script setup lang="ts">
import { ref } from "vue";
import Textarea from "@/components/markas/Textarea.vue";

const bio = ref("");
</script>

<template>
  <Textarea
    v-model="bio"
    name="bio"
    label="Bio singkat"
    hint="Maksimal 200 karakter."
  />
</template>
```

### Svelte

Simpan di `src/lib/components/markas/Textarea.svelte`. Tanpa dependensi tambahan.

```svelte
<script lang="ts">
  import type { HTMLTextareaAttributes } from "svelte/elements";

  const wrapper = "grid gap-1.5";
  const labelClass = "text-sm font-medium text-ink";
  const field =
    "block w-full resize-y rounded-md border border-line bg-surface px-3 py-2 text-sm text-ink transition-colors duration-150 ease-out placeholder:text-faint focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-primary disabled:cursor-not-allowed disabled:opacity-50 aria-invalid:border-danger motion-reduce:transition-none";
  const hintClass = "text-sm text-muted";
  const errorClass = "text-sm text-danger";

  type Props = HTMLTextareaAttributes & {
    label?: string;
    hint?: string;
    error?: string;
  };

  let {
    label,
    hint,
    error,
    id,
    rows = 4,
    value = $bindable(),
    class: className,
    ...rest
  }: Props = $props();

  const generatedId = $props.id();
  const textareaId = $derived(id ?? generatedId);
  const describedBy = $derived(
    error ? `${textareaId}-error` : hint ? `${textareaId}-hint` : undefined,
  );
</script>

<div class={wrapper}>
  {#if label}
    <label for={textareaId} class={labelClass}>{label}</label>
  {/if}
  <textarea
    id={textareaId}
    {rows}
    bind:value
    aria-invalid={error ? "true" : undefined}
    aria-describedby={describedBy}
    class={[field, className]}
    {...rest}></textarea>
  {#if error}
    <p id={`${textareaId}-error`} class={errorClass}>{error}</p>
  {:else if hint}
    <p id={`${textareaId}-hint`} class={hintClass}>{hint}</p>
  {/if}
</div>
```

```svelte
<script lang="ts">
  import Textarea from "$lib/components/markas/Textarea.svelte";

  let bio = $state("");
</script>

<Textarea
  bind:value={bio}
  name="bio"
  label="Bio singkat"
  hint="Maksimal 200 karakter."
/>
```

### Astro

Simpan di `src/components/markas/Textarea.astro`. Tanpa dependensi tambahan.

```astro
---
import type { HTMLAttributes } from "astro/types";

const wrapper = "grid gap-1.5";
const labelClass = "text-sm font-medium text-ink";
const field =
  "block w-full resize-y rounded-md border border-line bg-surface px-3 py-2 text-sm text-ink transition-colors duration-150 ease-out placeholder:text-faint focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-primary disabled:cursor-not-allowed disabled:opacity-50 aria-invalid:border-danger motion-reduce:transition-none";
const hintClass = "text-sm text-muted";
const errorClass = "text-sm text-danger";

type Props = HTMLAttributes<"textarea"> & {
  label?: string;
  hint?: string;
  error?: string;
  /** Initial text. A textarea has no value attribute, so it is rendered as content. */
  value?: string;
};

const {
  label,
  hint,
  error,
  id,
  rows = 4,
  value,
  class: className,
  ...rest
} = Astro.props;
const textareaId = id ?? `markas-textarea-${crypto.randomUUID().slice(0, 8)}`;
const describedBy = error
  ? `${textareaId}-error`
  : hint
    ? `${textareaId}-hint`
    : undefined;
---

<div class={wrapper}>
  {
    label && (
      <label for={textareaId} class={labelClass}>
        {label}
      </label>
    )
  }
  <textarea
    id={textareaId}
    rows={rows}
    aria-invalid={error ? "true" : undefined}
    aria-describedby={describedBy}
    class:list={[field, className]}
    {...rest}>{value}</textarea>
  {
    error ? (
      <p id={`${textareaId}-error`} class={errorClass}>
        {error}
      </p>
    ) : hint ? (
      <p id={`${textareaId}-hint`} class={hintClass}>
        {hint}
      </p>
    ) : null
  }
</div>
```

```astro
---
import Textarea from "@/components/markas/Textarea.astro";
---

<Textarea name="bio" label="Bio singkat" hint="Maksimal 200 karakter." />
<Textarea name="address" label="Alamat" rows={2} value="Jl. Merdeka 12" error="Alamat belum lengkap." />
```

Tanpa script klien. Isi awal dikirim lewat `value`, sama seperti Blade.

### Angular

Simpan di `src/app/markas/textarea.component.ts`. Tanpa dependensi tambahan.

```ts
import {
  booleanAttribute,
  ChangeDetectionStrategy,
  Component,
  computed,
  input,
  model,
  numberAttribute,
} from "@angular/core";

const wrapper = "grid gap-1.5";
const labelClass = "text-sm font-medium text-ink";
const field =
  "block w-full resize-y rounded-md border border-line bg-surface px-3 py-2 text-sm text-ink transition-colors duration-150 ease-out placeholder:text-faint focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-primary disabled:cursor-not-allowed disabled:opacity-50 aria-invalid:border-danger motion-reduce:transition-none";
const hintClass = "text-sm text-muted";
const errorClass = "text-sm text-danger";

let nextId = 0;

/**
 * Host stays out of layout (display: contents). id, name, aria-label, aria-invalid,
 * aria-describedby, placeholder, and class move to the textarea, so the host attributes are cleared.
 */
@Component({
  selector: "markas-textarea",
  changeDetection: ChangeDetectionStrategy.OnPush,
  host: {
    class: "contents",
    "[attr.id]": "null",
    "[attr.name]": "null",
    "[attr.aria-label]": "null",
    "[attr.aria-invalid]": "null",
    "[attr.aria-describedby]": "null",
    "[attr.placeholder]": "null",
  },
  template: `
    <div [class]="wrapper">
      @if (label()) {
        <label [for]="textareaId()" [class]="labelClass">{{ label() }}</label>
      }
      <textarea
        #field
        [id]="textareaId()"
        [rows]="rows()"
        [value]="value()"
        [disabled]="disabled()"
        [readOnly]="readonly()"
        [required]="required()"
        [attr.name]="name() ?? null"
        [attr.placeholder]="placeholder() ?? null"
        [attr.maxlength]="maxlength() ?? null"
        [attr.aria-label]="ariaLabel() ?? null"
        [attr.aria-invalid]="invalid()"
        [attr.aria-describedby]="describedBy()"
        [class]="fieldClass()"
        (input)="value.set(field.value)"
      ></textarea>
      @if (error()) {
        <p [id]="textareaId() + '-error'" [class]="errorClass">{{ error() }}</p>
      } @else if (hint()) {
        <p [id]="textareaId() + '-hint'" [class]="hintClass">{{ hint() }}</p>
      }
    </div>
  `,
})
export class Textarea {
  readonly label = input<string>();
  readonly hint = input<string>();
  readonly error = input<string>();
  readonly id = input<string>();
  readonly rows = input(4, { transform: numberAttribute });
  readonly name = input<string>();
  readonly placeholder = input<string>();
  readonly maxlength = input<number | undefined, unknown>(undefined, {
    transform: (value: unknown) =>
      value == null ? undefined : numberAttribute(value),
  });
  readonly disabled = input(false, { transform: booleanAttribute });
  readonly readonly = input(false, { transform: booleanAttribute });
  readonly required = input(false, { transform: booleanAttribute });
  readonly ariaLabel = input<string | undefined>(undefined, {
    alias: "aria-label",
  });
  /** Marks the field invalid without an error text, for example when the message sits elsewhere. */
  readonly ariaInvalid = input<boolean | "true" | "false" | undefined>(
    undefined,
    { alias: "aria-invalid" },
  );
  /** Extra description id, used when there is no error or hint. */
  readonly ariaDescribedby = input<string | undefined>(undefined, {
    alias: "aria-describedby",
  });
  readonly userClass = input("", { alias: "class" });
  /** Two-way bindable with [(value)]. */
  readonly value = model("");

  protected readonly wrapper = wrapper;
  protected readonly labelClass = labelClass;
  protected readonly hintClass = hintClass;
  protected readonly errorClass = errorClass;
  private readonly generatedId = `markas-textarea-${nextId++}`;
  protected readonly textareaId = computed(() => this.id() ?? this.generatedId);
  protected readonly describedBy = computed(() =>
    this.error()
      ? `${this.textareaId()}-error`
      : this.hint()
        ? `${this.textareaId()}-hint`
        : (this.ariaDescribedby() ?? null),
  );
  protected readonly invalid = computed(() =>
    this.error() ? "true" : (this.ariaInvalid()?.toString() ?? null),
  );
  protected readonly fieldClass = computed(() =>
    [field, this.userClass()].filter(Boolean).join(" "),
  );
}
```

```ts
import { Component, signal } from "@angular/core";
import { Textarea } from "./markas/textarea.component";

@Component({
  selector: "app-profile",
  imports: [Textarea],
  template: `
    <markas-textarea
      [(value)]="bio"
      name="bio"
      label="Bio singkat"
      hint="Maksimal 200 karakter."
    />
  `,
})
export class Profile {
  readonly bio = signal("");
}
```

## Props

| Nama | Tipe | Default | Keterangan |
| --- | --- | --- | --- |
| `label` | `string` | - | Teks label yang terhubung ke textarea lewat for/id. |
| `hint` | `string` | - | Teks petunjuk di bawah textarea. Disembunyikan saat ada error. |
| `error` | `string` | - | Pesan error. Mengaktifkan aria-invalid dan border danger. |
| `rows` | `number` | 4 | Jumlah baris yang terlihat. Pengguna tetap bisa mengubah tinggi secara vertikal. |
| `value / defaultValue / v-model / bind:value / [(value)]` | `string` | - | Isi awal. Blade dan Astro memakai prop value, React value atau defaultValue, Vue v-model, Svelte bind:value, Angular value atau [(value)]. |
| `id` | `string` | auto | Id textarea. Dibuat otomatis kalau kosong. |

## Aksesibilitas

- Label terhubung ke textarea lewat atribut for dan id.
- Pesan error dan petunjuk dibacakan screen reader lewat aria-describedby.
- aria-invalid aktif saat prop error terisi.

## Catatan

- Atribut tambahan (`name`, `placeholder`, `maxlength`, `class`) diteruskan ke elemen `<textarea>`, bukan ke pembungkusnya.
- Di Blade, isi awal diberikan lewat prop `value` karena textarea tidak punya atribut value.
- Komponen ini tidak punya teks bawaan. Semua teks berasal dari prop, jadi cukup isi `label`, `hint`, dan `error` dalam Bahasa Indonesia.
