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

OLMIS-7987: react components update #53

Merged
merged 1 commit into from
Oct 20, 2024
Merged
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
90 changes: 43 additions & 47 deletions src/react-components/inputs/numeric-input.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,69 +14,65 @@
*/

import React from 'react';
import WebTooltip from '../modals/web-tooltip';

const NumericInput = ({
onChange,
numeric,
allowNegative = false,
isInvalid,
errorMessage,
...props
onChange,
numeric,
allowNegative = false,
isInvalid,
errorMessage,
...props
}) => {

const inputProps = numeric ? {
className: 'number',
...props,
inputMode: 'numeric',
pattern: '[0-9]*'
} : props;
const inputProps = numeric ? {
className: 'number',
...props,
disabled: props.disabled,
inputMode: 'numeric',
pattern: '[0-9]*'
} : props;

const parseNumber = stringValue => {
const n = Number(stringValue);
const parseNumber = stringValue => {
const n = Number(stringValue);

if (Number.isNaN(n) || !Number.isSafeInteger(n)) {
return null;
}
if (Number.isNaN(n) || !Number.isSafeInteger(n)) {
return null;
}

return n;
};
return n;
};

const handleChange = (event) => {
const { value } = event.target;
const handleChange = (event) => {
const { value } = event.target;

if (value === '') {
onChange(null);
return;
}
if (value === '') {
onChange(null);
return;
}

const positiveNumberRegex = '^\\d+$';
const negativeNumberRegex = '^\\-?\d*$';
const positiveNumberRegex = '^\\d+$';
const negativeNumberRegex = '^\\-?\d*$';

const numberRegex = allowNegative ? negativeNumberRegex : positiveNumberRegex;
const numberRegex = allowNegative ? negativeNumberRegex : positiveNumberRegex;

const match = value.match(numberRegex);
const match = value.match(numberRegex);

if (match !== null) {
const parsedNumber = parseNumber(value);
if (match !== null) {
const parsedNumber = parseNumber(value);

if (parsedNumber !== null) {
onChange(parsedNumber);
}
}
};
if (parsedNumber !== null) {
onChange(parsedNumber);
}
}
};

return (
<WebTooltip shouldDisplayTooltip={isInvalid} tooltipContent={errorMessage}>
<div className={`input-control ${isInvalid ? 'is-invalid' : ''} ${props.disabled ? 'is-disabled' : ''}`}>
<input
className='number'
type='text'
onChange={handleChange}
{...inputProps}
/>
</div>
</WebTooltip>
<input
className='number'
type='text'
onChange={handleChange}
{...inputProps}
/>
);
};

Expand Down
8 changes: 4 additions & 4 deletions src/react-components/table/input-cell.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ const InputCell = ({
updateTableData,
validateRow,
showValidationErrors,
numeric = false
numeric = false,
disabled = false
}) => {
const [value, setValue] = useState(initialValue);
const [valid, setValid] = useState(true);

const onChange = val => {
setValue(val);

Expand Down Expand Up @@ -58,8 +58,8 @@ const InputCell = ({

return (
<div className={`form-control ${valid ? '' : 'is-invalid'}`}>
{numeric && <NumericInput value={value} onChange={onChange} onBlur={onBlur}/>}
{!numeric && <Input value={value} onChange={onChange} onBlur={onBlur}/>}
{numeric && <NumericInput value={value} onChange={onChange} onBlur={onBlur} disabled={disabled} />}
{!numeric && <Input value={value} onChange={onChange} onBlur={onBlur} disabled={disabled} />}
</div>
);
};
Expand Down
Loading