diff --git a/devbox/apps/Input.js b/devbox/apps/Input.js index 92ac89776..d33d5f0ed 100644 --- a/devbox/apps/Input.js +++ b/devbox/apps/Input.js @@ -1,45 +1,52 @@ -import React from 'react' +import React, { useState } from 'react' import styled from 'styled-components' -import { TextInput, IconBlank } from '@aragon/ui' +import { TextInput, IconBlank, SearchInput } from '@aragon/ui' -class App extends React.Component { - render() { - return ( -
- - - - } adornmentPosition="start" /> - } adornmentPosition="end" /> - -
- ) - } +function App() { + const [searchTerm, setSearchTerm] = useState('') + return ( +
+ + + + } adornmentPosition="start" /> + } adornmentPosition="end" /> + + { + console.log('Search term changed: ', value) + console.log('Search change event: ', ev) + setSearchTerm(value) + }} + /> +
+ ) } export default App diff --git a/src/components/Input/SearchInput.js b/src/components/Input/SearchInput.js new file mode 100644 index 000000000..7eb6dbafd --- /dev/null +++ b/src/components/Input/SearchInput.js @@ -0,0 +1,59 @@ +import React, { useRef, useCallback } from 'react' +import { ButtonIcon } from '../Button/ButtonIcon' +import TextInput from './TextInput' +import { IconSearch, IconCross } from '../../icons' +import { useTheme } from '../../theme' + +const EMPTY = '' + +const SearchInput = React.forwardRef(({ onChange, ...props }, ref) => { + const theme = useTheme() + const localRef = ref || useRef() + const handleChange = useCallback( + ev => { + const { value } = ev.currentTarget + onChange(value, { inputChangeEvent: ev }) + }, + [onChange] + ) + const clear = useCallback( + ev => { + onChange(EMPTY, { clearClickEvent: ev }) + }, + [onChange] + ) + + return ( + + ) : ( + + + + ) + } + adornmentPosition="end" + /> + ) +}) + +SearchInput.propTypes = { + ...TextInput.propTypes, +} + +export { SearchInput } diff --git a/src/components/Input/SearchInput.md b/src/components/Input/SearchInput.md new file mode 100644 index 000000000..be7737ba6 --- /dev/null +++ b/src/components/Input/SearchInput.md @@ -0,0 +1,24 @@ +# SearchInput + +A text input component with a set adornment that changes into a button when text is introduced in the input and when clicked on clears the input. + +## Usage + +```jsx +function App() { + const [value, setValue] = useState('') + return +} +``` + +## Props + +Extends `TextInput` so this component will take in the same props as `TextInput`. + +### `onChange` + +| Type | Default value | +| ---------- | ------------- | +| `Function` | None | + +The input box value and the two native `change` or `click` events. diff --git a/src/components/index.js b/src/components/index.js index aafad57dd..c2ae252e1 100644 --- a/src/components/index.js +++ b/src/components/index.js @@ -5,6 +5,7 @@ export { Info } from './Info' export { Layout, useLayout } from './Layout/Layout' export { Link, ExternalLink, SafeLink } from './Link' export { Pagination } from './Pagination/Pagination' +export { SearchInput } from './Input/SearchInput' export { Tag } from './Tag/Tag' export { ToastHub, Toast, useToast } from './ToastHub/ToastHub' export { default as Accordion } from './Accordion/Accordion'