Skip to content

Commit

Permalink
5730 - Form select field (#5788)
Browse files Browse the repository at this point in the history
* created folder for form components

* basic story for select

* changes to form fields props

* eslint rule update

* render with error

* fixed eslint error

* restored incorrect change to eslint

* added event handlers
  • Loading branch information
Zasa-san committed May 8, 2023
1 parent 57cfbc1 commit dcaaec0
Show file tree
Hide file tree
Showing 8 changed files with 133 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,35 @@ import React, { ChangeEventHandler, Ref } from 'react';
import { XMarkIcon } from '@heroicons/react/20/solid';

interface InputFieldProps {
fieldID?: string;
label?: string | React.ReactNode;
onChange?: ChangeEventHandler<HTMLInputElement>;
onBlur?: ChangeEventHandler<HTMLInputElement>;
name?: string;
ref?: Ref<any>;
value?: string;
id: string;
label: string | React.ReactNode;
disabled?: boolean;
hideLabel?: boolean;
placeholder?: string;
hasErrors?: boolean;
clearFieldAction?: () => any;
value?: string;
className?: string;
name?: string;
clearFieldAction?: () => any;
onChange?: ChangeEventHandler<HTMLInputElement>;
onBlur?: ChangeEventHandler<HTMLInputElement>;
}

const InputField = React.forwardRef(
(
{
fieldID,
id,
label,
disabled,
hideLabel,
placeholder,
hasErrors,
value,
clearFieldAction,
className,
name = '',
clearFieldAction,
onChange = () => {},
onBlur = () => {},
name = '',
}: InputFieldProps,
ref: Ref<any>
) => {
Expand All @@ -52,13 +51,13 @@ const InputField = React.forwardRef(

return (
<div className={className}>
<label htmlFor={fieldID} className={hideLabel ? 'sr-only' : ''}>
<label htmlFor={id} className={hideLabel ? 'sr-only' : ''}>
{label}
</label>
<div className="relative w-full">
<input
type="text"
id={fieldID}
id={id}
onChange={onChange}
onBlur={onBlur}
name={name}
Expand Down
63 changes: 63 additions & 0 deletions app/react/V2/Components/Forms/Select.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import React, { ChangeEventHandler, Ref } from 'react';

interface SelectProps {
id: string;
label: string | React.ReactNode;
options: { key: string; value: string }[];
disabled?: boolean;
hideLabel?: boolean;
hasErrors?: boolean;
className?: string;
name?: string;
onChange?: ChangeEventHandler<HTMLSelectElement>;
onBlur?: ChangeEventHandler<HTMLSelectElement>;
}

const Select = React.forwardRef(
(
{
id,
label,
options,
disabled,
hideLabel,
hasErrors,
className,
name = '',
onChange = () => {},
onBlur = () => {},
}: SelectProps,
ref: Ref<any>
) => {
const fieldStyles = hasErrors
? 'border-error-300 focus:border-error-500 focus:ring-error-500 border-2 text-red-900'
: 'border-gray-300 border text-gray-900';

return (
<div className={className}>
<div className="relative w-full">
<label htmlFor={id} className={hideLabel ? 'sr-only' : ''}>
{label}
</label>
<select
className={`${fieldStyles} disabled:text-gray-500 rounded-lg bg-gray-50 block flex-1 w-full text-sm p-2.5`}
id={id}
disabled={disabled}
ref={ref}
name={name}
onBlur={onBlur}
onChange={onChange}
>
{options.map(({ key, value }) => (
<option key={key} value={value}>
{value}
</option>
))}
</select>
</div>
</div>
);
}
);

export { Select };
4 changes: 4 additions & 0 deletions app/react/V2/Components/Forms/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { InputField } from './InputField';
import { Select } from './Select';

export { InputField, Select };
4 changes: 2 additions & 2 deletions app/react/V2/Components/Translations/FormInput.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from 'react';
import { UseFormGetFieldState, UseFormRegister, UseFormSetValue } from 'react-hook-form';
import { Translate } from 'app/I18N';
import { InputField } from '../UI';
import { InputField } from '../Forms';

type fromPropsType = {
register: UseFormRegister<any>;
Expand All @@ -20,7 +20,7 @@ const FormInput = (data: any, formProps: fromPropsType) => {
return (
<div>
<InputField
fieldID={data.cell.value}
id={data.cell.value}
label={data.cell.row.values.language}
hideLabel
disabled={submitting}
Expand Down
5 changes: 4 additions & 1 deletion app/react/V2/Components/UI/SearchMultiSelect.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React, { useEffect, useState } from 'react';
import { Translate } from 'app/I18N';
import { InputField } from './InputField';
import { InputField } from '../Forms';
import { Pill } from './Pill';

interface SearchMultiselectProps {
Expand All @@ -27,6 +27,9 @@ const SearchMultiselect = ({ items, onChange }: SearchMultiselectProps) => {
<div>
<div className="mb-4 sticky top-0 w-full">
<InputField
id="search-multiselect"
label="search-multiselect"
hideLabel
onChange={e => setSearchTerm(e.target.value)}
placeholder="Search"
value={searchTerm}
Expand Down
2 changes: 0 additions & 2 deletions app/react/V2/Components/UI/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { NavigationHeader } from './NavigationHeader';
import { Pill } from './Pill';
import { Table } from './Table';
import { ToggleButton } from './ToggleButton';
import { InputField } from './InputField';
import { ModalContainer } from './ModalContainer';
import { NotificationsContainer } from './NotificationsContainer';
import { Sidepanel } from './Sidepanel';
Expand All @@ -16,7 +15,6 @@ export {
Pill,
Table,
ToggleButton,
InputField,
ModalContainer,
NotificationsContainer,
Sidepanel,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import React from 'react';
import { ComponentStory, ComponentMeta } from '@storybook/react';
import { Provider } from 'react-redux';
import { LEGACY_createStore as createStore } from 'V2/shared/testingHelpers';
import { InputField } from 'V2/Components/UI/InputField';
import { InputField } from 'V2/Components/Forms';

const InputFieldStory = {
title: 'Components/InputField',
title: 'Forms/InputField',
component: InputField,
};

Expand All @@ -14,7 +14,7 @@ const Template: ComponentStory<typeof InputField> = args => (
<div className="tw-content">
<div className="md:w-1/2">
<InputField
fieldID={args.fieldID}
id={args.id}
label={<p className="text-lg mb-2">{args.label}</p>}
disabled={args.disabled}
hideLabel={args.hideLabel}
Expand All @@ -33,7 +33,7 @@ const WithClearFieldButton = Template.bind({});
const WithError = Template.bind({});

Basic.args = {
fieldID: '1',
id: '1',
label: 'Input field label',
disabled: false,
hideLabel: false,
Expand Down
44 changes: 44 additions & 0 deletions app/react/stories/Forms/Select.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import React from 'react';
import { ComponentStory, ComponentMeta } from '@storybook/react';
import { Select } from 'V2/Components/Forms';

const InputFieldStory = {
title: 'Forms/Select',
component: Select,
};

const Template: ComponentStory<typeof Select> = args => (
<div className="tw-content">
<div className="md:w-1/2">
<Select
id="1"
label={<p className="text-lg mb-2">{args.label}</p>}
options={args.options}
disabled={args.disabled}
hideLabel={args.hideLabel}
hasErrors={args.hasErrors}
/>
</div>
</div>
);

const Basic = Template.bind({});

Basic.args = {
label: 'Please select an option',
disabled: false,
hideLabel: false,
hasErrors: false,
options: [
{ key: '1', value: 'Algeria' },
{ key: '2', value: 'Argentina' },
{ key: '3', value: 'Bavaria' },
{ key: '4', value: 'Bolivia' },
{ key: '5', value: 'Colombia' },
{ key: '6', value: 'Dinamarca' },
],
};

export { Basic };

export default InputFieldStory as ComponentMeta<typeof Select>;

0 comments on commit dcaaec0

Please sign in to comment.