-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathindex.tsx
121 lines (112 loc) · 2.82 KB
/
index.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/**
* External dependencies
*/
import classnames from 'classnames';
/**
* WordPress dependencies
*/
import { useInstanceId, useMergeRefs } from '@wordpress/compose';
import { __ } from '@wordpress/i18n';
import { Icon, search, closeSmall } from '@wordpress/icons';
import { forwardRef, useRef } from '@wordpress/element';
/**
* Internal dependencies
*/
import Button from '../button';
import BaseControl from '../base-control';
import type { WordPressComponentProps } from '../ui/context/wordpress-component';
import type { SearchControlProps } from './types';
import type { ForwardedRef } from 'react';
function UnforwardedSearchControl(
{
__nextHasNoMarginBottom,
className,
onChange,
onKeyDown,
value,
label,
placeholder = __( 'Search' ),
hideLabelFromVision = true,
help,
onClose,
...restProps
}: WordPressComponentProps< SearchControlProps, 'input', false >,
forwardedRef: ForwardedRef< HTMLInputElement >
) {
const searchRef = useRef< HTMLInputElement >();
const instanceId = useInstanceId( SearchControl );
const id = `components-search-control-${ instanceId }`;
const renderRightButton = () => {
if ( onClose ) {
return (
<Button
icon={ closeSmall }
label={ __( 'Close search' ) }
onClick={ onClose }
/>
);
}
if ( !! value ) {
return (
<Button
icon={ closeSmall }
label={ __( 'Reset search' ) }
onClick={ () => {
onChange( '' );
searchRef.current?.focus();
} }
/>
);
}
return <Icon icon={ search } />;
};
return (
<BaseControl
__nextHasNoMarginBottom={ __nextHasNoMarginBottom }
label={ label }
id={ id }
hideLabelFromVision={ hideLabelFromVision }
help={ help }
className={ classnames( className, 'components-search-control' ) }
>
<div className="components-search-control__input-wrapper">
<input
{ ...restProps }
ref={ useMergeRefs( [ searchRef, forwardedRef ] ) }
className="components-search-control__input"
id={ id }
type="search"
placeholder={ placeholder }
onChange={ ( event ) => onChange( event.target.value ) }
onKeyDown={ onKeyDown }
autoComplete="off"
value={ value || '' }
/>
<div className="components-search-control__icon">
{ renderRightButton() }
</div>
</div>
</BaseControl>
);
}
/**
* SearchControl components let users display a search control.
*
* ```jsx
* import { SearchControl } from '@wordpress/components';
* import { useState } from '@wordpress/element';
*
* function MySearchControl( { className, setState } ) {
* const [ searchInput, setSearchInput ] = useState( '' );
*
* return (
* <SearchControl
* value={ searchInput }
* onChange={ setSearchInput }
* />
* );
* }
* ```
*/
export const SearchControl = forwardRef( UnforwardedSearchControl );
export default SearchControl;