Skip to content

Commit

Permalink
chore(search-bar): cleaned up props (#4923)
Browse files Browse the repository at this point in the history
* chore(search-bar): cleaned up props

* fix(lint): added missing prop

* fix(SearchBar): fix tests
  • Loading branch information
IgnacioBecerra authored May 13, 2024
1 parent 5d31c3c commit a7c9c01
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ const scopes = [

const defaultProps = {
clearButtonLabelText: 'Clear',
placeHolderText: 'Search...',
placeholderText: 'Search...',
submitLabel: 'Search',
onChange: (newVal) => action('onChange')(newVal),
onSubmit: (newVal) => action('onSubmit')(newVal),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const value = 'carbon';

const defaultProps = {
clearButtonLabelText: 'Clear',
placeHolderText: 'Search...',
placeholderText: 'Search...',
submitLabel: 'Search',
onChange: mockOnChange,
onSubmit: mockOnSubmit,
Expand Down Expand Up @@ -64,7 +64,7 @@ describe(componentName, () => {

expect(searchBox).toBeInTheDocument();
expect(searchBox.value).toBe('');
expect(searchBox.placeholder).toBe(defaultProps.placeHolderText);
expect(searchBox.placeholder).toBe(defaultProps.placeholderText);
expect(submitButton).toHaveTextContent(defaultProps.submitLabel);
expect(submitButton).toBeInTheDocument();
expect(submitButton).toBeDisabled();
Expand Down
40 changes: 25 additions & 15 deletions packages/ibm-products/src/components/SearchBar/SearchBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ interface SearchBarProps extends PropsWithChildren {
onSubmit?: (event: any) => void;

/** @type {string} Placeholder text to be displayed in the search input. */
placeHolderText: string;
placeholderText: string;

/** @type {Function} Function to get the text for each scope to display in dropdown. */
scopeToString?: () => void;
Expand Down Expand Up @@ -107,7 +107,7 @@ export let SearchBar = React.forwardRef<HTMLFormElement, SearchBarProps>(
labelText,
onChange = defaults.onChange,
onSubmit = defaults.onSubmit,
placeHolderText,
placeholderText,
scopes = [],
scopesTypeLabel,
scopeToString,
Expand Down Expand Up @@ -180,6 +180,15 @@ export let SearchBar = React.forwardRef<HTMLFormElement, SearchBarProps>(
onChange(eventObject);
};

const multiSelectProps = {
initialSelectedItems: selectedScopes,
items: scopes,
itemToString: scopeToString,
label: scopesTypeLabel,
sortItems,
translateWithId,
}

return (
<form
{...rest}
Expand All @@ -192,16 +201,11 @@ export let SearchBar = React.forwardRef<HTMLFormElement, SearchBarProps>(
>
{scopes?.length ? (
<MultiSelect
{...multiSelectProps}
id={`${blockClass}__multi-select`}
name="search-scopes"
className={`${blockClass}__scopes`}
label={scopesTypeLabel}
onChange={handleSearchScopeChange}
initialSelectedItems={selectedScopes}
items={scopes}
itemToString={scopeToString}
translateWithId={translateWithId}
sortItems={sortItems}
size="lg"
/>
) : null}
Expand All @@ -211,7 +215,7 @@ export let SearchBar = React.forwardRef<HTMLFormElement, SearchBarProps>(
labelText={labelText || ''}
name="search-input"
onChange={handleInputChange}
placeholder={placeHolderText}
placeholder={placeholderText}
value={text}
size="lg"
/>
Expand Down Expand Up @@ -251,6 +255,15 @@ const conditionalScopePropValidator = (
return PropTypes.string(props, propName, componentName, ...rest);
};

export const deprecatedProps = {
/**
* **Deprecated**
*
* Provide accessible label text for the scopes MultiSelect.
*/
titleText: PropTypes.string,
};

// The types and DocGen commentary for the component props,
// in alphabetical order (for consistency).
// See https://www.npmjs.com/package/prop-types#usage.
Expand All @@ -276,7 +289,7 @@ SearchBar.propTypes = {
onSubmit: PropTypes.func,

/** @type {string} Placeholder text to be displayed in the search input. */
placeHolderText: PropTypes.string.isRequired,
placeholderText: PropTypes.string.isRequired,

/** @type {Function} Function to get the text for each scope to display in dropdown. */
scopeToString: PropTypes.func,
Expand Down Expand Up @@ -307,14 +320,11 @@ SearchBar.propTypes = {
/** @type {string} The label text for the search submit button. */
submitLabel: PropTypes.string.isRequired,

/**
* Provide accessible label text for the scopes MultiSelect.
*/
titleText: PropTypes.string,

/** @type {func} Callback function for translating MultiSelect's child ListBoxMenuIcon SVG title. */
translateWithId: PropTypes.func, // eslint-disable-line react/require-default-props

/** @type {string} Search query value. */
value: PropTypes.string,

...deprecatedProps,
};

0 comments on commit a7c9c01

Please sign in to comment.