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

Changed function responsible for trimming leading zeros. #87

Merged
merged 3 commits into from
Nov 7, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
20 changes: 14 additions & 6 deletions cogboard-webapp/src/components/BoardForm/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,14 @@ const BoardForm = ({ onSubmit, renderActions, boardId, ...initialFormValues }) =
const validationSchema = createValidationSchema(boardId, boards);
const {values, handleChange, handleSubmit, errors} = useFormData(initialFormValues, validationSchema, true);

const numberInputHandler = (event) => {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Change the name to handleNumberInput or handleInput according to convention.

const { target: { value } } = event;

event.target.value = trimLeadingZeros(value);
};

return (
<form onSubmit={handleSubmit(onSubmit)} novalidate="novalidate">
<form onSubmit={handleSubmit(onSubmit)} noValidate="novalidate">
<StyledFieldset component="fieldset">
<TextField
onChange={handleChange('title')}
Expand All @@ -28,7 +34,7 @@ const BoardForm = ({ onSubmit, renderActions, boardId, ...initialFormValues }) =
label="Title"
margin="normal"
value={values.title}
error={errors.title}
error={errors.title !== undefined}
helperText={
<StyledValidationMessages
messages={errors.title}
Expand All @@ -38,7 +44,7 @@ const BoardForm = ({ onSubmit, renderActions, boardId, ...initialFormValues }) =
/>
<NumberInput
onChange={handleChange('columns')}
onInput={trimLeadingZeros}
onInput={numberInputHandler}
id="columns"
InputLabelProps={{
shrink: true
Expand All @@ -47,7 +53,8 @@ const BoardForm = ({ onSubmit, renderActions, boardId, ...initialFormValues }) =
label="Columns"
margin="normal"
value={values.columns}
error={errors.columns}
error={errors.columns !== undefined}
FormHelperTextProps={{ component: 'div' }}
helperText={
<StyledValidationMessages
messages={errors.columns}
Expand All @@ -71,15 +78,16 @@ const BoardForm = ({ onSubmit, renderActions, boardId, ...initialFormValues }) =
{values.autoSwitch &&
<NumberInput
onChange={handleChange('switchInterval')}
onInput={trimLeadingZeros}
onInput={numberInputHandler}
id="switchInterval"
InputLabelProps={{
shrink: true
}}
label="Switch interval [s]"
margin="normal"
value={values.switchInterval}
error={errors.switchInterval}
error={errors.switchInterval !== undefined}
FormHelperTextProps={{ component: 'div' }}
helperText={
<StyledValidationMessages
messages={errors.switchInterval}
Expand Down
9 changes: 4 additions & 5 deletions cogboard-webapp/src/components/helpers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,10 @@ export const parseYupErrors = (errors) => {
});

return result;
}
};

export const trimLeadingZeros = (event) => {
const inputValue = event.target.value;
export const trimLeadingZeros = (inputValue) => {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Change it to oneliner => String(parseInt(inputValue)). In my opinion it is clear what parseInt is doing, so it will be readable.

const parsedValue = parseInt(inputValue);

event.target.value = parsedValue.toString();
}
return parsedValue.toString();
};