Skip to content

Commit

Permalink
Update onBlur behavior for code editor (#71)
Browse files Browse the repository at this point in the history
* update onBlur behavior

* upd change log

* Update CHANGELOG.md

---------

Co-authored-by: Mikhail Volkov <[email protected]>
  • Loading branch information
vitPinchuk and mikhail-vl authored Nov 29, 2024
1 parent 8e0b080 commit 6576094
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 13 deletions.
7 changes: 7 additions & 0 deletions packages/components/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Change Log

## 3.6.0 (2024-11-29)

### Features / Enhancements

- Update AutosizeCodeEditor (#70)
- Update onBlur behavior for code editor (#71)

## 3.5.0 (2024-10-23)

### Features / Enhancements
Expand Down
2 changes: 1 addition & 1 deletion packages/components/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -89,5 +89,5 @@
"typecheck": "tsc --emitDeclarationOnly false --noEmit"
},
"types": "dist/index.d.ts",
"version": "3.5.1"
"version": "3.6.0"
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ jest.mock('@grafana/ui', () => ({
</>
);
}),
CodeEditor: jest.fn(({ value, onChange, height, onEditorDidMount }) => {
CodeEditor: jest.fn(({ value, onChange, onBlur, height, onEditorDidMount }) => {
/**
* Call the onEditorDidMount callback with the editor instance
*/
Expand All @@ -69,7 +69,8 @@ jest.mock('@grafana/ui', () => ({
{...InTestIds.field.apply()}
style={{ height }}
value={value}
onChange={(event) => onChange(event.currentTarget.value)}
onChange={(event) => onChange?.(event.currentTarget.value)}
onBlur={(event) => onBlur?.(event.currentTarget.value)}
/>
);
}),
Expand Down Expand Up @@ -121,6 +122,24 @@ describe('AutosizeCodeEditor', () => {
expect(selectors.field()).toHaveStyle(`height: 360px`);
});

it('Should not call onBlur if onBlur is not provided', () => {
const onBlur = jest.fn();
render(getComponent({}));

fireEvent.blur(selectors.field());
expect(onBlur).not.toHaveBeenCalled();
});

it('Should call onBlur if onBlur is provided', () => {
const onBlur = jest.fn();
render(getComponent({ onBlur, value: '1line\n' }));

fireEvent.blur(selectors.field());

expect(onBlur).toHaveBeenCalled();
expect(onBlur).toHaveBeenCalledWith('1line\\n');
});

it('Should update height if props changed', () => {
const { rerender } = render(getComponent({}));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ const getHeightByValue = (value: string, minHeight?: number, maxHeight?: number)
export const AutosizeCodeEditor: React.FC<Props> = ({
value,
onChange,
onBlur,
minHeight,
maxHeight,
height: staticHeight,
Expand Down Expand Up @@ -126,6 +127,29 @@ export const AutosizeCodeEditor: React.FC<Props> = ({
[monacoEditor, onEditorDidMount]
);

/**
* Change value
*/
const onChangeValue = useCallback(
(value: string) => {
const currentValue = value.replaceAll('\n', '\\n');
onChange?.(currentValue);
setHeight(getHeightByValue(value, minHeight, maxHeight));
},
[maxHeight, minHeight, onChange]
);

/**
* onBlur handler
*/
const onBlurUpdate = useCallback(
(value: string) => {
const currentValue = value.replaceAll('\n', '\\n');
onBlur?.(currentValue);
},
[onBlur]
);

/**
* Update Height on value change
*/
Expand All @@ -150,11 +174,8 @@ export const AutosizeCodeEditor: React.FC<Props> = ({
height={staticHeight ?? height}
monacoOptions={currentMonacoOptions}
onEditorDidMount={onEditorDidMountMain}
onChange={(value) => {
const currentValue = value.replaceAll('\n', '\\n');
onChange?.(currentValue);
setHeight(getHeightByValue(value, minHeight, maxHeight));
}}
onChange={onChangeValue}
onBlur={onBlurUpdate}
{...restProps}
/>

Expand Down Expand Up @@ -184,11 +205,8 @@ export const AutosizeCodeEditor: React.FC<Props> = ({
containerStyles={styles.modalEditor}
monacoOptions={currentMonacoOptions}
onEditorDidMount={modalEditorDidMount}
onChange={(value) => {
const currentValue = value.replaceAll('\n', '\\n');
onChange?.(currentValue);
setHeight(getHeightByValue(value, minHeight, maxHeight));
}}
onChange={onChangeValue}
onBlur={onBlurUpdate}
{...restProps}
/>
</div>
Expand Down

0 comments on commit 6576094

Please sign in to comment.