Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EuiCodeEditor now assigns correct attributes to textarea #3212

Merged
merged 5 commits into from
Apr 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## [`master`](https://github.com/elastic/eui/tree/master)

- Added support for `href`, `onClick`, and related props in `EuiBasicTable` default actions
- Added support for `href`, `onClick`, and related props in `EuiBasicTable` default actions ([#3115](https://github.com/elastic/eui/pull/3115))
- Added support for `EuiCodeEditor` to set `readonly` and `id` on `<textarea />` ([#3212](https://github.com/elastic/eui/pull/3212))

**Deprecation**

Expand Down
22 changes: 21 additions & 1 deletion src/components/code_editor/code_editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import React, { Component, AriaAttributes, KeyboardEventHandler } from 'react';
import classNames from 'classnames';
import AceEditor, { IAceEditorProps } from 'react-ace';

import { keysOf } from '../common';
import { htmlIdGenerator, keyCodes } from '../../services';
import { EuiI18n } from '../i18n';

Expand Down Expand Up @@ -46,11 +47,13 @@ export interface EuiCodeEditorProps
* Use string for a built-in mode or object for a custom mode
*/
mode?: IAceEditorProps['mode'] | object;
id?: string;
}

export interface EuiCodeEditorState {
isHintActive: boolean;
isEditing: boolean;
name: string;
}

export class EuiCodeEditor extends Component<
Expand All @@ -64,6 +67,7 @@ export class EuiCodeEditor extends Component<
state: EuiCodeEditorState = {
isHintActive: true,
isEditing: false,
name: htmlIdGenerator()(),
};

idGenerator = htmlIdGenerator();
Expand Down Expand Up @@ -158,6 +162,22 @@ export class EuiCodeEditor extends Component<
if (this.isCustomMode()) {
this.setCustomMode();
}
const { isReadOnly, id } = this.props;

const textareaProps: {
id?: string;
readOnly?: boolean;
} = { id, readOnly: isReadOnly };

const el = document.getElementById(this.state.name);
if (el) {
const textarea = el.querySelector('textarea');
if (textarea)
keysOf(textareaProps).forEach(key => {
if (textareaProps[key])
textarea.setAttribute(`${key}`, textareaProps[key]!.toString());
});
}
}

componentDidUpdate(prevProps: EuiCodeEditorProps) {
Expand Down Expand Up @@ -260,7 +280,7 @@ export class EuiCodeEditor extends Component<
// Setting a default, existing `mode` is necessary to properly initialize the editor
// prior to dynamically setting a custom mode (https://github.com/elastic/eui/pull/2616)
mode={this.isCustomMode() ? DEFAULT_MODE : (mode as string)} // https://github.com/securingsincity/react-ace/pull/771
name={this.idGenerator()}
name={this.state.name}
theme={theme}
ref={this.aceEditorRef}
width={width}
Expand Down