Skip to content

Commit

Permalink
refactor: split formFields into separate files in common/components/f…
Browse files Browse the repository at this point in the history
…orms/
  • Loading branch information
AdamNowotny committed Aug 29, 2024
1 parent f5c4d57 commit 6bd4883
Show file tree
Hide file tree
Showing 13 changed files with 211 additions and 186 deletions.
177 changes: 0 additions & 177 deletions src/common/components/formFields.tsx

This file was deleted.

29 changes: 29 additions & 0 deletions src/common/components/forms/booleanField.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import React from 'react';
import { FormSelectField } from './selectField';

export const FormBooleanField = ({
label,
onSelect,
items = { true: 'On', false: 'Off' },
activeItem,
disabled,
}: {
label: string;
onSelect: (value: boolean) => void;
items?: Record<string, string>;
activeItem?: boolean;
disabled?: boolean;
}) => {
const onSelectHandler = (value: string | null) => {
onSelect(value === 'true');
};
return (
<FormSelectField
label={label}
activeItem={activeItem ? 'true' : 'false'}
items={items}
onSelect={onSelectHandler}
disabled={disabled}
/>
);
};
32 changes: 32 additions & 0 deletions src/common/components/forms/buttonField.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import React from 'react';
import { Form, Row, Col, Button } from 'react-bootstrap';

export const FormButtonField = ({
disabled,
text,
icon,
onClick,
style = 'success',
}: {
disabled?: boolean;
text: string;
icon?: string;
onClick: () => void;
style?: 'success' | 'danger';
}) => {
return (
<>
<Form.Group>
<Row className="text-center">
<Col />
<Col sm="auto">
<Button onClick={onClick} variant={style} disabled={disabled}>
{icon && <i className={`fa fa-${icon}`}></i>} {text}
</Button>
</Col>
<Col />
</Row>
</Form.Group>
</>
);
};
21 changes: 21 additions & 0 deletions src/common/components/forms/field.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import React from 'react';
import { Col, Form, Row } from 'react-bootstrap';

export const FormField = ({
children,
label,
disabled,
}: {
children: React.JSX.Element;
label: string;
disabled?: boolean;
}) => {
return (
<Form.Group as={Row} className={`mb-1 ${disabled ? 'text-muted' : ''}`}>
<Form.Label column sm="7">
{label}
</Form.Label>
<Col sm="5">{children}</Col>
</Form.Group>
);
};
13 changes: 13 additions & 0 deletions src/common/components/forms/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { FormBooleanField } from './booleanField';
import { FormButtonField } from './buttonField';
import { FormInputField } from './inputField';
import { FormNumberField } from './numberField';
import { FormSelectField } from './selectField';

export {
FormSelectField,
FormNumberField,
FormBooleanField,
FormInputField,
FormButtonField,
};
41 changes: 41 additions & 0 deletions src/common/components/forms/inputField.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import React from 'react';
import { Form, InputGroup } from 'react-bootstrap';

export const FormInputField = ({
disabled,
text,
icon,
onChange,
placeholder,
type = 'text',
}: {
disabled?: boolean;
text: string;
icon?: string;
onChange: (value: string) => void;
placeholder?: string;
type: 'text' | 'url' | 'password';
}) => {
return (
<>
<Form.Group>
<InputGroup className="mb-3">
{icon && (
<InputGroup.Text>
<i className={`fa fa-${icon}`}></i>
</InputGroup.Text>
)}
<Form.Control
disabled={disabled}
type={type}
defaultValue={text}
onChange={e => {
onChange(e.target.value);
}}
placeholder={placeholder}
/>
</InputGroup>
</Form.Group>
</>
);
};
35 changes: 35 additions & 0 deletions src/common/components/forms/numberField.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import React, { useContext } from 'react';
import { Form } from 'react-bootstrap';
import { FormField } from './field';
import { ViewConfigContext } from '../react-types';

export const FormNumberField = ({
label,
onChange,
min,
max,
disabled,
}: {
label: string;
onChange: (value: number) => void;
min?: number;
max?: number;
disabled?: boolean;
}) => {
const viewConfig = useContext(ViewConfigContext);
const onChangeHandler = e => {
let value = e.target.value;
if (min && value < min) value = min;
if (max && value > max) value = max;
onChange(parseInt(value));
};
return (
<FormField label={label} disabled={disabled}>
<Form.Control
type="number"
defaultValue={viewConfig.columns}
onChange={onChangeHandler}
/>
</FormField>
);
};
31 changes: 31 additions & 0 deletions src/common/components/forms/selectField.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import React from 'react';
import { Nav } from 'react-bootstrap';
import { FormField } from './field';

export const FormSelectField = ({
label,
onSelect,
items,
activeItem,
disabled,
}: {
label: string;
onSelect: (value: string | null) => void;
items: Record<string, string>;
activeItem?: string;
disabled?: boolean;
}) => {
return (
<FormField label={label} disabled={disabled}>
<Nav justify variant="pills" activeKey={activeItem} onSelect={onSelect}>
{Object.entries(items).map(([key, value]) => (
<Nav.Item key={key} className="me-1">
<Nav.Link eventKey={key} disabled={disabled}>
{value}
</Nav.Link>
</Nav.Item>
))}
</Nav>
</FormField>
);
};
2 changes: 1 addition & 1 deletion src/options/pages/configuration/components/jsonEditor.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { FormButtonField } from 'common/components/formFields';
import { FormButtonField } from 'common/components/forms';
import React, { useState } from 'react';
import { Col, Form, Row } from 'react-bootstrap';
import './jsonEditor.css';
Expand Down
Loading

0 comments on commit 6bd4883

Please sign in to comment.