Skip to content

Commit

Permalink
Fix TextField clear issues
Browse files Browse the repository at this point in the history
  • Loading branch information
aelassas committed Dec 14, 2023
1 parent 95593cf commit 86ea153
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 3 deletions.
6 changes: 5 additions & 1 deletion backend/src/components/BookingFilter.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useState } from 'react'
import React, { useState, useRef } from 'react'
import {
FormControl,
TextField,
Expand Down Expand Up @@ -38,6 +38,8 @@ function BookingFilter({
const [keyword, setKeyword] = useState('')
const [minDate, setMinDate] = useState<Date>()

const inputRef = useRef<HTMLInputElement>(null)

const handleSearchChange = (e: React.ChangeEvent<HTMLInputElement>) => {
setKeyword(e.target.value)
}
Expand Down Expand Up @@ -115,6 +117,7 @@ function BookingFilter({
</FormControl>
<FormControl fullWidth margin="dense">
<TextField
inputRef={inputRef}
variant="standard"
value={keyword}
onKeyDown={handleSearchKeyDown}
Expand All @@ -126,6 +129,7 @@ function BookingFilter({
size="small"
onClick={() => {
setKeyword('')
inputRef.current?.focus()
}}
>
<ClearIcon className="d-adornment-icon" />
Expand Down
5 changes: 4 additions & 1 deletion backend/src/components/Search.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useState } from 'react'
import React, { useState, useRef } from 'react'
import { IconButton, TextField } from '@mui/material'
import { Search as SearchIcon, Clear as ClearIcon } from '@mui/icons-material'
import { strings as commonStrings } from '../lang/common'
Expand All @@ -15,6 +15,7 @@ function Search({
onSubmit
}: SearchProps) {
const [keyword, setKeyword] = useState('')
const inputRef = useRef<HTMLInputElement>(null)

const handleSearchChange = (e: React.ChangeEvent<HTMLInputElement>) => {
setKeyword(e.target.value)
Expand All @@ -39,6 +40,7 @@ function Search({
<form autoComplete="off" onSubmit={handleSubmit}>
<input autoComplete="false" name="hidden" type="text" style={{ display: 'none' }} />
<TextField
inputRef={inputRef}
variant="standard"
value={keyword}
onKeyDown={handleSearchKeyDown}
Expand All @@ -50,6 +52,7 @@ function Search({
size="small"
onClick={() => {
setKeyword('')
inputRef.current?.focus()
}}
>
<ClearIcon style={{ width: 20, height: 20 }} />
Expand Down
6 changes: 5 additions & 1 deletion frontend/src/components/BookingFilter.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useState } from 'react'
import React, { useState, useRef } from 'react'
import {
FormControl,
TextField,
Expand Down Expand Up @@ -38,6 +38,8 @@ function BookingFilter({
const [keyword, setKeyword] = useState('')
const [minDate, setMinDate] = useState<Date>()

const inputRef = useRef<HTMLInputElement>(null)

const handleSearchChange = (e: React.ChangeEvent<HTMLInputElement>) => {
setKeyword(e.target.value)
}
Expand Down Expand Up @@ -115,6 +117,7 @@ function BookingFilter({
</FormControl>
<FormControl fullWidth margin="dense">
<TextField
inputRef={inputRef}
variant="standard"
value={keyword}
onKeyDown={handleSearchKeyDown}
Expand All @@ -126,6 +129,7 @@ function BookingFilter({
size="small"
onClick={() => {
setKeyword('')
inputRef.current?.focus()
}}
>
<ClearIcon className="d-adornment-icon" />
Expand Down

0 comments on commit 86ea153

Please sign in to comment.