@focus-reactive/react-yaml

中文文档
1.1.2发布于 2022/06/23MITTypeScriptCommonJS近 30 天下载 3.7万

Yaml input field with validation and syntax highlighting as React Component

原始使用文档
文档目录5 节
  1. Install
  2. Usage
  3. Features
  4. API
  5. Credits

React-Yaml

Yaml input field with validation and syntax highlighting as React Component

Example

Demo

Install

npm i --save @focus-reactive/react-yaml
yarn add @focus-reactive/react-yaml

Usage

You can pass JS object as initial value:

import YamlEditor from '@focus-reactive/react-yaml';

const obj = { foo: 'bar' };
const handleChange = ({ json, text }) => {
  console.log(json);
  // { foo: 'bar' }
};

return <YamlEditor json={obj} onChange={handleChange} />;

Or you can pass a text with YAML syntax

import YamlEditor from '@focus-reactive/react-yaml';

const text = `
foo: bar
`;

const handleChange = ({ json, text }) => {
  console.log(text);
  // "foo: bar"
};

return <YamlEditor text={text} onChange={handleChange} />;

Features

  • themable (use Codemirror themes)
  • syntax and errors highlight
  • support working with text in YAML format or regular JS objects
  • controlled and uncontrolled mode

API

You can pass the following props to the YamlEditor

json - JS object that will be outputted in Editor in YAML syntax

text - Text in YAML syntax. Note: if you pass both json and text props - the json will be used.

Note: By default the Editor will work in uncontrolled mode. Means that changes in json or text props wouldn't cause code update. You can control this behavior with merge prop, that accept a function with which you can define how to update text in the Editor.

theme - Codemirror Theme to style editor. e.g.

import { oneDark } from '@codemirror/theme-one-dark';

onChange - a callback triggered on VALID YAML code changes in the Editor. It will be launched with the following parameters:

onChange({ json, text });

where: text is a last valid text editing in Editor, json is JS equivalent.

onSelect - a callback triggered on text selection. It will be launched with the following parameters:

onSelect({ selected, from, to });

where: selected - is a selected text, from and to - selection positions

onSetCursor - a callback triggered when user moves or set cursor on any word in the Editor. It will be launched with the following parameters:

onSetCursor({ word, from, to });

where: word - is a word separated by whitespaces, from and to - word start and end positions

onError - a callback triggered on any INVALID code changes in the Editor. It will be launched with the following parameters:

onError(errorObject | null);

where errorObject is Codemirror error object representing YAML syntax error. Note that after a user fixed syntax error in the Editor, the function will be launched with null.

merge (optional) - a function launched on json or text props changes. It can compare the new and the current values and return the result that will appear in the Editor. It will be launched with the following parameters:

merge({ json, text, currentText });

where json - the new json prop, text the new text prop, and currentText is the current text.

It should return an object with one of the keys: json or text which should contain new text of js value.

By default it returns currentText

Actions - allow to manipulate Editor in imperative form

All actions are available by a ref prop after the Editor is maintained:

const EditorWithActions = () => {
  const actions = React.useRef(null);

  React.useEffect(() => {
    // Here we have access to imperative actions
    actions.current.replaceValue({ json: { foo: 'updatedValue' } });
  }, []);

  return (
    <div>
      <YamlEditor json={{ foo: 'initValue' }} ref={actions} />
    </div>
  );
};

Currently available the following actions:

replaceValue - replace entire Editor content with a new value (json or text)

replaceValue({ json, text });

Credits

Created with ❤︎ to React and Open Source community by Oleg Proskurin at FocusReactive

2021

FocusReactive

版本与发布记录

@focus-reactive/react-yaml 共发布 5 个版本,首次发布于 2021/09/11;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.1.2latest2022/06/234 年前
1.1.12022/06/234 年前
1.1.02022/06/214 年前
1.0.02021/09/235 年前
0.1.02021/09/115 年前

关于 @focus-reactive/react-yaml 的常见问题

@focus-reactive/react-yaml 是什么?

Yaml input field with validation and syntax highlighting as React Component

如何安装 @focus-reactive/react-yaml?

在项目目录运行 npm install @focus-reactive/react-yaml,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:react(*)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@focus-reactive/react-yaml 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@focus-reactive/react-yaml 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@focus-reactive/react-yaml 可以商用吗?

@focus-reactive/react-yaml 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@focus-reactive/react-yaml 的下载量有多少?

根据 npm 官方下载统计,@focus-reactive/react-yaml 在 2026/10/15 – 2026/11/13 这 30 天内被下载 37,203 次,日均约 1,240 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@focus-reactive/react-yaml 还在维护吗?

最新版本 1.1.2 发布于 2022/06/23(4 年前);至今共发布 5 个版本,其中过去 12 个月发布了 0 个;首次发布于 2021/09/11。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。