Skip to content

Commit

Permalink
chore(search): apply latest changes from #1043
Browse files Browse the repository at this point in the history
  • Loading branch information
marcelovicentegc committed Nov 30, 2023
1 parent 5e903ce commit 9b3b600
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
7 changes: 5 additions & 2 deletions packages/components/src/search/search.css
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,18 @@

&:focus-within {
box-shadow: var(--sl-focus-ring);
border: var(--sl-border-strong-hover);
}

&[data-disabled='true'] {
border: var(--sl-border-disabled);
background-color: var(--sl-bg-disabled);
color: var(--sl-fg-disabled);

& > [data-sl-pre-icon] {
color: inherit;
&:not([data-loading='true']) {
& > [data-sl-pre-icon] {
color: inherit;
}
}

& > [data-sl-search-input] {
Expand Down
19 changes: 16 additions & 3 deletions packages/components/src/search/search.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { ComponentPropsWithoutRef } from 'react'
import React, { forwardRef } from 'react'
import React, { forwardRef, useRef } from 'react'
import { IconButton } from '../icon-button'
import { IconMagnifyingGlassSmall, IconXCircle } from '@vtex/shoreline-icons'
import { useId } from '@vtex/shoreline-utils'
Expand All @@ -25,12 +25,25 @@ export const Search = forwardRef<HTMLInputElement, SearchProps>(function Search(

const id = useId(defaultId)

let defaultRef = useRef<HTMLInputElement | null>(null)

if (ref) {
defaultRef = ref as React.MutableRefObject<HTMLInputElement | null>
}

const handleFocusFromParent = () => {
if (defaultRef.current) {
defaultRef.current.focus()
}
}

return (
<div
className={className}
data-disabled={disabled}
data-loading={loading}
data-sl-search
onClick={handleFocusFromParent}
>
{loading ? (
<Spinner data-sl-pre-icon />
Expand All @@ -40,7 +53,7 @@ export const Search = forwardRef<HTMLInputElement, SearchProps>(function Search(
<input
id={id}
data-sl-search-input
ref={ref}
ref={defaultRef}
disabled={disabled}
value={value}
placeholder={placeholder}
Expand All @@ -50,7 +63,7 @@ export const Search = forwardRef<HTMLInputElement, SearchProps>(function Search(
<VisuallyHidden>
<label htmlFor={id}>{placeholder}</label>
</VisuallyHidden>
{(value || defaultValue) && typeof onClear !== undefined ? (
{!disabled && value && typeof onClear !== undefined ? (
<IconButton
label="Clear"
onClick={onClear}
Expand Down

0 comments on commit 9b3b600

Please sign in to comment.