Skip to content

Commit

Permalink
Merge pull request #227 from platformatic/feature/update-searchbar-v2
Browse files Browse the repository at this point in the history
fix: updating search bar and adding story
  • Loading branch information
tonysnowboardunderthebridge authored Dec 14, 2023
2 parents 29eeea7 + 6a5a4e2 commit 8829e23
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 16 deletions.
37 changes: 22 additions & 15 deletions src/components/SearchBarV2.jsx
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
'use strict'
import React, { useRef, useState } from 'react'
import PropTypes from 'prop-types'
import commonStyles from './Common.module.css'
import styles from './SearchBarV2.module.css'
import PlatformaticIcon from './PlatformaticIcon'
import { MEDIUM, WHITE } from './constants'
import { MEDIUM, RICH_BLACK, TRANSPARENT, WHITE } from './constants'
function SearchBarV2 ({
onSubmit,
onChange,
onClear,
color,
onFocusColor,
backgroundColor,
placeholder,
dataAttrName,
dataAttrValue
}) {
const inputRef = useRef()
const baseClassName = `${styles.container} ${styles.wrapperPadding} ` + commonStyles[`background-color-${backgroundColor}`]
const [wrapperClassName, setWrapperClassName] = useState(normalClassName())
const [isOnFocus, setIsOnFocus] = useState(false)
const [showClear, setShowClear] = useState(false)
const dataProps = {}
if (dataAttrName && dataAttrValue) {
dataProps[`data-${dataAttrName}`] = dataAttrValue
Expand All @@ -31,12 +34,14 @@ function SearchBarV2 ({
function handleChange () {
if (onChange) {
const value = inputRef.current.value
setShowClear(!!value)
return onChange(value)
}
}

function handleClear () {
inputRef.current.value = ''
setShowClear(false)
return onClear()
}

Expand All @@ -53,20 +58,22 @@ function SearchBarV2 ({
}

function onFocusClassName () {
return `${styles.container} ${styles.wrapperPadding} ${styles.onFocus}`
return `${baseClassName} ${styles.onFocus}`
}

function normalClassName () {
return `${styles.container} ${styles.wrapperPadding} ${styles.onBlur}`
return `${baseClassName} ${styles.onBlur}`
}

return (
<div className={wrapperClassName} {...dataProps}>
<PlatformaticIcon iconName='LensIcon' color={isOnFocus ? onFocusColor : color} size={MEDIUM} onClick={handleSearch} />
<PlatformaticIcon iconName='LensIcon' color={color} disabled={!isOnFocus} size={MEDIUM} onClick={handleSearch} />
<input type='text' placeholder={placeholder} className={styles.input} ref={inputRef} onChange={handleChange} onFocus={onFocus} onBlur={onBlur} />
<div className={styles.clearContainer}>
<PlatformaticIcon iconName='CircleCloseIcon' color={isOnFocus ? onFocusColor : color} size={MEDIUM} onClick={handleClear} />
</div>
{showClear && (
<div className={styles.clearContainer}>
<PlatformaticIcon iconName='CircleCloseIcon' color={color} size={MEDIUM} onClick={handleClear} />
</div>
)}
</div>
)
}
Expand All @@ -87,11 +94,11 @@ SearchBarV2.propTypes = {
/**
* color
*/
color: PropTypes.string,
color: PropTypes.oneOf([WHITE, RICH_BLACK]),
/**
* onFocusColor
* backgroundColor
*/
onFocusColor: PropTypes.string,
backgroundColor: PropTypes.oneOf([WHITE, RICH_BLACK, TRANSPARENT]),
/**
* placeholder
*/
Expand All @@ -108,10 +115,10 @@ SearchBarV2.propTypes = {

SearchBarV2.defaultProps = {
color: WHITE,
onFocusColor: WHITE,
onSubmit: () => {},
onChange: () => {},
onClear: () => {},
backgroundColor: RICH_BLACK,
onSubmit: () => { },
onChange: () => { },
onClear: () => { },
placeholder: 'Search',
dataAttrName: '',
dataAttrValue: ''
Expand Down
2 changes: 1 addition & 1 deletion src/components/SearchBarV2.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
@apply font-semibold text-white text-3xl pb-4;
}
.clearContainer {
@apply absolute right-[1rem] translate-y-[-50%];
@apply absolute right-[1rem];
}
.wrapperPadding {
@apply px-2 py-1.5;
Expand Down
24 changes: 24 additions & 0 deletions src/stories/SearchBarV2.stories.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
'use strict'
import SearchBarV2 from '../components/SearchBarV2'
export default {
title: 'Platformatic/SearchBarV2',
component: SearchBarV2
}
const Template = (args) => {
return (
<>
<SearchBarV2 {...args} />
</>

)
}
export const Standard = Template.bind({})

Standard.args = {
onChange: (value) => {
console.log('Current search: ' + value)
},
onSubmit: (value) => {
alert('Query value: ' + value)
}
}

0 comments on commit 8829e23

Please sign in to comment.