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

Likhith/76940/ts migrate toggle switch toast tick progress #37

Merged
Show file tree
Hide file tree
Changes from 4 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
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import TickProgress from './tick-progress.jsx';
import TickProgress from './tick-progress';
import './tick-progress.scss';

export default TickProgress;
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
import classNames from 'classnames';
import PropTypes from 'prop-types';
import React from 'react';

const Tick = ({ is_on }) => {
type TTick = {
is_on?: boolean;
};

type TTickProgress = {
className: string;
columns: number;
value: number;
rows: number;
size: number;
};

const Tick = ({ is_on }: TTick) => {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if it only has one argument, why not typing it here instead of having TTick? 🤔

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It can be changed as u suggested.

return (
<div
className={classNames('dc-tick-progress__tick', {
Expand All @@ -12,7 +23,7 @@ const Tick = ({ is_on }) => {
);
};

const TickProgress = ({ className, rows, columns, size, value }) => {
const TickProgress = ({ className, rows = 2, columns = 5, size = 10, value = 0 }: Partial<TTickProgress>) => {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@likhith-deriv Same here 🤔

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought of using the interfaces that React provides. I don't think Partial makes the code less readable

return (
<div
className={classNames('dc-tick-progress', className)}
Expand All @@ -21,26 +32,11 @@ const TickProgress = ({ className, rows, columns, size, value }) => {
gridTemplateColumns: `repeat(${columns}, 1fr)`,
}}
>
{new Array(size).fill(null).map((item, index) => {
{new Array(size).fill(null).map((_, index) => {
return <Tick is_on={index < value} key={index} />;
})}
</div>
);
};

TickProgress.defaultProps = {
columns: 5,
rows: 2,
size: 10,
value: 0,
};

export default TickProgress;

TickProgress.propTypes = {
className: PropTypes.string,
columns: PropTypes.number,
value: PropTypes.number,
rows: PropTypes.number,
size: PropTypes.number,
};
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Toast from './toast.jsx';
import Toast from './toast';
import './toast.scss';

export default Toast;
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,24 @@ import PropTypes from 'prop-types';
import React from 'react';
import { CSSTransition } from 'react-transition-group';

const Toast = ({ children, className, is_open = true, onClose, onClick, type = 'info', timeout = 0 }) => {
type TToast = {
className: string;
is_open: boolean;
onClick: (event: React.MouseEvent<HTMLElement>) => void;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
onClick: (event: React.MouseEvent<HTMLElement>) => void;
onClick: React.MouseEventHandler;

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I keep getting this error if I changes as u suggested
Screen Shot 2022-10-03 at 9 11 56 AM

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@likhith-deriv It seems like you have given React.MouseEventHandler to event property, It should be as following:

onClick: React.MouseEventHandler;

Or to be more specific:

onClick: React.MouseEventHandler<HTMLElement>;

onClose: () => void;
type: 'error' | 'info';
timeout: number;
};

const Toast = ({
children,
className,
is_open = true,
onClose,
onClick,
type = 'info',
timeout = 0,
}: React.PropsWithChildren<Partial<TToast>>) => {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@likhith-deriv Why use Partial here? 🤔 If all the properties are optional then why not make them optional in the TToast type for better readability? 🤔

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought of using the interfaces that React provides. I don't think Partial makes the code less readable

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@likhith-deriv Partial is a utility type from TypeScript, Not React. We mainly use it in test files for mock data where you have a big object and you only need part of it for the test.

The props type of a component defines the signature of that component, As a dev, I will be looking at the type definition to understand what this component needs to work, So if I check the type I will see everything as required and I can easily miss the part Partial has been used, So by doing this we are making devs to look for two places to understand how things work which we are losing the code reality.

It's nothing important tho 😅 Let's move forward 🙇🏻

const [is_visible, setVisible] = React.useState(false);

React.useEffect(() => {
Expand Down Expand Up @@ -56,14 +73,4 @@ const Toast = ({ children, className, is_open = true, onClose, onClick, type = '
);
};

Toast.propTypes = {
className: PropTypes.string,
is_open: PropTypes.bool,
onClick: PropTypes.func,
onClose: PropTypes.func,
type: PropTypes.oneOf(['error', 'info']),
timeout: PropTypes.number,
children: PropTypes.oneOfType([PropTypes.node, PropTypes.array]),
};

export default Toast;
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import ToggleSwitch from './toggle-switch.jsx';
import ToggleSwitch from './toggle-switch';
import './toggle-switch.scss';

export default ToggleSwitch;
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
import * as React from 'react';
import PropTypes from 'prop-types';
import classNames from 'classnames';

const ToggleSwitch = ({ className, classNameButton, classNameLabel, handleToggle, id, is_enabled }) => {
type TToggleSwitch = {
className?: string;
classNameButton?: string;
classNameLabel?: string;
handleToggle: () => void;
id: string;
is_enabled: boolean;
};

const ToggleSwitch = ({ className, classNameButton, classNameLabel, handleToggle, id, is_enabled }: TToggleSwitch) => {
return (
<React.Fragment>
<input
Expand All @@ -19,13 +27,4 @@ const ToggleSwitch = ({ className, classNameButton, classNameLabel, handleToggle
);
};

ToggleSwitch.propTypes = {
className: PropTypes.string,
classNameButton: PropTypes.string,
classNameLabel: PropTypes.string,
handleToggle: PropTypes.func.isRequired,
id: PropTypes.string.isRequired,
is_enabled: PropTypes.bool.isRequired,
};

export default ToggleSwitch;