Skip to content

Commit d1e6b9c

Browse files
committed
feat: adjust clear button to undoable reset to default prefill
Closes #76
1 parent e88295e commit d1e6b9c

File tree

5 files changed

+27
-15
lines changed

5 files changed

+27
-15
lines changed

lib/components/Input/Input.jsx

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React from 'react';
1+
import React, { useRef } from 'react';
22

33
import { Link } from '@carbon/react';
44
import { Launch } from '@carbon/icons-react';
@@ -13,12 +13,19 @@ export default function Input({
1313
onSetInput,
1414
variablesForElement
1515
}) {
16-
const handleResetInput = () => {
16+
17+
const containerRef = /** @type {import('react').RefObject<HTMLDivElement | null>} */ (useRef(null));
18+
19+
const handleReset = () => {
1720
onResetInput();
21+
const cmContent = /** @type {HTMLElement | undefined} */ (
22+
containerRef.current?.querySelector('.cm-content')
23+
);
24+
cmContent?.focus();
1825
};
1926

2027
return (
21-
<div className="input">
28+
<div className="input" ref={ containerRef }>
2229
<div className="input__header">
2330
<div className="input__header--title">
2431
Input process variables
@@ -28,8 +35,8 @@ export default function Input({
2835
</Link>
2936
<Link
3037
className="input__header--button-reset"
31-
onClick={ handleResetInput }
32-
role="button">Clear</Link>
38+
onClick={ handleReset }
39+
role="button">Reset</Link>
3340
</div>
3441
<InputEditor
3542
allOutputs={ allOutputs }

lib/components/Input/InputEditor.jsx

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import React, { useEffect, useMemo, useRef, useState } from 'react';
22
import { renderToStaticMarkup } from 'react-dom/server';
33

44
import { autocompletion, closeBrackets } from '@codemirror/autocomplete';
5-
import { defaultKeymap } from '@codemirror/commands';
5+
import { defaultKeymap, history, historyKeymap } from '@codemirror/commands';
66
import { bracketMatching, indentOnInput } from '@codemirror/language';
77
import { Compartment, EditorState, Annotation } from '@codemirror/state';
88
import { EditorView, keymap, placeholder } from '@codemirror/view';
@@ -100,8 +100,10 @@ export default function InputEditor({
100100
closeBrackets(),
101101
bracketMatching(),
102102
indentOnInput(),
103+
history(),
103104
keymap.of([
104-
...defaultKeymap
105+
...defaultKeymap,
106+
...historyKeymap
105107
]),
106108
new Compartment().of(json()),
107109
new Compartment().of(EditorState.tabSize.of(2)),
@@ -249,4 +251,5 @@ function getDetail(value) {
249251
}
250252

251253
return type.charAt(0).toUpperCase() + type.slice(1);
252-
}
254+
}
255+

lib/utils/prefill.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { has, isObject, isString } from 'min-dash';
22

3-
export const DEFAULT_INPUT_CONFIG = '{}';
3+
export const DEFAULT_INPUT_CONFIG = '{\n \n}';
44

55

66
/**

test/ElementConfig.spec.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ describe('ElementConfig', function() {
112112
// then
113113
const inputConfigForElement = elementConfig.getInputConfigForElement(element);
114114

115-
expect(inputConfigForElement).to.eql('{}');
115+
expect(inputConfigForElement).to.eql('{\n \n}');
116116

117117
expect(spy).to.have.been.calledOnce;
118118
}));
@@ -195,7 +195,7 @@ describe('ElementConfig', function() {
195195
const inputConfigForElement = elementConfig.getInputConfigForElement(element);
196196

197197
// then
198-
expect(inputConfigForElement).to.eql('{}');
198+
expect(inputConfigForElement).to.eql('{\n \n}');
199199
}));
200200

201201
});

test/components/Input/Input.spec.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,9 @@ function renderWithProps(props) {
3838
const {
3939
element = elementRegistry.get('ServiceTask_1'),
4040
input = '{}',
41-
setInput = () => {},
42-
reset = () => {},
41+
onSetInput = () => {},
42+
onResetInput = () => {},
43+
onErrorChange = () => {},
4344
variablesForElement,
4445
output,
4546
onRunTask = () => {}
@@ -49,8 +50,9 @@ function renderWithProps(props) {
4950
<Input
5051
element={ element }
5152
input={ input }
52-
setInput={ setInput }
53-
reset={ reset }
53+
onSetInput={ onSetInput }
54+
onResetInput={ onResetInput }
55+
onErrorChange={ onErrorChange }
5456
variablesForElement={ variablesForElement }
5557
output={ output }
5658
onRunTask={ onRunTask }

0 commit comments

Comments
 (0)