Skip to content

Commit

Permalink
feat(forminput): add FormInput component, storybook
Browse files Browse the repository at this point in the history
  • Loading branch information
vankhoawin committed Aug 9, 2020
1 parent 50bdee7 commit e9a8ea6
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 0 deletions.
41 changes: 41 additions & 0 deletions src/components/FormInput/FormInput.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// import { action } from '@storybook/addon-actions';
import { Button, FormLabel } from '@twilio-paste/core';
import { Theme } from '@twilio-paste/theme';
import { Formik } from 'formik';
import * as React from 'react';

import { FormInput } from './FormInput';

export default {
title: 'FormInput',
};

export const Basic: React.FC = () => (
<Theme.Provider theme="default">
<Formik
initialValues={{ checkbox: true }}
onSubmit={(values) => {
// eslint-disable-next-line no-alert
window.alert(JSON.stringify(values));
}}
>
{({ handleSubmit }) => (
<form onSubmit={handleSubmit}>
<FormLabel htmlFor="email_address">Email Address</FormLabel>

<FormInput
aria-describedby="email_error_help_text"
id="email_address"
name="email_address"
type="email"
placeholder="[email protected]"
/>

<Button variant="primary" type="submit">
Submit
</Button>
</form>
)}
</Formik>
</Theme.Provider>
);
38 changes: 38 additions & 0 deletions src/components/FormInput/FormInput.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import * as React from 'react';
import { FieldProps } from 'formik';
import { FormInput as $FormInput, FormInputProps as $FormInputProps } from '@twilio-paste/core';

import { FormikFieldProps } from '../../interfaces';
import { Field } from '../Field';

export type FormInputProps = FormikFieldProps & Omit<$FormInputProps, 'value'>;

export const FormInput = React.forwardRef(
(
{ name, validate, fast, onChange: $onChange, onBlur: $onBlur, ...restProps }: FormInputProps,
ref: React.Ref<typeof $FormInput>,
) => (
<Field name={name} validate={validate} fast={fast}>
{({ field: { value, onChange, onBlur } }: FieldProps) => (
<$FormInput
{...restProps}
ref={(ref as unknown) as React.RefObject<HTMLInputElement>}
name={name}
value={value}
onChange={(event) => {
onChange(event);
$onChange?.(event);
}}
onBlur={(event) => {
onBlur(event);
$onBlur?.(event);
}}
/>
)}
</Field>
),
);

FormInput.displayName = 'FormInput';

export default FormInput;
5 changes: 5 additions & 0 deletions src/components/FormInput/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { FormInput } from './FormInput';

export { FormInput };

export default FormInput;

0 comments on commit e9a8ea6

Please sign in to comment.