Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Search/SearchAssistance.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const SearchAssistance = ({ children, className, relativeTo, ...rest }) => {

SearchAssistance.propTypes = {
children: PropTypes.node.isRequired,
className: PropTypes.string,
className: PropTypes.string,
relativeTo: PropTypes.string.isRequired,
position: PropTypes.oneOf(POSITIONS),
};
Expand Down
29 changes: 27 additions & 2 deletions src/Search/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import classnames from 'classnames';
import PropTypes from 'prop-types';
import React from 'react';
import React, { useRef } from 'react';
import Icon from '../Icon';
import { wcBool } from '../utils';
import { useEffectExceptOnMount } from '../hooks/useEffectExceptOnMounted';

/**
* @see https://helixdesignsystem.github.io/helix-ui/components/search/
Expand All @@ -18,11 +19,35 @@ const Search = ({
optional,
required,
wrapperId,
value,
...rest
}) => {
/**
* Show clear icon when value changes programmatically by triggering 'input' event manually.
* @see https://stackoverflow.com/questions/23892547/what-is-the-best-way-to-trigger-onchange-event-in-react-js
*/
const inputRef = useRef();
useEffectExceptOnMount(() => {
const input = inputRef.current;
const nativeInputValueSetter = Object.getOwnPropertyDescriptor(
window.HTMLInputElement.prototype,
'value'
).set;
nativeInputValueSetter.call(input, value);
const ev = new Event('input', { bubbles: true });
input.dispatchEvent(ev);
}, [value]);

return (
<hx-search-control class={className} id={wrapperId}>
<input id={id} {...rest} disabled={wcBool(disabled)} type="search" />
<input
id={id}
value={value}
ref={inputRef}
{...rest}
disabled={wcBool(disabled)}
type="search"
/>
<button type="button" className="hxClear" aria-label={clearLabel} hidden onClick={onClear}>
<Icon type="times" />
</button>
Expand Down
23 changes: 23 additions & 0 deletions src/hooks/useEffectExceptOnMounted.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import React from 'react';
/**
* Identical to React.useEffect, except that it never runs on mount. This is the equivalent of
* the componentDidUpdate lifecycle function.
* @see https://stackoverflow.com/questions/53253940/make-react-useeffect-hook-not-run-on-initial-render
* @param {function:function} effect - A useEffect effect.
* @param {array} dependencies - useEffect dependency list.
*/
export const useEffectExceptOnMount = (effect, dependencies) => {
const mounted = React.useRef(false);
React.useEffect(() => {
if (mounted.current) {
const unmount = effect();
return () => unmount && unmount();
} else {
mounted.current = true;
}
}, dependencies);

React.useEffect(() => {
return () => (mounted.current = false);
}, []);
};