@jsb188/form 0.0.0-stage → 1.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (64) hide show
  1. package/README.md +132 -2
  2. package/css/form.css +2 -0
  3. package/package.json +40 -5
  4. package/src/assets.d.ts +1 -0
  5. package/src/libs/SchemaForm.tsx +158 -0
  6. package/src/libs/SchemaForm.types.ts +165 -0
  7. package/src/libs/defineForm.ts +11 -0
  8. package/src/libs/rules.ts +126 -0
  9. package/src/libs/useField.ts +16 -0
  10. package/src/libs/useSchemaForm.helpers.ts +71 -0
  11. package/src/libs/useSchemaForm.ts +268 -0
  12. package/src/libs/validation.ts +42 -0
  13. package/src/main.ts +40 -0
  14. package/src/modules/FieldLabel.tsx +28 -0
  15. package/src/modules/Form-Input.tsx +2 -0
  16. package/src/modules/Form-Line.tsx +126 -0
  17. package/src/modules/Form.helpers.ts +12 -0
  18. package/src/modules/Form.tsx +14 -0
  19. package/src/modules/FormItem.tsx +117 -0
  20. package/src/modules/FormOptions.tsx +12 -0
  21. package/src/modules/FormUI.tsx +2 -0
  22. package/src/modules/Input.tsx +16 -0
  23. package/src/modules/InputClick.tsx +48 -0
  24. package/src/modules/InputWithButton.tsx +37 -0
  25. package/src/modules/ListItemForm.tsx +345 -0
  26. package/src/modules/ListItemForm.types.ts +50 -0
  27. package/src/modules/PasswordInput.tsx +36 -0
  28. package/src/modules/PopOverInput.tsx +75 -0
  29. package/src/modules/SMSCodeInput.tsx +118 -0
  30. package/src/modules/SchemaField.tsx +104 -0
  31. package/src/modules/Select.tsx +163 -0
  32. package/src/modules/Textarea.tsx +33 -0
  33. package/src/modules/TimeInput.helpers.ts +73 -0
  34. package/src/modules/TimeInput.tsx +159 -0
  35. package/src/modules/TimeInput.types.ts +43 -0
  36. package/src/modules/UploadInput.tsx +141 -0
  37. package/src/modules/composeFormInput.tsx +88 -0
  38. package/src/types/form.ts +65 -0
  39. package/src/ui/ContentEditable.tsx +70 -0
  40. package/src/ui/Form.types.ts +138 -0
  41. package/src/ui/FormOptions.tsx +125 -0
  42. package/src/ui/FormUI.tsx +237 -0
  43. package/src/ui/Input.helpers.ts +7 -0
  44. package/src/ui/Input.tsx +199 -0
  45. package/src/ui/Input.types.ts +48 -0
  46. package/src/ui/InputButtons.tsx +41 -0
  47. package/src/ui/InputControls.tsx +68 -0
  48. package/src/ui/Label.tsx +46 -0
  49. package/src/ui/LineForm.tsx +34 -0
  50. package/src/ui/SMSCodeInput.tsx +51 -0
  51. package/src/ui/Select.tsx +33 -0
  52. package/src/ui/Textarea.tsx +96 -0
  53. package/src/ui/TimeInput.tsx +140 -0
  54. package/tests/controller.test.tsx +336 -0
  55. package/tests/controls.test.tsx +261 -0
  56. package/tests/schema-form.test.tsx +303 -0
  57. package/tests/settings-definitions.test.ts +129 -0
  58. package/tests/sms-input.test.tsx +131 -0
  59. package/tests/time-input.test.tsx +395 -0
  60. package/tests/types.ts +102 -0
  61. package/tests/upload-input.test.tsx +114 -0
  62. package/tests/validation.test.ts +65 -0
  63. package/tsconfig.json +13 -0
  64. package/vitest.config.ts +8 -0
