import _ from "lodash"; import cn from "classnames"; import React, { useState, useMemo, useCallback } from "react"; import searchIcon from "!!svg-sprite-loader!@cloudbees/honeyui-icons/solid/search.svg"; import { Icon } from "../"; interface ISearchProps { ["data-testid"]?: string; ariaLabel?: string; className?: string; debounce?: number; onChange: (value: string) => void; placeholder?: string; size?: "sm" | "md" | "lg"; value?: string; disabled?: boolean; } const Search: React.FC = props => { const dataTestId = props["data-testid"] || "honey-ui-search"; const [value, setValue] = useState(props.value); const handleChange = useMemo( () => (props.debounce ? _.debounce(props.onChange, props.debounce) : props.onChange), [props.debounce, props.onChange] ); const onChange = useCallback( (e: React.FormEvent) => { setValue(e.currentTarget.value); handleChange(e.currentTarget.value); }, [handleChange] ); if (!props.ariaLabel && !props.placeholder) { console.warn("ariaLabel or placeholder must be provided with search"); } return (
{props.children ? (
{props.children}
) : null}
); }; Search.displayName = "Search"; Search.defaultProps = { size: "md", value: "" }; export default Search;