|
| 1 | +import React, { ChangeEvent, FormEvent, RefObject } from 'react'; |
| 2 | + |
| 3 | +export type ControlledSearchBoxProps = React.ComponentProps<'div'> & { |
| 4 | + inputRef: RefObject<HTMLInputElement>; |
| 5 | + isSearchStalled: boolean; |
| 6 | + onChange(event: ChangeEvent): void; |
| 7 | + onReset(event: FormEvent): void; |
| 8 | + onSubmit?(event: FormEvent): void; |
| 9 | + placeholder?: string; |
| 10 | + value: string; |
| 11 | +}; |
| 12 | + |
| 13 | +export function ControlledSearchBox({ |
| 14 | + inputRef, |
| 15 | + isSearchStalled, |
| 16 | + onChange, |
| 17 | + onReset, |
| 18 | + onSubmit, |
| 19 | + placeholder, |
| 20 | + value, |
| 21 | + ...props |
| 22 | +}: ControlledSearchBoxProps) { |
| 23 | + function handleSubmit(event: FormEvent) { |
| 24 | + event.preventDefault(); |
| 25 | + event.stopPropagation(); |
| 26 | + |
| 27 | + if (onSubmit) { |
| 28 | + onSubmit(event); |
| 29 | + } |
| 30 | + |
| 31 | + if (inputRef.current) { |
| 32 | + inputRef.current.blur(); |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + function handleReset(event: FormEvent) { |
| 37 | + event.preventDefault(); |
| 38 | + event.stopPropagation(); |
| 39 | + |
| 40 | + onReset(event); |
| 41 | + |
| 42 | + if (inputRef.current) { |
| 43 | + inputRef.current.focus(); |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + return ( |
| 48 | + <div className="ais-SearchBox" {...props}> |
| 49 | + <form |
| 50 | + action="" |
| 51 | + className="ais-SearchBox-form" |
| 52 | + noValidate |
| 53 | + onSubmit={handleSubmit} |
| 54 | + onReset={handleReset} |
| 55 | + > |
| 56 | + <input |
| 57 | + ref={inputRef} |
| 58 | + className="ais-SearchBox-input" |
| 59 | + autoComplete="off" |
| 60 | + autoCorrect="off" |
| 61 | + autoCapitalize="off" |
| 62 | + placeholder={placeholder} |
| 63 | + spellCheck={false} |
| 64 | + maxLength={512} |
| 65 | + type="search" |
| 66 | + value={value} |
| 67 | + onChange={onChange} |
| 68 | + /> |
| 69 | + <button |
| 70 | + className="ais-SearchBox-submit" |
| 71 | + type="submit" |
| 72 | + title="Submit the search query." |
| 73 | + > |
| 74 | + <svg |
| 75 | + className="ais-SearchBox-submitIcon" |
| 76 | + width="10" |
| 77 | + height="10" |
| 78 | + viewBox="0 0 40 40" |
| 79 | + > |
| 80 | + <path d="M26.804 29.01c-2.832 2.34-6.465 3.746-10.426 3.746C7.333 32.756 0 25.424 0 16.378 0 7.333 7.333 0 16.378 0c9.046 0 16.378 7.333 16.378 16.378 0 3.96-1.406 7.594-3.746 10.426l10.534 10.534c.607.607.61 1.59-.004 2.202-.61.61-1.597.61-2.202.004L26.804 29.01zm-10.426.627c7.323 0 13.26-5.936 13.26-13.26 0-7.32-5.937-13.257-13.26-13.257C9.056 3.12 3.12 9.056 3.12 16.378c0 7.323 5.936 13.26 13.258 13.26z"></path> |
| 81 | + </svg> |
| 82 | + </button> |
| 83 | + <button |
| 84 | + className="ais-SearchBox-reset" |
| 85 | + type="reset" |
| 86 | + title="Clear the search query." |
| 87 | + hidden={value.length === 0 && !isSearchStalled} |
| 88 | + > |
| 89 | + <svg |
| 90 | + className="ais-SearchBox-resetIcon" |
| 91 | + viewBox="0 0 20 20" |
| 92 | + width="10" |
| 93 | + height="10" |
| 94 | + > |
| 95 | + <path d="M8.114 10L.944 2.83 0 1.885 1.886 0l.943.943L10 8.113l7.17-7.17.944-.943L20 1.886l-.943.943-7.17 7.17 7.17 7.17.943.944L18.114 20l-.943-.943-7.17-7.17-7.17 7.17-.944.943L0 18.114l.943-.943L8.113 10z"></path> |
| 96 | + </svg> |
| 97 | + </button> |
| 98 | + <span |
| 99 | + className="ais-SearchBox-loadingIndicator" |
| 100 | + hidden={!isSearchStalled} |
| 101 | + > |
| 102 | + <svg |
| 103 | + width="16" |
| 104 | + height="16" |
| 105 | + viewBox="0 0 38 38" |
| 106 | + stroke="#444" |
| 107 | + className="ais-SearchBox-loadingIcon" |
| 108 | + > |
| 109 | + <g fill="none" fillRule="evenodd"> |
| 110 | + <g transform="translate(1 1)" strokeWidth="2"> |
| 111 | + <circle strokeOpacity=".5" cx="18" cy="18" r="18" /> |
| 112 | + <path d="M36 18c0-9.94-8.06-18-18-18"> |
| 113 | + <animateTransform |
| 114 | + attributeName="transform" |
| 115 | + type="rotate" |
| 116 | + from="0 18 18" |
| 117 | + to="360 18 18" |
| 118 | + dur="1s" |
| 119 | + repeatCount="indefinite" |
| 120 | + /> |
| 121 | + </path> |
| 122 | + </g> |
| 123 | + </g> |
| 124 | + </svg> |
| 125 | + </span> |
| 126 | + </form> |
| 127 | + </div> |
| 128 | + ); |
| 129 | +} |
0 commit comments