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

feat: readonly text area #12388

Merged
merged 8 commits into from
Dec 2, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
Original file line number Diff line number Diff line change
Expand Up @@ -7604,6 +7604,9 @@ Map {
"placeholder": Object {
"type": "string",
},
"readOnly": Object {
"type": "bool",
},
"rows": Object {
"type": "number",
},
Expand Down
13 changes: 10 additions & 3 deletions packages/react/src/components/TextArea/TextArea.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ const TextArea = React.forwardRef(function TextArea(
aria-invalid={invalid || null}
aria-describedby={invalid ? errorId : null}
disabled={other.disabled}
readOnly={other.readOnly}
/>
);

Expand All @@ -133,7 +134,9 @@ const TextArea = React.forwardRef(function TextArea(
{counter}
</div>
<div
className={`${prefix}--text-area__wrapper`}
className={classNames(`${prefix}--text-area__wrapper`, {
[`${prefix}--text-area__wrapper--readonly`]: other.readOnly,
})}
data-invalid={invalid || null}>
{invalid && !isFluid && (
<WarningFilled className={`${prefix}--text-area__invalid-icon`} />
Expand Down Expand Up @@ -239,9 +242,13 @@ TextArea.propTypes = {
placeholder: PropTypes.string,

/**
* Specify the rows attribute for the `<textarea>`
* Whether the textarea should be read-only
*/
rows: PropTypes.number,
readOnly: PropTypes.bool,

/**
* Specify the rows attribute for the `<textarea>`
*/ rows: PropTypes.number,
lee-chase marked this conversation as resolved.
Show resolved Hide resolved

/**
* Provide the current value of the `<textarea>`
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**
* Copyright IBM Corp. 2016, 2018
*
* This source code is licensed under the Apache-2.0 license found in the
* LICENSE file in the root directory of this source tree.
*/

import React from 'react';
import TextArea from '../TextArea';
import userEvent from '@testing-library/user-event';
import { render, screen } from '@testing-library/react';

describe('TextArea', () => {
describe('behaves as expected - Component API', () => {
it('should respect readOnly prop', () => {
const onChange = jest.fn();
const onClick = jest.fn();
render(
<TextArea
id="input-1"
labelText="TextArea label"
onClick={onClick}
onChange={onChange}
readOnly
value="test"
/>
);

// Click events should fire
userEvent.click(screen.getByRole('textbox'));
expect(onClick).toHaveBeenCalledTimes(1);

// Change events should *not* fire
userEvent.type(screen.getByRole('textbox'), 'x');
expect(screen.getByRole('textbox')).not.toHaveValue('x');
expect(onChange).toHaveBeenCalledTimes(0);
});
});
});
7 changes: 7 additions & 0 deletions packages/styles/scss/components/text-area/_text-area.scss
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,13 @@
color: $text-disabled;
}

//-----------------------------
// Readonly
//-----------------------------
.#{$prefix}--text-area__wrapper--readonly .#{$prefix}--text-area {
background: transparent;
}

// V11: Possibly deprecate
.#{$prefix}--text-area.#{$prefix}--text-area--light:disabled {
background-color: $field-02;
Expand Down