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

Add submit_on_enter prop to Textarea #1036

Merged
merged 2 commits into from
May 14, 2024
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
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
Loading