Skip to content

Commit

Permalink
feat: add clearableInput component
Browse files Browse the repository at this point in the history
  • Loading branch information
Birkbjo committed Nov 14, 2024
1 parent c33c962 commit ba7d803
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 11 deletions.
30 changes: 19 additions & 11 deletions src/app/sidebar/sidenav/SidenavFilter.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
import i18n from '@dhis2/d2-i18n'
import { Input, InputEventPayload, InputChangeHandler } from '@dhis2/ui'
import { InputEventPayload, InputEventHandler } from '@dhis2/ui'
import React, { useState } from 'react'
import { ClearableInput } from '../../../components'
import styles from './Sidenav.module.css'

interface SidenavParentProps {
onChange?: InputChangeHandler
onChange?: InputEventHandler<React.SyntheticEvent>
}

export const SidenavFilter = ({ onChange }: SidenavParentProps) => {
const [value, setValue] = useState('')

const handleChange = (
input: InputEventPayload,
e: React.ChangeEvent<HTMLInputElement>
e: React.SyntheticEvent
) => {
setValue(input.value ?? '')
if (onChange) {
Expand All @@ -21,13 +22,20 @@ export const SidenavFilter = ({ onChange }: SidenavParentProps) => {
}

return (
<Input
className={styles['sidenav-filter']}
dense
type="text"
value={value}
placeholder={i18n.t('Search for menu items')}
onChange={handleChange}
/>
<div>
<ClearableInput
className={styles['sidenav-filter']}
dense
name="sidenav-filter"
type="text"
value={value}
placeholder={i18n.t('Search for menu items')}
onChange={handleChange}
showClearButton={value.length > 0}
onClear={(e) =>
handleChange({ value: '', name: 'sidenav-filter' }, e)
}
/>
</div>
)
}
1 change: 1 addition & 0 deletions src/components/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ export * from './metadataFormControls'
export * from './SearchableSingleSelect'
export * from './sectionList'
export * from './standardForm'
export * from './input'
17 changes: 17 additions & 0 deletions src/components/input/ClearableInput.module.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
.clearableInputWrapper {
display: flex;
align-items: center;
position: relative;
justify-content: flex-start;
}

.clearableInputWrapper > div {
flex-grow: 1;
}

.clearableInputClearButton {
cursor: pointer;
position: absolute;
display: inline-flex;
right: var(--spacers-dp16);
}
36 changes: 36 additions & 0 deletions src/components/input/ClearableInput.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { IconCross16, Input, InputProps } from '@dhis2/ui'
import cx from 'classnames'
import React from 'react'
import css from './ClearableInput.module.css'

export type ClearableInputProps = InputProps & {
showClearButton?: boolean
className?: string
inputClassName?: string
clearButtonClassName?: string
onClear?: (e: React.MouseEvent<HTMLSpanElement>) => void
}

export const ClearableInput = ({
className,
inputClassName,
clearButtonClassName,
onClear,
showClearButton = true,
...inputProps
}: ClearableInputProps) => {
return (
<span className={cx(css.clearableInputWrapper, className)}>
<Input {...inputProps} className={inputClassName} />
<span
className={cx(
css.clearableInputClearButton,
clearButtonClassName
)}
onClick={onClear}
>
{showClearButton && <IconCross16 />}
</span>
</span>
)
}
1 change: 1 addition & 0 deletions src/components/input/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './ClearableInput'

0 comments on commit ba7d803

Please sign in to comment.