Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

useCombinedRefs #35

Merged
merged 2 commits into from
Jul 23, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
15 changes: 2 additions & 13 deletions src/components/form/field_password/field_password.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import React, {
FunctionComponent,
useState,
Ref,
useCallback,
} from 'react';
import { CommonProps } from '../../common';
import classNames from 'classnames';
Expand All @@ -35,6 +34,7 @@ import {
import { EuiValidatableControl } from '../validatable_control';
import { EuiButtonIcon, EuiButtonIconProps } from '../../button';
import { useEuiI18n } from '../../i18n';
import { useCombinedRefs } from '../../../services';

export type EuiFieldPasswordProps = InputHTMLAttributes<HTMLInputElement> &
CommonProps & {
Expand Down Expand Up @@ -102,18 +102,7 @@ export const EuiFieldPassword: FunctionComponent<EuiFieldPasswordProps> = ({

// Setup the inputRef to auto-focus when toggling visibility
const [inputRef, _setInputRef] = useState<HTMLInputElement | null>(null);
const setInputRef = useCallback(
(ref: HTMLInputElement | null) => {
_setInputRef(ref);
if (typeof _inputRef === 'function') {
_inputRef(ref);
} else if (_inputRef) {
// @ts-ignore need to mutate current
_inputRef.current = ref;
}
},
[_inputRef]
);
const setInputRef = useCombinedRefs([_setInputRef, _inputRef]);

const handleToggle = (isVisible: boolean) => {
setInputType(isVisible ? 'password' : 'text');
Expand Down
1 change: 1 addition & 0 deletions src/services/hooks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@
* under the License.
*/

export * from './useCombinedRefs';
export * from './useDependentState';
38 changes: 38 additions & 0 deletions src/services/hooks/useCombinedRefs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import { MutableRefObject, Ref, useCallback } from 'react';

export const useCombinedRefs = <T>(
thompsongl marked this conversation as resolved.
Show resolved Hide resolved
refs: Array<Ref<T> | MutableRefObject<T | undefined> | undefined>
) => {
return useCallback(
(node: T) =>
refs.forEach(ref => {
if (!ref) return;

if (typeof ref === 'function') {
ref(node);
} else {
(ref as MutableRefObject<T>).current = node;
}
}),
[refs]
);
};
2 changes: 1 addition & 1 deletion src/services/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,4 +108,4 @@ export {

export { EuiWindowEvent } from './window_event';

export { useDependentState } from './hooks';
export { useCombinedRefs, useDependentState } from './hooks';