Skip to content
weftGitHub

JsonEditor

A code field for JSON that points to the line with the mistake.

SourcePackage @weft-js/warp

Preview

tsx
import { JsonEditor } from '@weft-js/warp'export function Basic() {  return (    <JsonEditor label="weft.json" defaultValue={CONFIG} rows={7} />  )}const CONFIG = `{  "region": "eu-west",  "copies": 3,  "healthCheck": "/health"}`

Installation

npm install @weft-js/warp

Usage

tsx
import { JsonEditor } from '@weft-js/warp'<JsonEditor label="weft.json" defaultValue={CONFIG} rows={7} />

A parse error

Delete the trailing comma and the error goes.

tsx
import { JsonEditor } from '@weft-js/warp'export function Broken() {  return (    <JsonEditor label="weft.json" defaultValue={'{\n  "region": "eu-west",\n  "copies": 3,\n}'} rows={5} />  )}

Your own check

schema gets the parsed value and returns a message. Change the value to 3.

tsx
import { JsonEditor } from '@weft-js/warp'export function Schema() {  return (    <JsonEditor label="weft.json" defaultValue={BAD_CONFIG} rows={7} schema={checkConfig} />  )}const CONFIG = `{  "region": "eu-west",  "copies": 3,  "healthCheck": "/health"}`const BAD_CONFIG = CONFIG.replace('3', '"three"')const checkConfig = (v: unknown) => (typeof v === 'object' && v !== null && 'copies' in v && typeof v.copies === 'number' && v.copies > 0 ? null : '"copies" must be a number above zero.')

Empty, filled, invalid, disabled and read-only

The line under the field reports on the JSON.

tsx
import { Grid, JsonEditor, Stack, Text } from '@weft-js/warp'export function States() {  return (    <Grid cols={3} gap="group">      <Cell name="Empty"><JsonEditor label="weft.json" rows={3} /></Cell>      <Cell name="Filled"><JsonEditor label="weft.json" rows={3} defaultValue={'{ "copies": 3 }'} /></Cell>      <Cell name="Invalid"><JsonEditor label="weft.json" rows={3} defaultValue={'{ "copies": 0 }'} schema={checkConfig} /></Cell>      <Cell name="Disabled"><JsonEditor label="weft.json" rows={3} defaultValue={'{ "copies": 3 }'} disabled /></Cell>      <Cell name="Read-only"><JsonEditor label="weft.json" rows={3} defaultValue={'{ "copies": 3 }'} readonly /></Cell>    </Grid>  )}const Cell = (p: { name: string; children?: unknown }) => (  <Stack gap="tight">    <Text role="caption" subtle>{p.name}</Text>    {p.children}  </Stack>)const checkConfig = (v: unknown) => (typeof v === 'object' && v !== null && 'copies' in v && typeof v.copies === 'number' && v.copies > 0 ? null : '"copies" must be a number above zero.')

Accessibility

Tab
Indents
EscthenTab
Leaves the field

API Reference

PropTypeDefaultDescription
labelstring—The visible name, and what screen readers announce.
hintunknown—A short line of help under the field.
errorunknown—Replaces the hint and marks the field invalid.
iconIconRef—Leading icon: it sweeps with the border.
prefixunknown—Text or an element inside the field before the value, such as $.
suffixunknown—Text or an element inside the field after the value, such as kg.
variant"floating" | "classic"—floating (default, or the app's warp({ fields })) · classic: the label above.
appearance"line"—line: only a bottom line under the value, no box or fill. The boxed field otherwise.
size"sm" | "md" | "lg"'md'Control size. md by default.
clearablebooleanfalseA clear button (shown once there is a value).
counternumber—Maximum length, shown as a counter.
idstring—The element id. One is made for you if you leave it out.
disabledbooleanfalseCan't be focused or changed.
readonlybooleanfalseShows the value and lets it be selected and copied, but not changed.
namestring—Posts the value with a form.
valuestring | State<string>—The current value, when you own the state.
defaultValuestring—The starting value, when the component owns the state.
lineNumbersbooleanfalseNumbers each line in a gutter. Default true; off while wrap is on.
tabSize2 | 422 or 4 spaces, used by Format too.
wrapbooleanfalseSoft-wraps long lines; line numbers then give way, since a number no longer means one row.
rowsnumber—The height in lines.
schema((value: unknown) => string | null | undefined)—Checks the parsed value. What it returns shows as the error.
EventTypeDefaultDescription
onInput((e: InputEvent & { currentTarget: HTMLInputElement & HTMLTextAreaElemen…—Called on each keystroke.
onValueChange((v: string, reason: ChangeReason) => void)—Called with each new value, and whether a pointer, a key or code made the change.
onReset(() => void)—After a form's reset put the value back.

Parts

Every element carries a stable name, so you can style it from your own CSS: [data-scope="json-editor"] [data-part="suffix"]

suffix

RelatedCode inputKey value editor