Skip to content

Commit 38fe183

Browse files
authored
Add submit_on_enter prop to Textarea (#1036)
* Add submit_on_enter prop to allow user to control submit behaviour * Add Textarea test
1 parent deaff56 commit 38fe183

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

src/components/input/Textarea.js

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ const Textarea = props => {
3939
spellcheck,
4040
tabIndex,
4141
tabindex,
42+
submit_on_enter,
4243
...otherProps
4344
} = props;
4445
const [valueState, setValueState] = useState(value || '');
@@ -71,7 +72,7 @@ const Textarea = props => {
7172
};
7273

7374
const onKeyPress = e => {
74-
if (setProps && e.key === 'Enter' && !e.shiftKey) {
75+
if (submit_on_enter && setProps && e.key === 'Enter' && !e.shiftKey) {
7576
e.preventDefault(); // don't create newline if submitting
7677
const payload = {
7778
n_submit: n_submit + 1,
@@ -404,15 +405,22 @@ Textarea.propTypes = {
404405
n_blur_timestamp: PropTypes.number,
405406

406407
/**
407-
* Number of times the `Enter` key was pressed while the textarea had focus.
408+
* Number of times the `Enter` key was pressed while the textarea had focus. Only
409+
* updates if submit_on_enter is True.
408410
*/
409411
n_submit: PropTypes.number,
410412

411413
/**
412-
* Last time that `Enter` was pressed.
414+
* Last time that `Enter` was pressed. Only updates if submit_on_enter is True.
413415
*/
414416
n_submit_timestamp: PropTypes.number,
415417

418+
/**
419+
* Whether or not the form should increment the n_submit and n_submit_timestamp props
420+
* when enter key is pressed. If True, use shift + enter to create a newline. Default: True
421+
*/
422+
submit_on_enter: PropTypes.bool,
423+
416424
/**
417425
* An integer that represents the number of times
418426
* that this element has been clicked on.
@@ -490,7 +498,8 @@ Textarea.defaultProps = {
490498
debounce: false,
491499
persisted_props: ['value'],
492500
persistence_type: 'local',
493-
value: ''
501+
value: '',
502+
submit_on_enter: true
494503
};
495504

496505
export default Textarea;

src/components/input/__tests__/Textarea.test.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,15 @@ describe('Textarea', () => {
152152
expect(mockSetProps.mock.calls).toHaveLength(0);
153153
});
154154

155+
test("don't increment n_submit if submit_on_enter is false", () => {
156+
mockSetProps = jest.fn();
157+
const {
158+
container: {firstChild: ta}
159+
} = render(<Textarea submit_on_enter={false} setProps={mockSetProps} />);
160+
fireEvent.keyPress(ta, {key: 'Enter', code: 13, charCode: 13});
161+
expect(mockSetProps.mock.calls).toHaveLength(0);
162+
});
163+
155164
describe('debounce', () => {
156165
let textarea, mockSetProps;
157166
beforeEach(() => {

0 commit comments

Comments
 (0)