@hep-code-runner/react 3.2.2 → 3.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -35,18 +35,20 @@ function App() {
35
35
  | 属性 | 类型 | 默认值 | 说明 |
36
36
  | -------------------- | ------------------------ | ------------- | ------------------- |
37
37
  | pistonUrl | string | '/api/piston' | Piston API 地址 |
38
- | language | string | 'javascript' | 默认语言 |
38
+ | language | string | 'javascript' | 当前语言(受控) |
39
+ | code | string | '' | 当前代码(受控) |
39
40
  | theme | 'light' \| 'dark' | 'light' | 主题 |
40
41
  | themeColor | string | - | 自定义主题色 |
41
42
  | showLanguageSelector | boolean | true | 显示语言选择器 |
42
43
  | showEditor | boolean | true | 显示代码编辑器 |
43
44
  | editable | boolean | true | 是否可编辑 |
44
- | defaultCode | string | '' | 默认代码 |
45
45
  | executorLabel | string | '运行' | 运行按钮文字 |
46
46
  | onExecuteStart | () => void | - | 代码开始执行回调 |
47
47
  | onExecuteEnd | (result) => void | - | 代码执行完成回调 |
48
48
  | onLanguageChange | (language, code) => void | - | 语言切换回调 |
49
- | onChange | (code: string) => void | - | 代码变化回调(防抖)|
49
+ | onLanguageUpdate | (language: string) => void | - | 语言更新回调 |
50
+ | onCodeUpdate | (code: string) => void | - | 代码更新回调 |
51
+ | onChange | (code: string) => void | - | 代码变化回调(实时)|
50
52
 
51
53
  ## 主题切换
52
54
 
@@ -91,6 +93,29 @@ function App() {
91
93
  <CodeRunner theme="dark" language="python" />
92
94
  ```
93
95
 
96
+ ### 受控模式
97
+
98
+ 组件完全受控,父组件通过 props + 回调控制 language 和 code:
99
+
100
+ ```jsx
101
+ import { useState } from 'react';
102
+ import { CodeRunner } from "@hep-code-runner/react";
103
+
104
+ function App() {
105
+ const [currentLanguage, setCurrentLanguage] = useState('javascript');
106
+ const [currentCode, setCurrentCode] = useState('console.log("hello")');
107
+
108
+ return (
109
+ <CodeRunner
110
+ language={currentLanguage}
111
+ code={currentCode}
112
+ onLanguageUpdate={setCurrentLanguage}
113
+ onCodeUpdate={setCurrentCode}
114
+ />
115
+ );
116
+ }
117
+ ```
118
+
94
119
  ### 监听事件
95
120
 
96
121
  ```jsx
package/dist/index.d.ts CHANGED
@@ -25,16 +25,18 @@ export declare interface CodeRunnerDialogRef {
25
25
  export declare interface CodeRunnerProps {
26
26
  pistonUrl?: string;
27
27
  language?: string;
28
+ code?: string;
28
29
  theme?: "light" | "dark";
29
30
  themeColor?: string;
30
31
  showLanguageSelector?: boolean;
31
32
  showEditor?: boolean;
32
33
  editable?: boolean;
33
- defaultCode?: string;
34
34
  executorLabel?: string;
35
35
  onExecuteStart?: () => void;
36
36
  onExecuteEnd?: (result: ExecuteResult) => void;
37
37
  onLanguageChange?: (language: string, code: string) => void;
38
+ onLanguageUpdate?: (language: string) => void;
39
+ onCodeUpdate?: (code: string) => void;
38
40
  onChange?: (code: string) => void;
39
41
  }
40
42