Skip to content

Commit

Permalink
call onSubmit when the Enter key is pressed in formInput
Browse files Browse the repository at this point in the history
  • Loading branch information
ducaale committed Jan 1, 2024
1 parent 0c6b338 commit fa3b79c
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/components/core/FormInput.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useState, CSSProperties } from 'react'
import { useState, CSSProperties, SyntheticEvent } from 'react'
import styled from 'styled-components'

import { StyledInput } from './Input'
Expand All @@ -17,16 +17,23 @@ interface FormInputProps {
export function FormInput({ onSubmit, style }: FormInputProps) {
const [value, setValue] = useState('')

const handleSubmit = (event: SyntheticEvent) => {
event.preventDefault()
onSubmit(value)
setValue('')
}

return (
<Container style={style}>
<StyledInput
value={value}
onChange={(e) => setValue(e.target.value)}
onKeyDown={(e) => e.key === 'Enter' && handleSubmit(e)}
style={{ gridColumn: '1/4', gridRow: '1/1', paddingRight: 96 }}
/>
<MiniButton
type="button"
onClick={() => onSubmit(value)}
onClick={handleSubmit}
style={{ gridArea: '1/2' }}
>
Add
Expand Down

0 comments on commit fa3b79c

Please sign in to comment.