-
Notifications
You must be signed in to change notification settings - Fork 314
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(search-form): extract search actions component #3475
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import * as React from 'react'; | ||
import { injectIntl, IntlShape } from 'react-intl'; | ||
|
||
import ClearBadge16 from '../../icon/fill/ClearBadge16'; | ||
import Search16 from '../../icon/fill/Search16'; | ||
import makeLoadable from '../loading-indicator/makeLoadable'; | ||
|
||
import messages from './messages'; | ||
|
||
export interface SearchActionsProps { | ||
/** Whether to render an interactive search button */ | ||
hasSubmitAction: boolean; | ||
/** Intl object */ | ||
intl: IntlShape; | ||
/** Called when clear button is clicked */ | ||
onClear: (event: React.SyntheticEvent<HTMLButtonElement>) => void; | ||
} | ||
|
||
const SearchActions = ({ hasSubmitAction, intl, onClear }: SearchActionsProps) => { | ||
const { formatMessage } = intl; | ||
|
||
return ( | ||
<div className="action-buttons"> | ||
{hasSubmitAction ? ( | ||
<button | ||
type="submit" | ||
className="action-button search-button" | ||
title={formatMessage(messages.searchButtonTitle)} | ||
> | ||
<Search16 /> | ||
</button> | ||
) : ( | ||
<div className="action-button search-button"> | ||
<Search16 /> | ||
</div> | ||
)} | ||
<button | ||
className="action-button clear-button" | ||
onClick={onClear} | ||
title={formatMessage(messages.clearButtonTitle)} | ||
type="button" | ||
> | ||
<ClearBadge16 /> | ||
</button> | ||
</div> | ||
); | ||
}; | ||
|
||
export { SearchActions as SearchActionsBase }; | ||
export default makeLoadable(injectIntl(SearchActions)); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,17 @@ | ||
// @flow | ||
import * as React from 'react'; | ||
import { defineMessages, injectIntl } from 'react-intl'; | ||
import { injectIntl } from 'react-intl'; | ||
import classNames from 'classnames'; | ||
import omit from 'lodash/omit'; | ||
|
||
import ClearBadge16 from '../../icon/fill/ClearBadge16'; | ||
import Search16 from '../../icon/fill/Search16'; | ||
// $FlowFixMe | ||
import SearchActions from './SearchActions'; | ||
|
||
import makeLoadable from '../loading-indicator/makeLoadable'; | ||
// $FlowFixMe | ||
import messages from './messages'; | ||
|
||
import './SearchForm.scss'; | ||
|
||
const messages = defineMessages({ | ||
clearButtonTitle: { | ||
defaultMessage: 'Clear', | ||
description: 'Title for a clear button', | ||
id: 'boxui.searchForm.clearButtonTitle', | ||
}, | ||
searchButtonTitle: { | ||
defaultMessage: 'Search', | ||
description: 'Title for a search button', | ||
id: 'boxui.searchForm.searchButtonTitle', | ||
}, | ||
searchLabel: { | ||
defaultMessage: 'Search query', | ||
description: 'Label for a search input', | ||
id: 'boxui.searchForm.searchLabel', | ||
}, | ||
}); | ||
|
||
type Props = { | ||
/** Form submit action */ | ||
action?: string, | ||
|
@@ -41,13 +24,13 @@ type Props = { | |
method?: 'get' | 'post', | ||
/** Name of the text input */ | ||
name?: string, | ||
/** On change handler for the search input, debounced by 250ms */ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. outdated comment - the debounce was removed in box-react-ui |
||
/** On change handler for the search input */ | ||
onChange?: Function, | ||
/** On submit handler for the search input */ | ||
onSubmit?: Function, | ||
/** Extra query parameters in addition to the form data */ | ||
queryParams: { [arg: string]: string }, | ||
/** Boolean to prevent propogation of search clear action */ | ||
/** Whether to prevent propagation of search clear action */ | ||
shouldPreventClearEventPropagation?: boolean, | ||
/** If the clear button is shown when input field is not empty */ | ||
useClearButton?: boolean, | ||
|
@@ -177,35 +160,6 @@ class SearchFormBase extends React.Component<Props, State> { | |
<input key={index} name={param} type="hidden" value={queryParams[param]} /> | ||
)); | ||
|
||
const SearchActions = () => ( | ||
<div className="action-buttons"> | ||
{onSubmit ? ( | ||
<button | ||
type="submit" | ||
className="action-button search-button" | ||
title={formatMessage(messages.searchButtonTitle)} | ||
> | ||
<Search16 /> | ||
</button> | ||
) : ( | ||
<div className="action-button search-button"> | ||
<Search16 /> | ||
</div> | ||
)} | ||
|
||
<button | ||
className="action-button clear-button" | ||
onClick={this.onClearHandler} | ||
title={formatMessage(messages.clearButtonTitle)} | ||
type="button" | ||
> | ||
<ClearBadge16 /> | ||
</button> | ||
</div> | ||
); | ||
|
||
const LoadableSearchActions = makeLoadable(SearchActions); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. legacy docs state to not use HOCs in the render method: https://legacy.reactjs.org/docs/higher-order-components.html#dont-use-hocs-inside-the-render-method I don't know if this is the direct cause of the original issue but it ended up fixing it without changing the other logic |
||
|
||
// @NOTE Prevent errors from React about controlled inputs | ||
const onChangeStub = () => {}; | ||
|
||
|
@@ -229,11 +183,13 @@ class SearchFormBase extends React.Component<Props, State> { | |
type="search" | ||
{...inputProps} | ||
/> | ||
<LoadableSearchActions | ||
<SearchActions | ||
hasSubmitAction={!!onSubmit} | ||
isLoading={isLoading} | ||
loadingIndicatorProps={{ | ||
className: 'search-form-loading-indicator', | ||
}} | ||
onClear={this.onClearHandler} | ||
/> | ||
{hiddenInputs} | ||
</form> | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { defineMessages } from 'react-intl'; | ||
|
||
const messages = defineMessages({ | ||
clearButtonTitle: { | ||
defaultMessage: 'Clear', | ||
description: 'Title for a clear button', | ||
id: 'boxui.searchForm.clearButtonTitle', | ||
}, | ||
searchButtonTitle: { | ||
defaultMessage: 'Search', | ||
description: 'Title for a search button', | ||
id: 'boxui.searchForm.searchButtonTitle', | ||
}, | ||
searchLabel: { | ||
defaultMessage: 'Search query', | ||
description: 'Label for a search input', | ||
id: 'boxui.searchForm.searchLabel', | ||
}, | ||
}); | ||
|
||
export default messages; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fyi this component is just copied from
SearchForm
. leaving class names and whatnot as the same so it doesn't break existing functionality for apps