Skip to content

Commit 21d5ae2

Browse files
authored
Merge pull request #2 from qoretechnologies/feature/forms-and-fields
Forms and fields
2 parents 01a6d87 + 02fc007 commit 21d5ae2

File tree

7 files changed

+109
-5
lines changed

7 files changed

+109
-5
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@qoretechnologies/reqraft",
3-
"version": "0.0.1",
3+
"version": "0.1.0",
44
"description": "ReQraft is a collection of React components and hooks that are used across Qore Technologies' products made using the ReQore component library from Qore.",
55
"main": "dist/index.js",
66
"types": "dist/index.d.ts",

src/components/form/fields/Field.tsx

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { TFormFieldType, TFormFieldValueType } from '../../../types/Form';
2+
import { FormStringField } from './string/String';
3+
4+
export interface IFormFieldProps<T extends TFormFieldType = TFormFieldType> {
5+
type?: T;
6+
value: TFormFieldValueType<T>;
7+
onChange: (value: TFormFieldValueType<T>, event?: unknown) => void;
8+
9+
validateSelf?: boolean;
10+
onValidateChange?: (isValid: boolean) => void;
11+
}
12+
13+
export const FormField = <T extends TFormFieldType>({
14+
type,
15+
onChange,
16+
...rest
17+
}: IFormFieldProps<T>) => {
18+
const handleChange = (value: TFormFieldValueType<T>, event?: unknown) => {
19+
onChange(value, event);
20+
};
21+
22+
const renderField = (type: T) => {
23+
switch (type) {
24+
case 'string':
25+
return (
26+
<FormStringField
27+
{...rest}
28+
onChange={(value: string) => handleChange(value as TFormFieldValueType<T>)}
29+
/>
30+
);
31+
default:
32+
return null;
33+
}
34+
};
35+
36+
return renderField(type);
37+
};

src/components/form/string/String.stories.tsx src/components/form/fields/string/String.stories.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { StoryObj } from '@storybook/react';
2-
import { StoryMeta } from '../../../types';
2+
import { StoryMeta } from '../../../../types';
33
import { FormStringField } from './String';
44

55
const meta = {

src/components/form/string/String.tsx src/components/form/fields/string/String.tsx

+3-2
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,16 @@ import { IReqoreControlGroupProps } from '@qoretechnologies/reqore/dist/componen
33
import { IReqoreInputProps } from '@qoretechnologies/reqore/dist/components/Input';
44
import { IReqoreTagProps } from '@qoretechnologies/reqore/dist/components/Tag';
55
import { ChangeEvent, useCallback } from 'react';
6+
import { TFormFieldValueType } from '../../../../types/Form';
67

78
export interface IStringFormFieldProps extends Omit<IReqoreInputProps, 'onChange' | 'value'> {
89
sensitive?: boolean;
9-
value?: string;
10+
value?: TFormFieldValueType<'string'>;
1011
label?: IReqoreTagProps['label'];
1112
labelPosition?: 'top' | 'left' | 'right' | 'bottom';
1213
labelProps?: IReqoreTagProps;
1314
wrapperProps?: IReqoreControlGroupProps;
14-
onChange?: (value?: string, event?: ChangeEvent<HTMLInputElement>) => void;
15+
onChange?: (value?: TFormFieldValueType<'string'>, event?: ChangeEvent<HTMLInputElement>) => void;
1516
}
1617

1718
export const FormStringField = ({

src/hooks/useValidation.ts

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { TFormFieldType } from '../types/Form';
2+
3+
// @ts-expect-error "need to implement this"
4+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
5+
export const useValidation = (value: any, type?: TFormFieldType) => {
6+
// Build validation...
7+
8+
return true;
9+
};

src/index.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export { FormStringField, IStringFormFieldProps } from './components/form/string/String';
1+
export { FormStringField, IStringFormFieldProps } from './components/form/fields/string/String';

src/types/Form.ts

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
export type TFormFieldType =
2+
| 'string'
3+
| 'number'
4+
| 'boolean'
5+
| 'date'
6+
| 'time'
7+
| 'datetime'
8+
| 'select'
9+
| 'multiSelect'
10+
| 'radio'
11+
| 'checkbox'
12+
| 'file'
13+
| 'image'
14+
| 'color'
15+
| 'password'
16+
| 'email'
17+
| 'phone'
18+
| 'url'
19+
| 'markdown';
20+
21+
export type TFormFieldValueType<T> = T extends 'string'
22+
? string
23+
: T extends 'number'
24+
? number
25+
: T extends 'boolean'
26+
? boolean
27+
: T extends 'date'
28+
? Date | string
29+
: T extends 'time'
30+
? Date | string
31+
: T extends 'datetime'
32+
? Date | string
33+
: T extends 'select'
34+
? string
35+
: T extends 'multiSelect'
36+
? string[]
37+
: T extends 'radio'
38+
? string
39+
: T extends 'checkbox'
40+
? boolean
41+
: T extends 'file'
42+
? File
43+
: T extends 'image'
44+
? string
45+
: T extends 'color'
46+
? string
47+
: T extends 'password'
48+
? string
49+
: T extends 'email'
50+
? string
51+
: T extends 'phone'
52+
? string
53+
: T extends 'url'
54+
? string
55+
: T extends 'markdown'
56+
? string
57+
: any;

0 commit comments

Comments
 (0)