|
|
import React, { useCallback, useRef, forwardRef, useImperativeHandle, useState, useEffect } from 'react'; |
|
|
import { IonSearchbar } from '@ionic/react'; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export interface SearchBoxProps { |
|
|
|
|
|
defaultValue: string; |
|
|
|
|
|
onValueChange: (value: string) => void; |
|
|
|
|
|
onSearch?: () => void; |
|
|
|
|
|
onClear?: () => void; |
|
|
|
|
|
placeholder?: string; |
|
|
|
|
|
sx?: any; |
|
|
|
|
|
size?: 'smallest' | 'small' | 'medium'; |
|
|
|
|
|
variant?: 'outlined' | 'filled' | 'standard'; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export interface SearchBoxRef { |
|
|
focus: () => void; |
|
|
blur: () => void; |
|
|
selectAll: () => void; |
|
|
setValue: (value: string) => void; |
|
|
getValue: () => string; |
|
|
isFocused: () => boolean; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export const SearchBox = forwardRef<SearchBoxRef, SearchBoxProps>(({ |
|
|
defaultValue, |
|
|
onValueChange, |
|
|
onSearch, |
|
|
onClear, |
|
|
placeholder = '搜索...', |
|
|
sx, |
|
|
size = 'small', |
|
|
// variant is kept for API compatibility but not used in Ionic implementation |
|
|
}, ref) => { |
|
|
const ionSearchbarRef = useRef<HTMLIonSearchbarElement>(null); |
|
|
const [value, setValue] = useState(defaultValue); |
|
|
const hadFocusRef = useRef(false); |
|
|
|
|
|
|
|
|
useEffect(() => { |
|
|
setValue(defaultValue); |
|
|
}, [defaultValue]); |
|
|
|
|
|
const getValue = useCallback(() => { |
|
|
return value; |
|
|
}, [value]); |
|
|
|
|
|
|
|
|
useImperativeHandle(ref, () => ({ |
|
|
focus: async () => { |
|
|
await ionSearchbarRef.current?.setFocus(); |
|
|
}, |
|
|
blur: () => { |
|
|
ionSearchbarRef.current?.getInputElement().then(input => input?.blur()); |
|
|
}, |
|
|
selectAll: () => { |
|
|
ionSearchbarRef.current?.getInputElement().then(input => input?.select()); |
|
|
}, |
|
|
setValue: (newValue: string) => { |
|
|
setValue(newValue); |
|
|
}, |
|
|
getValue: getValue, |
|
|
isFocused: () => { |
|
|
|
|
|
const activeElement = document.activeElement; |
|
|
if (!activeElement) return false; |
|
|
|
|
|
|
|
|
return ionSearchbarRef.current?.contains(activeElement) ?? false; |
|
|
} |
|
|
}), [getValue]); |
|
|
|
|
|
|
|
|
const handleFocus = useCallback(() => { |
|
|
if (hadFocusRef.current && getValue()) { |
|
|
|
|
|
setTimeout(() => { |
|
|
ionSearchbarRef.current?.getInputElement().then(input => input?.select()); |
|
|
}, 0); |
|
|
} |
|
|
hadFocusRef.current = true; |
|
|
}, [getValue]); |
|
|
|
|
|
|
|
|
const handleChange = useCallback((event: CustomEvent) => { |
|
|
const newValue = event.detail.value || ''; |
|
|
console.log('Search value changed:', newValue); |
|
|
setValue(newValue); |
|
|
onValueChange(newValue); |
|
|
}, [onValueChange]); |
|
|
|
|
|
|
|
|
const handleKeyDown = useCallback((event: React.KeyboardEvent) => { |
|
|
switch (event.key) { |
|
|
case 'Enter': |
|
|
event.preventDefault(); |
|
|
onSearch?.(); |
|
|
break; |
|
|
case 'Escape': |
|
|
event.preventDefault(); |
|
|
if (getValue() && onClear) { |
|
|
setValue(''); |
|
|
onClear(); |
|
|
} |
|
|
break; |
|
|
} |
|
|
}, [getValue, onSearch, onClear]); |
|
|
|
|
|
|
|
|
const handleClear = useCallback(() => { |
|
|
setValue(''); |
|
|
onValueChange(''); |
|
|
onClear?.(); |
|
|
}, [onValueChange, onClear]); |
|
|
|
|
|
|
|
|
const sizeStyles: React.CSSProperties = (() => { |
|
|
switch (size) { |
|
|
case 'smallest': |
|
|
return { |
|
|
'--height': '28px', |
|
|
'--padding-top': '4px', |
|
|
'--padding-bottom': '4px', |
|
|
'--font-size': '13px', |
|
|
} as React.CSSProperties; |
|
|
case 'small': |
|
|
return { |
|
|
'--height': '36px', |
|
|
'--padding-top': '6px', |
|
|
'--padding-bottom': '6px', |
|
|
'--font-size': '14px', |
|
|
} as React.CSSProperties; |
|
|
case 'medium': |
|
|
default: |
|
|
return { |
|
|
'--height': '44px', |
|
|
'--padding-top': '8px', |
|
|
'--padding-bottom': '8px', |
|
|
'--font-size': '16px', |
|
|
} as React.CSSProperties; |
|
|
} |
|
|
})(); |
|
|
|
|
|
return ( |
|
|
<div style={{ width: '100%', ...sx }}> |
|
|
<IonSearchbar |
|
|
ref={ionSearchbarRef} |
|
|
value={value} |
|
|
onIonInput={handleChange} |
|
|
onIonClear={handleClear} |
|
|
onIonFocus={handleFocus} |
|
|
onKeyDown={handleKeyDown} |
|
|
placeholder={placeholder} |
|
|
showClearButton="focus" |
|
|
style={sizeStyles} |
|
|
className={`searchbox-${size}`} |
|
|
/> |
|
|
</div> |
|
|
); |
|
|
}); |
|
|
|
|
|
|
|
|
SearchBox.displayName = 'SearchBox'; |
|
|
|
|
|
export default SearchBox; |
|
|
|