Skip to content
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
63 changes: 63 additions & 0 deletions apps/content-insights/src/components/TextInputInteger.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import React from 'react';
import { TextInput, TextInputProps } from '@contentful/f36-components';

interface TextInputIntegerProps extends Omit<TextInputProps, 'value' | 'onChange'> {
value: number | undefined;
onChange: (value: number | undefined) => void;
}

const TextInputInteger: React.FC<TextInputIntegerProps> = ({
value,
onChange,
...restProps
}: TextInputIntegerProps) => {
const hasFloatOrNegativeChars = (text: string): boolean => {
return text.includes('.') || text.includes(',') || text.includes('-');
};

const handleKeyDown = (e: React.KeyboardEvent<HTMLInputElement>) => {
if (hasFloatOrNegativeChars(e.key)) {
e.preventDefault();
}
};

const handlePaste = (e: React.ClipboardEvent<HTMLInputElement>) => {
const pastedText = e.clipboardData.getData('text');

if (hasFloatOrNegativeChars(pastedText)) {
e.preventDefault();
}
};

const handleChange = (e: React.ChangeEvent<HTMLInputElement | HTMLTextAreaElement>) => {
const inputValue = e.target.value;

if (hasFloatOrNegativeChars(inputValue)) {
return;
}

const digitsOnly = inputValue.replace(/\D/g, '');

let numValue: number | undefined = parseInt(digitsOnly, 10);

if (isNaN(numValue) || digitsOnly === '') {
numValue = undefined;
}

onChange(numValue);
};

return (
<TextInput
{...restProps}
type="number"
step={1}
value={value !== undefined ? String(value) : ''}
onChange={handleChange}
onKeyDown={handleKeyDown}
onPaste={handlePaste}
/>
);
};

export default TextInputInteger;
39 changes: 12 additions & 27 deletions apps/content-insights/src/locations/ConfigScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {
Switch,
Text,
Paragraph,
TextInput,
Image,
Select,
Tooltip,
Expand All @@ -27,6 +26,7 @@ import appearanceImage from '../assets/appearance.png';
import { styles } from './ConfigScreen.styles';
import { Validator } from '../utils/Validator';
import { InfoIcon } from '@contentful/f36-icons';
import TextInputInteger from '../components/TextInputInteger';

export interface AppInstallationParameters {
defaultContentTypes?: string[];
Expand Down Expand Up @@ -107,20 +107,10 @@ const ConfigScreen = () => {
})();
}, [sdk]);

const handleOnChangeInput = (
e: React.ChangeEvent<HTMLInputElement | HTMLTextAreaElement>,
field: ConfigField
) => {
const value = e.target.value;
let numValue: number | undefined = parseInt(value, 10);

if (isNaN(numValue)) {
numValue = undefined;
}

const handleIntegerChange = (field: ConfigField) => (value: number | undefined) => {
setParameters((prev) => ({
...prev,
[field]: numValue,
[field]: value,
}));

if (errors[field]) {
Expand Down Expand Up @@ -173,12 +163,11 @@ const ConfigScreen = () => {
<FormControl.Label>
Content &quot;Needs update&quot; time threshold (months)
</FormControl.Label>
<TextInput
<TextInputInteger
id="needs-update-months"
name="needs-update-months"
type="number"
value={parameters.needsUpdateMonths ? String(parameters.needsUpdateMonths) : ''}
onChange={(event) => handleOnChangeInput(event, ConfigField.NeedsUpdateMonths)}
value={parameters.needsUpdateMonths}
onChange={handleIntegerChange(ConfigField.NeedsUpdateMonths)}
/>
{errors.needsUpdateMonths && (
<FormControl.ValidationMessage>
Expand All @@ -199,14 +188,11 @@ const ConfigScreen = () => {
<FormControl.Label>
&quot;Recently published&quot; time period (days)
</FormControl.Label>
<TextInput
<TextInputInteger
id="recently-published-days"
name="recently-published-days"
type="number"
value={
parameters.recentlyPublishedDays ? String(parameters.recentlyPublishedDays) : ''
}
onChange={(event) => handleOnChangeInput(event, ConfigField.RecentlyPublishedDays)}
value={parameters.recentlyPublishedDays}
onChange={handleIntegerChange(ConfigField.RecentlyPublishedDays)}
/>
{errors.recentlyPublishedDays && (
<FormControl.ValidationMessage>
Expand All @@ -222,12 +208,11 @@ const ConfigScreen = () => {

<FormControl marginBottom="spacingL" isRequired isInvalid={!!errors.timeToPublishDays}>
<FormControl.Label>Time to publish threshold (days)</FormControl.Label>
<TextInput
<TextInputInteger
id="time-to-publish-days"
name="time-to-publish-days"
type="number"
value={parameters.timeToPublishDays ? String(parameters.timeToPublishDays) : ''}
onChange={(event) => handleOnChangeInput(event, ConfigField.TimeToPublishDays)}
value={parameters.timeToPublishDays}
onChange={handleIntegerChange(ConfigField.TimeToPublishDays)}
/>
{errors.timeToPublishDays && (
<FormControl.ValidationMessage>
Expand Down