Skip to content

Controlled & Uncontrolled Component #3

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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
29 changes: 28 additions & 1 deletion src/components/pages/about-page/AboutPage.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,41 @@
import React from 'react';
import React, { useState } from 'react';
import Link from 'next/link';
import { Routes } from '../../../constants/Routes';
import { UiSelect } from '../../ui/ui-select/UiSelect';

interface IProps {}

export const AboutPage: React.FC<IProps> = (props) => {
const [controlledValue, setControlledValue] = useState<string>('3');

return (
<div>
<h1>About</h1>
<p>This is the about page</p>
<div>
<UiSelect
id="uncontrolled"
onChange={(value) => console.log(value, typeof value)}
initialValue="2"
options={[
{ label: 'One', value: '1' },
{ label: 'Two', value: '2' },
{ label: 'Three', value: '3' },
{ label: 'Four', value: '4' },
]}
/>
<UiSelect
id="controlled"
onChange={(value) => setControlledValue(value)}
value={controlledValue}
options={[
{ label: 'One', value: '1' },
{ label: 'Two', value: '2' },
{ label: 'Three', value: '3' },
{ label: 'Four', value: '4' },
]}
/>
</div>
<p>
<Link href={Routes.Index}>
<a>Go home</a>
Expand Down
4 changes: 4 additions & 0 deletions src/components/ui/ui-select/UiSelect.constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export interface IUiSelectOptions {
label: string;
value: string | number;
}
44 changes: 44 additions & 0 deletions src/components/ui/ui-select/UiSelect.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import React, { useEffect, useState } from 'react';
import { IUiSelectOptions } from './UiSelect.constants';
import { noop } from '../../../utils/misc.utils';

interface IProps {
options: IUiSelectOptions[];
/**
* If a value is passed in then the component is considered controlled.
*/
value?: string;
id?: string;
name?: string;
onChange?: (value: string) => void;
initialValue?: string;
}

export const UiSelect: React.FC<IProps> = (props) => {
const [selected, setSelected] = useState(props.value || props.initialValue);

useEffect(() => {
setSelected(props.value);
}, [props.value]);

return (
<select
value={selected}
name={props.name}
id={props.id}
onChange={(event: React.ChangeEvent<HTMLSelectElement>) => {
props.onChange!(event.target.value);
}}
>
{props.options.map((option) => (
<option key={option.label} value={option.value}>
{option.label}
</option>
))}
</select>
);
};

UiSelect.defaultProps = {
onChange: noop,
};