Skip to content

Commit

Permalink
refactor(TextInput): Destructure props
Browse files Browse the repository at this point in the history
  • Loading branch information
maxime-gendron committed Feb 22, 2021
1 parent 70af7d1 commit a95ee9a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 20 deletions.
6 changes: 6 additions & 0 deletions packages/react/src/components/text-input/text-input.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ describe('TextInput', () => {
);
}

test('input has name property when name prop is set on TextInput', () => {
const wrapper = mountWithTheme(<TextInput label="test" name="test" />);

expect(wrapper.find('input').prop('name')).toBe('test');
});

test('onChange callback is called when content is changed', () => {
const callback = jest.fn();
const wrapper = setup(callback);
Expand Down
35 changes: 15 additions & 20 deletions packages/react/src/components/text-input/text-input.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ import React, {
ChangeEvent,
DetailedHTMLProps,
FocusEvent,
forwardRef,
InputHTMLAttributes,
ReactElement,
Ref,
useCallback,
useMemo,
useState,
Expand All @@ -18,8 +20,6 @@ const Input = styled.input`
${({ theme }) => inputsStyle(theme)}
`;

type inputModeType = 'none' | 'numeric' | 'tel' | 'decimal' | 'email' | 'url' | 'search';

type PartialInputProps = Pick<DetailedHTMLProps<InputHTMLAttributes<HTMLInputElement>, HTMLInputElement>,
'inputMode' | 'name' | 'value'>;

Expand All @@ -30,7 +30,6 @@ interface TextInputProps extends PartialInputProps {
/** Disables default margin */
noMargin?: boolean;
id?: string;
inputMode?: inputModeType;
label?: string;
pattern?: string;
placeholder?: string;
Expand All @@ -46,16 +45,26 @@ interface TextInputProps extends PartialInputProps {
onFocus?(event: FocusEvent<HTMLInputElement>): void;
}

export const TextInput = React.forwardRef(({
export const TextInput = forwardRef(({
className,
defaultValue,
disabled,
hint,
id: providedId,
inputMode,
label,
name,
noMargin,
pattern,
placeholder,
required,
type,
validationErrorMessage,
value,
onBlur,
onChange,
onFocus,
...props
}: TextInputProps, ref: React.Ref<HTMLInputElement>): ReactElement => {
}: TextInputProps, ref: Ref<HTMLInputElement>): ReactElement => {
const { t } = useTranslation('text-input');
const [{ validity }, setValidity] = useState({ validity: true });
const id = useMemo(() => providedId || uuid(), [providedId]);
Expand All @@ -80,20 +89,6 @@ export const TextInput = React.forwardRef(({
}
}, [onFocus]);

const {
defaultValue,
disabled,
inputMode,
label,
pattern,
placeholder,
required,
type,
validationErrorMessage,
value,
hint,
} = props;

return (
<FieldContainer
className={className}
Expand Down

0 comments on commit a95ee9a

Please sign in to comment.