@@ -0,0 +1,159 @@
1
+ import { getObject } from '@jsb188/app/utils/object.ts';
2
+ import { cn } from '@jsb188/app/utils/string.ts';
3
+ import { usePopOverActions, usePopOverEvent, usePopOverIsActive } from '@jsb188/react/states';
4
+ import { PopOverButton } from '@jsb188/react-web/modules/popover/PopOverButton';
5
+ import { Icon } from '@jsb188/react-web/svgs/Icon';
6
+ import { useEffect, useId, useRef, useState } from 'react';
7
+ import { TimeInputUI } from '../ui/TimeInput';
8
+ import { Label } from './FieldLabel';
9
+ import { setFormValue } from './Form.helpers';
10
+ import { getTimeFormValues, getTimeSourceKey, resolveTimeInput } from './TimeInput.helpers';
11
+ import type { InputTimeProps, TimeFormErrors, TimeFormField, TimeFormValues } from './TimeInput.types';
12
+
13
+ export type { InputTimeProps, TimeFormField, TimeFormValues } from './TimeInput.types';
14
+
15
+ /*
16
+ * Own the time draft and bundled AM/PM popover while committing valid instants in the requested timezone.
17
+ */
18
+ export const InputTimeFromDate = (p: InputTimeProps) => {
19
+ const { name, timeZone, resetKey, formValues, setFormValues, onChange, disabled, locked, scrollAreaDOMId, borderRadiusClassName } =
20
+ p;
21
+ const value = formValues ? getObject(formValues, name) : p.value;
22
+ const sourceKey = getTimeSourceKey(value);
23
+ const instanceId = useId().replaceAll(':', '_');
24
+ const popOverId = `${name}_${instanceId}_ampm`;
25
+ const popOverEvent = usePopOverEvent({ popOverId });
26
+ const isPopOverActive = usePopOverIsActive(popOverId);
27
+ const containerRef = useRef<HTMLDivElement>(null);
28
+ const { acknowledgePopOverEvent, closePopOver } = usePopOverActions();
29
+ const isDisabled = !!(disabled || locked);
30
+ const [draft, setDraft] = useState(() => getTimeFormValues(value, timeZone));
31
+ const [errors, setErrors] = useState<TimeFormErrors>({});
32
+ const draftRef = useRef(draft);
33
+ const pendingCommit = useRef<string | number | null>(null);
34
+ const previousSource = useRef({ name, timeZone, resetKey });
35
+
36
+ /* Keep event handlers current even when multiple native events arrive before React renders. */
37
+ const updateDraft = (next: TimeFormValues) => {
38
+ draftRef.current = next;
39
+ setDraft(next);
40
+ };
41
+
42
+ useEffect(() => {
43
+ const isOwnChange =
44
+ pendingCommit.current === sourceKey &&
45
+ previousSource.current.name === name &&
46
+ previousSource.current.timeZone === timeZone &&
47
+ previousSource.current.resetKey === resetKey;
48
+ pendingCommit.current = null;
49
+ previousSource.current = { name, timeZone, resetKey };
50
+ if (isOwnChange) return;
51
+ updateDraft(getTimeFormValues(value, timeZone));
52
+ setErrors({});
53
+ }, [sourceKey, name, timeZone, resetKey]);
54
+
55
+ useEffect(() => {
56
+ if (isDisabled) closePopOver({ id: popOverId });
57
+ }, [isDisabled, popOverId]);
58
+
59
+ /* Keep partial or invalid edits local, committing only a complete, existent wall time. */
60
+ const onChangeTime = (event: React.ChangeEvent<HTMLInputElement>, field: TimeFormField) => {
61
+ if (isDisabled) return;
62
+ const next = { ...draftRef.current, [field]: event.target.value };
63
+ updateDraft(next);
64
+ const result = resolveTimeInput({ value, values: next, timeZone, fallbackDate: new Date() });
65
+ setErrors(result.status === 'invalid' ? result.errors : {});
66
+ if (result.status !== 'valid') return;
67
+ pendingCommit.current = result.value.getTime();
68
+ setFormValues?.((current) => setFormValue(current, name, result.value));
69
+ onChange?.(event, name, result.value);
70
+ };
71
+
72
+ /* Treat both numeric inputs, the period trigger, and its active portal as one editing interaction. */
73
+ const retainsCompositeFocus = (target: EventTarget | null) => {
74
+ const element = target as Element | null;
75
+ if (element?.nodeType && containerRef.current?.contains(element)) return true;
76
+ return isPopOverActive && (!element || (element.nodeType === 1 && !!element.closest('.popover')));
77
+ };
78
+
79
+ /* Restore abandoned drafts only when focus leaves the complete time control. */
80
+ const onCompositeBlur = (event: React.FocusEvent<HTMLDivElement>) => {
81
+ if (retainsCompositeFocus(event.relatedTarget)) return;
82
+ const result = resolveTimeInput({ value, values: draftRef.current, timeZone, fallbackDate: new Date() });
83
+ if (result.status !== 'valid') {
84
+ updateDraft(getTimeFormValues(value, timeZone));
85
+ setErrors({});
86
+ }
87
+ };
88
+
89
+ /* Keep the existing input blur event contract while excluding internal focus transitions. */
90
+ const onBlur = (event: React.FocusEvent<HTMLInputElement>) => {
91
+ if (!retainsCompositeFocus(event.relatedTarget)) p.onBlur?.(event);
92
+ };
93
+
94
+ useEffect(() => {
95
+ if (popOverEvent?.action !== 'ITEM') return;
96
+ acknowledgePopOverEvent(popOverEvent.eventId);
97
+ if (!isDisabled && (popOverEvent.value === 'AM' || popOverEvent.value === 'PM')) {
98
+ onChangeTime({ target: { value: popOverEvent.value } } as React.ChangeEvent<HTMLInputElement>, 'AMPM');
99
+ }
100
+ closePopOver({ id: popOverId });
101
+ }, [popOverEvent]);
102
+
103
+ const periodControl = (
104
+ <PopOverButton
105
+ id={popOverId}
106
+ scrollAreaDOMId={scrollAreaDOMId}
107
+ className={cn(
108
+ 'form_input h_spread pl_xs fs ml_xs',
109
+ borderRadiusClassName ?? 'r_sm',
110
+ // Use .cl_md for faded color for "disabled" status,
111
+ // but we're combining "Edit" and "Create" forms together now,
112
+ // So I prefer if everything is not-faded.
113
+ // disabled ? 'disabled cl_md' : '',
114
+ disabled ? 'disabled' : '',
115
+ )}
116
+ zClassName="z9"
117
+ position="bottom_left"
118
+ offsetX={0}
119
+ offsetY={7}
120
+ disabled={isDisabled}
121
+ iface={{
122
+ name: 'PO_LIST',
123
+ variables: {
124
+ designClassName: 'w_60',
125
+ options: [
126
+ {
127
+ __type: 'LIST_ITEM' as const,
128
+ text: 'AM',
129
+ value: 'AM',
130
+ },
131
+ {
132
+ __type: 'LIST_ITEM' as const,
133
+ text: 'PM',
134
+ value: 'PM',
135
+ },
136
+ ],
137
+ },
138
+ }}
139
+ >
140
+ <span>{draft.AMPM || 'AM'}</span>
141
+ <span className="cl_lt form_el_r pr_6">
142
+ <Icon name="caret-down" />
143
+ </span>
144
+ </PopOverButton>
145
+ );
146
+ return (
147
+ <TimeInputUI
148
+ {...p}
149
+ containerRef={containerRef}
150
+ onCompositeBlur={onCompositeBlur}
151
+ timeFormValues={draft}
152
+ timeErrors={errors}
153
+ onChangeTime={onChangeTime}
154
+ onBlur={onBlur}
155
+ periodControl={periodControl}
156
+ labelComponent={Label}
157
+ />
158
+ );
159
+ };
@@ -0,0 +1,43 @@
1
+ import type { FormValuesObj, FormValueSetter } from '@jsb188/form/types/form';
2
+ import type { InputFocusStyle, InputPresetName, LabelType } from '../ui/FormUI';
3
+
4
+ export type TimeFormField = 'hours' | 'minutes' | 'AMPM';
5
+ export type TimeFormValues = Record<TimeFormField, string>;
6
+ export type TimeFormErrors = Partial<Record<TimeFormField, boolean>>;
7
+ export type TimeInputValue = string | Date | null | undefined;
8
+ export type TimeEditResult =
9
+ | { status: 'valid'; value: Date }
10
+ | { status: 'incomplete' }
11
+ | { status: 'invalid'; reason: 'fields' | 'date' | 'zone' | 'gap'; errors: TimeFormErrors };
12
+
13
+ export interface InputTimeProps extends Omit<LabelType, 'children'> {
14
+ id?: string;
15
+ scrollAreaDOMId?: string;
16
+ timeZone?: string | null;
17
+ alertCount?: number;
18
+ autoFocus?: boolean;
19
+ spellCheck?: boolean;
20
+ name: string;
21
+ value?: TimeInputValue;
22
+ // Advance this key when resetting an incomplete edit to the same underlying instant.
23
+ resetKey?: string | number;
24
+ formValues?: FormValuesObj;
25
+ setFormValues?: FormValueSetter<FormValuesObj>;
26
+ allowClearIfLocked?: boolean;
27
+ locked?: boolean;
28
+ disabled?: boolean;
29
+ focused?: boolean;
30
+ error?: boolean;
31
+ fullWidth?: boolean;
32
+ className?: string;
33
+ inputClassName?: string;
34
+ borderRadiusClassName?: string;
35
+ preset?: InputPresetName;
36
+ focusStyle?: InputFocusStyle;
37
+ onChange?: (e: React.ChangeEvent<HTMLInputElement>, name: string, value: Date) => void;
38
+ onKeyDown?: (e: React.KeyboardEvent, name: string, field: TimeFormField) => void;
39
+ onSubmit?: (e: React.MouseEvent, name: string) => void;
40
+ onBlur?: React.FocusEventHandler<HTMLInputElement>;
41
+ label?: string;
42
+ children?: React.ReactNode;
43
+ }
@@ -0,0 +1,141 @@
1
+ import { forwardRef, useImperativeHandle, useRef, type ChangeEvent } from 'react';
2
+
3
+ /**
4
+ * Imperative handlers exposed by UploadInput.
5
+ */
6
+
7
+ export interface UploadInputRef {
8
+ click: () => void;
9
+ upload: (options?: { doNotClearInput?: boolean }) => Promise<boolean>;
10
+ startUpload: (options?: { doNotClearInput?: boolean }) => Promise<boolean>;
11
+ clearPickedFile: () => void;
12
+ }
13
+
14
+ /**
15
+ * Hidden file input component that exposes a click handler via ref.
16
+ */
17
+
18
+ export interface UploadInputProps {
19
+ disabled?: boolean;
20
+ acceptedFileTypes?: string;
21
+ initiateUploadOnPick?: boolean;
22
+ onChangeFile?: (file: UploadInputFileObj | null) => void;
23
+ // A truthy result indicates success; storage-specific result types stay with the caller.
24
+ onUpload: (file: File) => Promise<unknown>;
25
+ }
26
+
27
+ /**
28
+ * File details emitted when the selected file changes.
29
+ */
30
+
31
+ export interface UploadInputFileObj {
32
+ name: string;
33
+ size: number;
34
+ contentType: string;
35
+ lastModified: number;
36
+ file: File;
37
+ }
38
+
39
+ /*
40
+ * Select a file and expose picker, clearing, and delegated upload controls.
41
+ */
42
+ export const UploadInput = forwardRef<UploadInputRef, UploadInputProps>((p, ref) => {
43
+ const { disabled, acceptedFileTypes, initiateUploadOnPick = false, onChangeFile, onUpload } = p;
44
+
45
+ const fileInputRef = useRef<HTMLInputElement>(null);
46
+ const pickedFileRef = useRef<File | null>(null);
47
+
48
+ /**
49
+ * Emit selected-file details to external listeners.
50
+ */
51
+
52
+ const emitChangeFile = (file: File | null) => {
53
+ if (file) {
54
+ onChangeFile?.({
55
+ name: file.name,
56
+ size: file.size,
57
+ contentType: file.type,
58
+ lastModified: file.lastModified,
59
+ file,
60
+ });
61
+ return;
62
+ }
63
+
64
+ onChangeFile?.(null);
65
+ };
66
+
67
+ /**
68
+ * Clear selected file from local state and notify listeners.
69
+ */
70
+
71
+ const clearPickedFile = () => {
72
+ pickedFileRef.current = null;
73
+ emitChangeFile(null);
74
+
75
+ if (fileInputRef.current) {
76
+ fileInputRef.current.value = '';
77
+ }
78
+ };
79
+
80
+ /**
81
+ * Start upload for the currently selected file.
82
+ */
83
+
84
+ const startPickedFileUpload = async (options?: { doNotClearInput?: boolean }): Promise<boolean> => {
85
+ const pickedFile = pickedFileRef.current;
86
+ if (!pickedFile) {
87
+ return false;
88
+ }
89
+
90
+ if (!options?.doNotClearInput) {
91
+ clearPickedFile();
92
+ }
93
+
94
+ return !!(await onUpload(pickedFile));
95
+ };
96
+
97
+ /**
98
+ * Handle native file selection and optionally start upload.
99
+ */
100
+
101
+ const handleChangeFile = (e: ChangeEvent<HTMLInputElement>) => {
102
+ const file = e.target.files?.[0];
103
+ pickedFileRef.current = file || null;
104
+ emitChangeFile(file || null);
105
+
106
+ if (file && initiateUploadOnPick) {
107
+ void startPickedFileUpload();
108
+ }
109
+
110
+ // Allow selecting the same file again in future picks.
111
+ e.target.value = '';
112
+ };
113
+
114
+ useImperativeHandle(
115
+ ref,
116
+ () => ({
117
+ click: () => {
118
+ if (disabled) {
119
+ return;
120
+ }
121
+ fileInputRef.current?.click();
122
+ },
123
+ upload: startPickedFileUpload,
124
+ startUpload: startPickedFileUpload,
125
+ clearPickedFile,
126
+ }),
127
+ [disabled, startPickedFileUpload],
128
+ );
129
+
130
+ return (
131
+ <input
132
+ ref={fileInputRef}
133
+ type="file"
134
+ accept={acceptedFileTypes}
135
+ className="hidden"
136
+ onChange={handleChangeFile}
137
+ />
138
+ );
139
+ });
140
+
141
+ UploadInput.displayName = 'UploadInput';
@@ -0,0 +1,88 @@
1
+ import { getPlatform } from '@jsb188/app/platform.ts';
2
+ import type { FormValueSetter, FormValuesObj } from '@jsb188/form/types/form';
3
+ import { getObject } from '@jsb188/app/utils/object.ts';
4
+ import type { InputType } from '../ui/Form.types';
5
+ import { clearFieldError, setFormValue } from './Form.helpers';
6
+
7
+ /**
8
+ * Platform agnostic composer for form input components
9
+ */
10
+
11
+ export interface FormInputProps extends InputType {
12
+ name: string;
13
+ formValues: FormValuesObj;
14
+ setFormValues: FormValueSetter<FormValuesObj>;
15
+ // Used when the form needs to listen to changes.
16
+ listenToInput?: boolean;
17
+ onChangeText?: (value: string, name: string) => void;
18
+ setter?: (value: unknown) => unknown; // Used to transform the value before setting it in formValues
19
+ }
20
+
21
+ export function composeFormInput(Component: React.FC<any>, isTextarea?: boolean) {
22
+ return function FormInputCmp(p: FormInputProps) {
23
+ const { name, listenToInput, onInput, onChange, onChangeText, onSubmit, formValues, setFormValues, setter } = p;
24
+
25
+ const platform = getPlatform();
26
+ const isError = formValues.__errorFields?.includes(name);
27
+
28
+ let onKeyDown: FormInputProps['onKeyDown'];
29
+ if (platform !== 'MOBILE' && (onSubmit || p.onKeyDown)) {
30
+ onKeyDown = (e: React.KeyboardEvent, name_: string) => {
31
+ if (p.onKeyDown) {
32
+ p.onKeyDown(e, name_);
33
+ }
34
+
35
+ if (onSubmit && !isTextarea && e.key === 'Enter') {
36
+ e.preventDefault();
37
+ // @ts-expect-error - allow both click and keyboard events
38
+ onSubmit(e, name_);
39
+ }
40
+ };
41
+ }
42
+
43
+ let platformAgnosticProps: Pick<Partial<FormInputProps>, 'onKeyDown' | 'onInput' | 'onChange' | 'onChangeText'>;
44
+ if (listenToInput) {
45
+ // Preserve native input-event binding for callers that explicitly request it.
46
+ platformAgnosticProps = {
47
+ onKeyDown,
48
+ onInput: (e: React.FormEvent, name_: string) => {
49
+ if (onInput) {
50
+ onInput(e, name_);
51
+ }
52
+ const value = platform === 'WEB' ? (e.target as HTMLInputElement)?.value : e;
53
+ setFormValues((current) => setFormValue(current, String(name_), setter ? setter(value) : value));
54
+ },
55
+ };
56
+ } else if (platform === 'MOBILE') {
57
+ platformAgnosticProps = {
58
+ onChangeText: (value: string, name_: string) => {
59
+ if (onChangeText) {
60
+ onChangeText(value, name_);
61
+ }
62
+ setFormValues((current) => setFormValue(current, String(name_), setter ? setter(value) : value));
63
+ },
64
+ };
65
+ } else {
66
+ platformAgnosticProps = {
67
+ onKeyDown,
68
+ onChange: (e: React.ChangeEvent<HTMLInputElement>, name_: string) => {
69
+ if (onChange) {
70
+ onChange(e, name_);
71
+ }
72
+
73
+ const value = setter ? setter(e.target?.value) : e.target?.value;
74
+ setFormValues((current) => clearFieldError(setFormValue(current, String(name_), value), name));
75
+ },
76
+ };
77
+ }
78
+
79
+ return (
80
+ <Component
81
+ {...p}
82
+ {...platformAgnosticProps}
83
+ value={getObject(formValues, name)}
84
+ error={isError}
85
+ />
86
+ );
87
+ };
88
+ }
@@ -0,0 +1,65 @@
1
+ import type { ChangeEvent, FormEvent, KeyboardEvent, MouseEvent } from 'react';
2
+
3
+ /**
4
+ * Types; form schema
5
+ */
6
+
7
+ export type FormValuesObj = {
8
+ [key: string]: any;
9
+ __errorFields?: string[];
10
+ };
11
+
12
+ export type FormValueSetter<TFormValues extends FormValuesObj = any> = (
13
+ values: TFormValues | ((prev: TFormValues) => TFormValues),
14
+ ) => void;
15
+
16
+ export type FormItemType =
17
+ | 'subtitle'
18
+ | 'break'
19
+ | 'group'
20
+ | 'input'
21
+ | 'section_title'
22
+ | 'input_click'
23
+ | 'input_w_button'
24
+ | 'input_time_from_date'
25
+ | 'password'
26
+ | 'textarea'
27
+ | 'none'
28
+ | (string & {});
29
+
30
+ interface SchemaItemAny<TCurrentData = any, TDataForSchema = any> {
31
+ [key: string]: any;
32
+ __typename?: string;
33
+ setter?: (currentData?: TCurrentData, dataForSchema?: TDataForSchema) => any;
34
+ }
35
+
36
+ export interface FormItemSchemaObj<TCurrentData = any, TDataForSchema = any> {
37
+ __type: FormItemType;
38
+ autoFocus?: boolean;
39
+ disabled?: boolean;
40
+ hidden?: boolean | ((dataForSchema: TDataForSchema) => boolean);
41
+ locked?: boolean | ((dataForSchema: TDataForSchema) => boolean);
42
+ labelClassName?: string;
43
+ listenToInput?: boolean;
44
+ value?: any;
45
+ item: SchemaItemAny<TCurrentData, TDataForSchema>;
46
+ }
47
+
48
+ /* Legacy item descriptors remain supported for metadata-driven standalone editors. */
49
+
50
+ export interface FormItemIfaceObj<TFormValues extends FormValuesObj = any, TCurrentData = any, TDataForSchema = any>
51
+ extends FormItemSchemaObj<TCurrentData, TDataForSchema> {
52
+ name: string;
53
+ preset?: string;
54
+ className?: string;
55
+ inputClassName?: string;
56
+ focusStyle?: string;
57
+ hidden?: boolean | ((dataForSchema: TDataForSchema) => boolean);
58
+ locked?: boolean | ((dataForSchema: TDataForSchema) => boolean);
59
+ formValues: TFormValues;
60
+ setFormValues: FormValueSetter<TFormValues>;
61
+ onInput?: (e: FormEvent, name: string) => void;
62
+ onKeyDown?: (e: KeyboardEvent, name: string) => void;
63
+ onSubmit?: (e: MouseEvent, name: string) => void;
64
+ onChange?: (e: ChangeEvent<HTMLInputElement | HTMLTextAreaElement>, name: string) => void;
65
+ }
@@ -0,0 +1,70 @@
1
+ import { forwardRef, memo } from 'react';
2
+
3
+ /**
4
+ * Content editable elements must never re-render because content is controlled by the browser.
5
+ * To make it easier, use this Component to prevent re-renders.
6
+ */
7
+
8
+ export const ContentEditable = memo(
9
+ forwardRef(
10
+ (
11
+ p: {
12
+ domId?: string;
13
+ role?: string;
14
+ tabIndex?: number;
15
+ className?: string;
16
+ disabled?: boolean;
17
+ htmlText: string;
18
+ placeholder?: string;
19
+ onKeyDown: (e: React.KeyboardEvent<HTMLDivElement>) => void;
20
+ onKeyUp: (e: React.KeyboardEvent<HTMLDivElement>) => void;
21
+ onInput: (e: React.FormEvent<HTMLDivElement>) => void;
22
+ onPaste: (e: React.ClipboardEvent<HTMLDivElement>) => void;
23
+ onFocus?: (e: React.FocusEvent<HTMLDivElement>) => void;
24
+ onBlur?: (e: React.FocusEvent<HTMLDivElement>) => void;
25
+ },
26
+ ref: React.ForwardedRef<HTMLDivElement>,
27
+ ) => {
28
+ const {
29
+ domId,
30
+ role,
31
+ tabIndex,
32
+ className,
33
+ disabled,
34
+ placeholder,
35
+ htmlText,
36
+ onKeyDown,
37
+ onKeyUp,
38
+ onInput,
39
+ onPaste,
40
+ onFocus,
41
+ onBlur,
42
+ } = p;
43
+
44
+ return (
45
+ <div
46
+ ref={ref}
47
+ id={domId}
48
+ className={className}
49
+ role={role ?? 'textbox'}
50
+ tabIndex={tabIndex ?? 0}
51
+ contentEditable={disabled ? 'false' : 'true'}
52
+ data-placeholder={placeholder}
53
+ onKeyDown={onKeyDown}
54
+ onKeyUp={onKeyUp}
55
+ onInput={onInput}
56
+ onPaste={onPaste}
57
+ onFocus={onFocus}
58
+ onBlur={onBlur}
59
+ dangerouslySetInnerHTML={{ __html: htmlText }}
60
+ />
61
+ );
62
+
63
+ // IMPORTANT NOTE:
64
+ // <ContentEditable /> works by making it only render when it absolutely has to.
65
+ // If you re-render every time there's a text change, there will be issues.
66
+ // If you *do* see issues, use checkPropsDiff() to figure out which prop is triggering re-render.
67
+ // }), globalThis.checkPropsDiff);
68
+ },
69
+ ),
70
+ );
@@ -0,0 +1,138 @@
1
+ import type { InputPresentationProps } from './Input.types';
2
+ import type { FormValueSetter, FormValuesObj } from '@jsb188/form/types/form';
3
+ import type { POPosition } from '@jsb188/react/types/PopOver.d';
4
+ import type { Button } from '@jsb188/react-web/ui/Button';
5
+ import type { InputFocusStyle, InputPresetName } from './FormUI';
6
+
7
+ export interface InputType extends InputPresentationProps {
8
+ formValues?: FormValuesObj;
9
+ setFormValues?: FormValueSetter<FormValuesObj>;
10
+ }
11
+
12
+ export type SelectValue = string | number | null;
13
+
14
+ export interface SelectOptionType {
15
+ value: SelectValue;
16
+ text: string;
17
+ description?: string;
18
+ iconName?: string;
19
+ className?: string;
20
+ textClassName?: string;
21
+ disabled?: boolean;
22
+ }
23
+
24
+ export interface SelectType {
25
+ ariaLabel?: string;
26
+ id?: string;
27
+ name: string;
28
+ options: SelectOptionType[];
29
+ value?: SelectValue;
30
+ label?: string;
31
+ placeholder?: string;
32
+ placeholderIconName?: string;
33
+ info?: string;
34
+ locked?: boolean;
35
+ disabled?: boolean;
36
+ editable?: boolean;
37
+ focused?: boolean;
38
+ error?: boolean;
39
+ fullWidth?: boolean;
40
+ className?: string;
41
+ selectClassName?: string;
42
+ borderRadiusClassName?: string;
43
+ preset?: InputPresetName;
44
+ focusStyle?: InputFocusStyle;
45
+ fontSizeClassName?: string;
46
+ labelClassName?: string;
47
+ labelPaddingClassName?: string;
48
+ matchTriggerWidth?: boolean;
49
+ // Added to the selected option's (or placeholder's) icon wrapper.
50
+ leftIconClassName?: string;
51
+ rightIconClassName?: string;
52
+ position?: POPosition;
53
+ // Pixel offset of the option menu from the trigger; defaults to 0 (x) and 5 (y).
54
+ xOffset?: number;
55
+ yOffset?: number;
56
+ popOverClassName?: string;
57
+ zClassName?: string;
58
+ onFocus?: React.FocusEventHandler<HTMLDivElement>;
59
+ onBlur?: React.FocusEventHandler<HTMLDivElement>;
60
+ onClick?: (e: React.MouseEvent, name: string) => void;
61
+ onChange?: (value: SelectValue, name: string, option?: SelectOptionType) => void;
62
+ children?: React.ReactNode;
63
+ }
64
+
65
+ export type OptionType = {
66
+ disabled?: boolean;
67
+ checked?: boolean;
68
+ value?: string | number | null;
69
+ text?: string;
70
+ iconName?: string;
71
+ className?: string;
72
+ hideCheckIcon?: boolean;
73
+ rightLabel?: string;
74
+ rightLabelColor?: string;
75
+ name: string;
76
+ index: number;
77
+ onChange: (e: React.MouseEvent, name: string, value: string | number | null) => void;
78
+ };
79
+
80
+ export type OptionsType = {
81
+ id?: string;
82
+ name: string;
83
+ value?: string | number | null;
84
+ options: Partial<OptionType>[];
85
+ className?: string;
86
+ label?: string;
87
+ labelClassName?: string;
88
+ info?: string;
89
+ disabled?: boolean;
90
+ locked?: boolean;
91
+ onChange: (e: React.MouseEvent, name: string, value: string | number | null) => void;
92
+ };
93
+
94
+ export type TextareaType = {
95
+ id?: string;
96
+ 'aria-label'?: string;
97
+ autoFocus?: boolean;
98
+ autoComplete?: 'on' | 'off' | string;
99
+ spellCheck?: boolean;
100
+ minHeight?: number;
101
+ maxLength?: number;
102
+ focusStyle?: InputFocusStyle;
103
+ name: string;
104
+ formValues?: FormValuesObj;
105
+ setFormValues?: FormValueSetter<FormValuesObj>;
106
+ allowClearIfLocked?: boolean;
107
+ locked?: boolean;
108
+ disabled?: boolean;
109
+ error?: boolean;
110
+ fullWidth?: boolean;
111
+ className?: string;
112
+ borderRadiusClassName?: string;
113
+ textareaClassName?: string;
114
+ preset?: InputPresetName;
115
+ onFocus?: React.FocusEventHandler<HTMLTextAreaElement>;
116
+ onBlur?: React.FocusEventHandler<HTMLTextAreaElement>;
117
+ onChange?: (e: React.ChangeEvent<HTMLTextAreaElement>, name: string) => void;
118
+ onInput?: (e: React.FormEvent, name: string) => void;
119
+ onKeyDown?: (e: React.KeyboardEvent, name: string) => void;
120
+ onSubmit?: (e: React.MouseEvent, name: string) => void;
121
+ value?: string;
122
+ label?: string;
123
+ placeholder?: string;
124
+ };
125
+
126
+ export type SideInputButtonProps = {
127
+ href?: string;
128
+ className?: string;
129
+ iconName?: string;
130
+ alertCount?: number;
131
+ text?: string;
132
+ offsetRight?: boolean;
133
+ disabled?: boolean;
134
+ selected?: boolean;
135
+ onClick?: React.MouseEventHandler<HTMLAnchorElement>;
136
+ };
137
+
138
+ export type InputRightButtonProps = React.ComponentProps<typeof Button>;