Skip to content

Commit

Permalink
Add submit_on_enter prop to Textarea (#1036)
Browse files Browse the repository at this point in the history
* Add submit_on_enter prop to allow user to control submit behaviour

* Add Textarea test
  • Loading branch information
tcbegley committed May 14, 2024
1 parent deaff56 commit 38fe183
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
17 changes: 13 additions & 4 deletions src/components/input/Textarea.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ const Textarea = props => {
spellcheck,
tabIndex,
tabindex,
submit_on_enter,
...otherProps
} = props;
const [valueState, setValueState] = useState(value || '');
Expand Down Expand Up @@ -71,7 +72,7 @@ const Textarea = props => {
};

const onKeyPress = e => {
if (setProps && e.key === 'Enter' && !e.shiftKey) {
if (submit_on_enter && setProps && e.key === 'Enter' && !e.shiftKey) {
e.preventDefault(); // don't create newline if submitting
const payload = {
n_submit: n_submit + 1,
Expand Down Expand Up @@ -404,15 +405,22 @@ Textarea.propTypes = {
n_blur_timestamp: PropTypes.number,

/**
* Number of times the `Enter` key was pressed while the textarea had focus.
* Number of times the `Enter` key was pressed while the textarea had focus. Only
* updates if submit_on_enter is True.
*/
n_submit: PropTypes.number,

/**
* Last time that `Enter` was pressed.
* Last time that `Enter` was pressed. Only updates if submit_on_enter is True.
*/
n_submit_timestamp: PropTypes.number,

/**
* Whether or not the form should increment the n_submit and n_submit_timestamp props
* when enter key is pressed. If True, use shift + enter to create a newline. Default: True
*/
submit_on_enter: PropTypes.bool,

/**
* An integer that represents the number of times
* that this element has been clicked on.
Expand Down Expand Up @@ -490,7 +498,8 @@ Textarea.defaultProps = {
debounce: false,
persisted_props: ['value'],
persistence_type: 'local',
value: ''
value: '',
submit_on_enter: true
};

export default Textarea;
9 changes: 9 additions & 0 deletions src/components/input/__tests__/Textarea.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,15 @@ describe('Textarea', () => {
expect(mockSetProps.mock.calls).toHaveLength(0);
});

test("don't increment n_submit if submit_on_enter is false", () => {
mockSetProps = jest.fn();
const {
container: {firstChild: ta}
} = render(<Textarea submit_on_enter={false} setProps={mockSetProps} />);
fireEvent.keyPress(ta, {key: 'Enter', code: 13, charCode: 13});
expect(mockSetProps.mock.calls).toHaveLength(0);
});

describe('debounce', () => {
let textarea, mockSetProps;
beforeEach(() => {
Expand Down

0 comments on commit 38fe183

Please sign in to comment.