@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.
- package/README.md +132 -2
- package/css/form.css +2 -0
- package/package.json +40 -5
- package/src/assets.d.ts +1 -0
- package/src/libs/SchemaForm.tsx +158 -0
- package/src/libs/SchemaForm.types.ts +165 -0
- package/src/libs/defineForm.ts +11 -0
- package/src/libs/rules.ts +126 -0
- package/src/libs/useField.ts +16 -0
- package/src/libs/useSchemaForm.helpers.ts +71 -0
- package/src/libs/useSchemaForm.ts +268 -0
- package/src/libs/validation.ts +42 -0
- package/src/main.ts +40 -0
- package/src/modules/FieldLabel.tsx +28 -0
- package/src/modules/Form-Input.tsx +2 -0
- package/src/modules/Form-Line.tsx +126 -0
- package/src/modules/Form.helpers.ts +12 -0
- package/src/modules/Form.tsx +14 -0
- package/src/modules/FormItem.tsx +117 -0
- package/src/modules/FormOptions.tsx +12 -0
- package/src/modules/FormUI.tsx +2 -0
- package/src/modules/Input.tsx +16 -0
- package/src/modules/InputClick.tsx +48 -0
- package/src/modules/InputWithButton.tsx +37 -0
- package/src/modules/ListItemForm.tsx +345 -0
- package/src/modules/ListItemForm.types.ts +50 -0
- package/src/modules/PasswordInput.tsx +36 -0
- package/src/modules/PopOverInput.tsx +75 -0
- package/src/modules/SMSCodeInput.tsx +118 -0
- package/src/modules/SchemaField.tsx +104 -0
- package/src/modules/Select.tsx +163 -0
- package/src/modules/Textarea.tsx +33 -0
- package/src/modules/TimeInput.helpers.ts +73 -0
- package/src/modules/TimeInput.tsx +159 -0
- package/src/modules/TimeInput.types.ts +43 -0
- package/src/modules/UploadInput.tsx +141 -0
- package/src/modules/composeFormInput.tsx +88 -0
- package/src/types/form.ts +65 -0
- package/src/ui/ContentEditable.tsx +70 -0
- package/src/ui/Form.types.ts +138 -0
- package/src/ui/FormOptions.tsx +125 -0
- package/src/ui/FormUI.tsx +237 -0
- package/src/ui/Input.helpers.ts +7 -0
- package/src/ui/Input.tsx +199 -0
- package/src/ui/Input.types.ts +48 -0
- package/src/ui/InputButtons.tsx +41 -0
- package/src/ui/InputControls.tsx +68 -0
- package/src/ui/Label.tsx +46 -0
- package/src/ui/LineForm.tsx +34 -0
- package/src/ui/SMSCodeInput.tsx +51 -0
- package/src/ui/Select.tsx +33 -0
- package/src/ui/Textarea.tsx +96 -0
- package/src/ui/TimeInput.tsx +140 -0
- package/tests/controller.test.tsx +336 -0
- package/tests/controls.test.tsx +261 -0
- package/tests/schema-form.test.tsx +303 -0
- package/tests/settings-definitions.test.ts +129 -0
- package/tests/sms-input.test.tsx +131 -0
- package/tests/time-input.test.tsx +395 -0
- package/tests/types.ts +102 -0
- package/tests/upload-input.test.tsx +114 -0
- package/tests/validation.test.ts +65 -0
- package/tsconfig.json +13 -0
- package/vitest.config.ts +8 -0
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
import type { FormItemIfaceObj } from '@jsb188/form/types/form';
|
|
2
|
+
import { FormSectionTitle, getHtmlFor } from '../ui/FormUI';
|
|
3
|
+
import { Label } from './FieldLabel';
|
|
4
|
+
import { composeFormInput } from './composeFormInput';
|
|
5
|
+
import { Input } from './Input';
|
|
6
|
+
import { InputWithButton } from './InputWithButton';
|
|
7
|
+
import { InputClick } from './InputClick';
|
|
8
|
+
import { InputTimeFromDate } from './TimeInput';
|
|
9
|
+
import { PasswordInput } from './PasswordInput';
|
|
10
|
+
import { Textarea } from './Textarea';
|
|
11
|
+
|
|
12
|
+
const FVInput = composeFormInput(Input);
|
|
13
|
+
const FVPasswordInput = composeFormInput(PasswordInput);
|
|
14
|
+
const FVTextarea = composeFormInput(Textarea, true);
|
|
15
|
+
|
|
16
|
+
/*
|
|
17
|
+
* Adapt the resolved metadata descriptors used by standalone log editors to the shared controls.
|
|
18
|
+
* Descriptors must already contain resolved presentation values; the new schema renderer uses SchemaField instead.
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
export function FormItem(p: any) {
|
|
22
|
+
const { __type, item, disabled, ...other } = p;
|
|
23
|
+
|
|
24
|
+
switch (__type) {
|
|
25
|
+
case 'subtitle': {
|
|
26
|
+
return <strong className="bl ft_md mt_df pt_xs cl_lt">{item.text}</strong>;
|
|
27
|
+
}
|
|
28
|
+
case 'break': {
|
|
29
|
+
// return <div className='h_6 bd_t_1 bd_lt mt_12' />;
|
|
30
|
+
|
|
31
|
+
// This is tuned for condensed preset
|
|
32
|
+
return <div className="h_6 rel texture_bf pattern_texture mt_16 mb_20" />;
|
|
33
|
+
}
|
|
34
|
+
case 'group': {
|
|
35
|
+
const { label, items } = item;
|
|
36
|
+
return (
|
|
37
|
+
<div className={`grid gapx_xs size_${items.length}`}>
|
|
38
|
+
{label ? (
|
|
39
|
+
<Label
|
|
40
|
+
htmlFor={getHtmlFor(items?.[0]?.id, items?.[0]?.name)}
|
|
41
|
+
labelClassName={other.labelClassName}
|
|
42
|
+
>
|
|
43
|
+
{label}
|
|
44
|
+
</Label>
|
|
45
|
+
) : null}
|
|
46
|
+
{items.map((subItem: FormItemIfaceObj, i: number) => (
|
|
47
|
+
<FormItem
|
|
48
|
+
key={i}
|
|
49
|
+
disabled={disabled}
|
|
50
|
+
{...subItem}
|
|
51
|
+
{...other}
|
|
52
|
+
/>
|
|
53
|
+
))}
|
|
54
|
+
</div>
|
|
55
|
+
);
|
|
56
|
+
}
|
|
57
|
+
case 'input':
|
|
58
|
+
return (
|
|
59
|
+
<FVInput
|
|
60
|
+
disabled={disabled}
|
|
61
|
+
{...item}
|
|
62
|
+
{...other}
|
|
63
|
+
/>
|
|
64
|
+
);
|
|
65
|
+
case 'input_w_button':
|
|
66
|
+
return (
|
|
67
|
+
<InputWithButton
|
|
68
|
+
disabled={disabled}
|
|
69
|
+
{...item}
|
|
70
|
+
{...other}
|
|
71
|
+
/>
|
|
72
|
+
);
|
|
73
|
+
case 'input_click':
|
|
74
|
+
return (
|
|
75
|
+
<InputClick
|
|
76
|
+
disabled={disabled}
|
|
77
|
+
{...item}
|
|
78
|
+
{...other}
|
|
79
|
+
/>
|
|
80
|
+
);
|
|
81
|
+
case 'input_time_from_date':
|
|
82
|
+
return (
|
|
83
|
+
<InputTimeFromDate
|
|
84
|
+
disabled={disabled}
|
|
85
|
+
{...item}
|
|
86
|
+
{...other}
|
|
87
|
+
/>
|
|
88
|
+
);
|
|
89
|
+
case 'section_title':
|
|
90
|
+
return <FormSectionTitle {...item} />;
|
|
91
|
+
case 'password':
|
|
92
|
+
return (
|
|
93
|
+
<FVPasswordInput
|
|
94
|
+
disabled={disabled}
|
|
95
|
+
{...item}
|
|
96
|
+
{...other}
|
|
97
|
+
/>
|
|
98
|
+
);
|
|
99
|
+
case 'textarea':
|
|
100
|
+
return (
|
|
101
|
+
<FVTextarea
|
|
102
|
+
disabled={disabled}
|
|
103
|
+
{...item}
|
|
104
|
+
{...other}
|
|
105
|
+
/>
|
|
106
|
+
);
|
|
107
|
+
case 'none':
|
|
108
|
+
// I'm using this iface as a custom element switch
|
|
109
|
+
return null;
|
|
110
|
+
default:
|
|
111
|
+
console.warn('Unknown schema form __type:', __type);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
return null;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
FormItem.displayName = 'FormItem';
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { FormOptionsUI } from '../ui/FormOptions';
|
|
2
|
+
import { Label } from './FieldLabel';
|
|
3
|
+
|
|
4
|
+
/*
|
|
5
|
+
* Add the bundled label tooltip to a controlled option group.
|
|
6
|
+
*/
|
|
7
|
+
export const FormOptions = (p: React.ComponentProps<typeof FormOptionsUI>) => (
|
|
8
|
+
<FormOptionsUI
|
|
9
|
+
{...p}
|
|
10
|
+
labelComponent={Label}
|
|
11
|
+
/>
|
|
12
|
+
);
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { InputUI } from '../ui/Input';
|
|
2
|
+
import { Label } from './FieldLabel';
|
|
3
|
+
import { setFormValue } from './Form.helpers';
|
|
4
|
+
|
|
5
|
+
/*
|
|
6
|
+
* Keep the existing input presentation and help tooltip, with an optional legacy locked-value reset.
|
|
7
|
+
*/
|
|
8
|
+
export const Input = (p: React.ComponentProps<typeof InputUI>) => (
|
|
9
|
+
<InputUI
|
|
10
|
+
{...p}
|
|
11
|
+
labelComponent={Label}
|
|
12
|
+
onClearLocked={
|
|
13
|
+
p.onClearLocked || (p.setFormValues ? () => p.setFormValues!((current) => setFormValue(current, String(p.name), '')) : undefined)
|
|
14
|
+
}
|
|
15
|
+
/>
|
|
16
|
+
);
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { getObject } from '@jsb188/app/utils/object.ts';
|
|
2
|
+
import { cn } from '@jsb188/app/utils/string.ts';
|
|
3
|
+
import { PopOverButton } from '@jsb188/react-web/modules/popover/PopOverButton';
|
|
4
|
+
import { useEffect } from 'react';
|
|
5
|
+
import type { InputType } from '../ui/Form.types';
|
|
6
|
+
import type { LabelType } from '../ui/Label';
|
|
7
|
+
import { Input } from './Input';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Input but only allow clicks
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
export function InputClick(p: Partial<InputType> & Omit<LabelType, 'children'> & { popOverProps?: any }) {
|
|
14
|
+
const { formValues, setFormValues, name, className, inputClassName, disabled, locked } = p;
|
|
15
|
+
const { popOverProps, ...rest } = p;
|
|
16
|
+
const isErrored = p.error || (name ? formValues?.__errorFields?.includes(name) : false);
|
|
17
|
+
const isDisabled = disabled || locked;
|
|
18
|
+
const value = formValues && name ? getObject(formValues, name) : p.value;
|
|
19
|
+
|
|
20
|
+
useEffect(() => {
|
|
21
|
+
if (isErrored && value) {
|
|
22
|
+
setFormValues?.((current) => ({ ...current, __errorFields: (current.__errorFields || []).filter((field) => field !== name) }));
|
|
23
|
+
}
|
|
24
|
+
}, [isErrored, value]);
|
|
25
|
+
|
|
26
|
+
return (
|
|
27
|
+
<Input
|
|
28
|
+
{...rest}
|
|
29
|
+
// as={popOverProps ? PopOverButton : undefined}
|
|
30
|
+
// locked
|
|
31
|
+
disabledButClickable
|
|
32
|
+
value={value}
|
|
33
|
+
inputClassName={isDisabled ? inputClassName : cn('pointer', inputClassName)}
|
|
34
|
+
rightIconName={locked ? 'locked' : 'caret-down'}
|
|
35
|
+
className={cn(className, popOverProps ? 'link' : '', isErrored ? 'error' : '')}
|
|
36
|
+
InputOverlayComponent={
|
|
37
|
+
popOverProps ? (
|
|
38
|
+
<PopOverButton
|
|
39
|
+
{...popOverProps}
|
|
40
|
+
domId={`input_click_${name}`}
|
|
41
|
+
className="abs_full"
|
|
42
|
+
disabled={isDisabled}
|
|
43
|
+
/>
|
|
44
|
+
) : undefined
|
|
45
|
+
}
|
|
46
|
+
/>
|
|
47
|
+
);
|
|
48
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { composeFormInput, type FormInputProps } from './composeFormInput';
|
|
2
|
+
import { Input } from './Input';
|
|
3
|
+
|
|
4
|
+
const FVInput = composeFormInput(Input);
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Input with button at right
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
export function InputWithButton(
|
|
11
|
+
p: Partial<FormInputProps> & {
|
|
12
|
+
buttonText?: string;
|
|
13
|
+
onClickButton: (e: React.MouseEvent) => void;
|
|
14
|
+
},
|
|
15
|
+
) {
|
|
16
|
+
const { buttonText, onClickButton, ...rest } = p;
|
|
17
|
+
const inputProps = {
|
|
18
|
+
...rest,
|
|
19
|
+
onClick: onClickButton,
|
|
20
|
+
RightIconComponent: (
|
|
21
|
+
<div className="abs_r_center mr_5">
|
|
22
|
+
<span className="btn link bg_active lh_1 ft_xs ft_medium p_xs r_sm">{buttonText}</span>
|
|
23
|
+
</div>
|
|
24
|
+
),
|
|
25
|
+
};
|
|
26
|
+
if (p.name && p.formValues && p.setFormValues) {
|
|
27
|
+
return (
|
|
28
|
+
<FVInput
|
|
29
|
+
{...inputProps}
|
|
30
|
+
name={p.name}
|
|
31
|
+
formValues={p.formValues}
|
|
32
|
+
setFormValues={p.setFormValues}
|
|
33
|
+
/>
|
|
34
|
+
);
|
|
35
|
+
}
|
|
36
|
+
return <Input {...inputProps} />;
|
|
37
|
+
}
|
|
@@ -0,0 +1,345 @@
|
|
|
1
|
+
import i18n from '@jsb188/app/i18n/index.ts';
|
|
2
|
+
import { cn } from '@jsb188/app/utils/string.ts';
|
|
3
|
+
import { COMMON_ICON_NAMES, Icon } from '@jsb188/react-web/svgs/Icon';
|
|
4
|
+
import { Button } from '@jsb188/react-web/ui/Button';
|
|
5
|
+
import { TooltipButton } from '@jsb188/react-web/modules/popover/Tooltip';
|
|
6
|
+
import { MoreScrollArea } from '@jsb188/react-web/modules/Scroll';
|
|
7
|
+
import { memo, useEffect, useId, useRef, useState, type KeyboardEvent } from 'react';
|
|
8
|
+
import { Select } from './Select';
|
|
9
|
+
import type { ListItemFormField, ListItemFormProps, ListItemFormValues } from './ListItemForm.types';
|
|
10
|
+
export type { ListItemFormField, ListItemFormProps, ListItemFormValues } from './ListItemForm.types';
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* List item form
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
/*
|
|
17
|
+
* Render one field of a list item form as a bare input or a preset-less select,
|
|
18
|
+
* both sized by the field's width class so the row scrolls when they overflow.
|
|
19
|
+
* The wrapper carries the border, the optional caption pinned to its top and
|
|
20
|
+
* the stripes of a disabled field.
|
|
21
|
+
*/
|
|
22
|
+
const ListItemFormFieldControl = memo(
|
|
23
|
+
(p: {
|
|
24
|
+
field: ListItemFormField;
|
|
25
|
+
value: string;
|
|
26
|
+
disabled: boolean;
|
|
27
|
+
fontSizeClassName: string;
|
|
28
|
+
onChange: (name: string, value: string) => void;
|
|
29
|
+
onKeyDown: (e: KeyboardEvent<HTMLInputElement>) => void;
|
|
30
|
+
}) => {
|
|
31
|
+
const { field, value, disabled, fontSizeClassName, onChange, onKeyDown } = p;
|
|
32
|
+
const id = useId();
|
|
33
|
+
const isDisabled = disabled || field.disabled;
|
|
34
|
+
// The wrapper owns the background and hover tint; the control stays transparent over it.
|
|
35
|
+
const controlClassName = cn('ol_none bd_0 h_f w_f px_8 rel', fontSizeClassName);
|
|
36
|
+
|
|
37
|
+
return (
|
|
38
|
+
<div
|
|
39
|
+
className={cn(
|
|
40
|
+
'rel no_shrink h_f r_sm bd_1 bd_lt bg bg_main_fd_hv',
|
|
41
|
+
field.label && 'pt_13',
|
|
42
|
+
field.widthClassName ?? 'w_200',
|
|
43
|
+
isDisabled && 'pattern_stripes medium_bf',
|
|
44
|
+
)}
|
|
45
|
+
>
|
|
46
|
+
{field.label ? (
|
|
47
|
+
<label
|
|
48
|
+
className="abs_tl ft_xxs lh_1 cl_md px_8 pt_2 ellip max_w_f"
|
|
49
|
+
htmlFor={id}
|
|
50
|
+
>
|
|
51
|
+
{field.label}
|
|
52
|
+
</label>
|
|
53
|
+
) : null}
|
|
54
|
+
|
|
55
|
+
{field.type === 'select' ? (
|
|
56
|
+
<Select
|
|
57
|
+
id={id}
|
|
58
|
+
name={field.name}
|
|
59
|
+
preset="none"
|
|
60
|
+
zClassName="z9"
|
|
61
|
+
className="h_f"
|
|
62
|
+
selectClassName={cn(controlClassName, 'py_n')}
|
|
63
|
+
borderRadiusClassName=""
|
|
64
|
+
options={field.options ?? []}
|
|
65
|
+
placeholder={field.placeholder}
|
|
66
|
+
value={value}
|
|
67
|
+
disabled={isDisabled}
|
|
68
|
+
matchTriggerWidth
|
|
69
|
+
onChange={(next) => onChange(field.name, next == null ? '' : String(next))}
|
|
70
|
+
/>
|
|
71
|
+
) : (
|
|
72
|
+
<input
|
|
73
|
+
className={cn(controlClassName, 'stock')}
|
|
74
|
+
style={{ backgroundColor: 'transparent' }}
|
|
75
|
+
id={id}
|
|
76
|
+
name={field.name}
|
|
77
|
+
type={field.type}
|
|
78
|
+
value={value}
|
|
79
|
+
placeholder={field.placeholder}
|
|
80
|
+
maxLength={field.maxLength}
|
|
81
|
+
disabled={isDisabled}
|
|
82
|
+
onChange={(e) => onChange(field.name, e.target.value)}
|
|
83
|
+
onKeyDown={onKeyDown}
|
|
84
|
+
/>
|
|
85
|
+
)}
|
|
86
|
+
</div>
|
|
87
|
+
);
|
|
88
|
+
},
|
|
89
|
+
);
|
|
90
|
+
|
|
91
|
+
ListItemFormFieldControl.displayName = 'ListItemFormFieldControl';
|
|
92
|
+
|
|
93
|
+
/*
|
|
94
|
+
* Render the display-mode leading icon, as a hover tooltip when it has one.
|
|
95
|
+
*/
|
|
96
|
+
const ListItemFormIcon = memo((p: { iconName: string; tooltipText?: string }) =>
|
|
97
|
+
p.tooltipText ? (
|
|
98
|
+
<TooltipButton
|
|
99
|
+
as="div"
|
|
100
|
+
ariaLabel={p.tooltipText}
|
|
101
|
+
message={p.tooltipText}
|
|
102
|
+
position="top"
|
|
103
|
+
offsetY={0}
|
|
104
|
+
className="no_shrink ic_sm cl_md"
|
|
105
|
+
>
|
|
106
|
+
<Icon name={p.iconName} />
|
|
107
|
+
</TooltipButton>
|
|
108
|
+
) : (
|
|
109
|
+
<span className="no_shrink ic_sm cl_md">
|
|
110
|
+
<Icon name={p.iconName} />
|
|
111
|
+
</span>
|
|
112
|
+
),
|
|
113
|
+
);
|
|
114
|
+
|
|
115
|
+
ListItemFormIcon.displayName = 'ListItemFormIcon';
|
|
116
|
+
|
|
117
|
+
/*
|
|
118
|
+
* Render the display-mode actions of a list item form: the Delete (when
|
|
119
|
+
* deletable) and Edit icons, or the Cancel/Confirm pair while a delete
|
|
120
|
+
* awaits confirmation.
|
|
121
|
+
*/
|
|
122
|
+
const ListItemFormActions = memo(
|
|
123
|
+
(p: {
|
|
124
|
+
buttonClassName: string;
|
|
125
|
+
confirmingDelete: boolean;
|
|
126
|
+
deleteText?: string;
|
|
127
|
+
editText?: string;
|
|
128
|
+
inactive: boolean;
|
|
129
|
+
loading?: boolean;
|
|
130
|
+
onClickDelete?: () => void;
|
|
131
|
+
onClickEdit: () => void;
|
|
132
|
+
onToggleConfirmDelete: (confirming: boolean) => void;
|
|
133
|
+
}) => {
|
|
134
|
+
const { buttonClassName, inactive, onClickDelete } = p;
|
|
135
|
+
|
|
136
|
+
if (p.confirmingDelete && onClickDelete) {
|
|
137
|
+
return (
|
|
138
|
+
<span className="no_shrink h_item gap_5">
|
|
139
|
+
<Button
|
|
140
|
+
size="xs"
|
|
141
|
+
preset="outline_subtle"
|
|
142
|
+
className={buttonClassName}
|
|
143
|
+
text={i18n.t('c.cancel')}
|
|
144
|
+
disabled={inactive}
|
|
145
|
+
onClick={() => p.onToggleConfirmDelete(false)}
|
|
146
|
+
/>
|
|
147
|
+
<Button
|
|
148
|
+
size="xs"
|
|
149
|
+
preset="outline_subtle"
|
|
150
|
+
className={cn('cl_err bg_err_fd', buttonClassName)}
|
|
151
|
+
text={i18n.t('c.confirm')}
|
|
152
|
+
disabled={inactive}
|
|
153
|
+
loading={p.loading}
|
|
154
|
+
onClick={onClickDelete}
|
|
155
|
+
/>
|
|
156
|
+
</span>
|
|
157
|
+
);
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
return (
|
|
161
|
+
<span className="no_shrink h_item">
|
|
162
|
+
{onClickDelete ? (
|
|
163
|
+
<TooltipButton
|
|
164
|
+
ariaLabel={p.deleteText ?? i18n.t('c.remove')}
|
|
165
|
+
message={p.deleteText ?? i18n.t('c.remove')}
|
|
166
|
+
position="top"
|
|
167
|
+
offsetY={0}
|
|
168
|
+
className="p_8 ic_sm r_sm link bg_err_fd_hv cl_md cl_err_hv"
|
|
169
|
+
disabled={inactive}
|
|
170
|
+
onClick={() => p.onToggleConfirmDelete(true)}
|
|
171
|
+
>
|
|
172
|
+
<Icon name={COMMON_ICON_NAMES.delete} />
|
|
173
|
+
</TooltipButton>
|
|
174
|
+
) : null}
|
|
175
|
+
<TooltipButton
|
|
176
|
+
ariaLabel={p.editText ?? i18n.t('c.edit')}
|
|
177
|
+
message={p.editText ?? i18n.t('c.edit')}
|
|
178
|
+
position="top"
|
|
179
|
+
offsetY={0}
|
|
180
|
+
className="p_8 ic_sm r_sm link bg_primary_fd_hv cl_md"
|
|
181
|
+
disabled={inactive}
|
|
182
|
+
onClick={p.onClickEdit}
|
|
183
|
+
>
|
|
184
|
+
<Icon name={COMMON_ICON_NAMES.edit} />
|
|
185
|
+
</TooltipButton>
|
|
186
|
+
</span>
|
|
187
|
+
);
|
|
188
|
+
},
|
|
189
|
+
);
|
|
190
|
+
|
|
191
|
+
ListItemFormActions.displayName = 'ListItemFormActions';
|
|
192
|
+
|
|
193
|
+
/*
|
|
194
|
+
* Render a fixed-height row that shows its text with Edit/Delete icons. In edit
|
|
195
|
+
* mode it swaps to a horizontally scrolling strip of inputs followed by
|
|
196
|
+
* Cancel/Save, which sit outside the strip so its edge affordances stay
|
|
197
|
+
* visible. Deleting asks for confirmation in place. Edit mode can be owned by
|
|
198
|
+
* the row or by the caller; the form is re-seeded from initialFormValues each
|
|
199
|
+
* time edit mode turns on.
|
|
200
|
+
*/
|
|
201
|
+
export const ListItemForm = <V extends ListItemFormValues>(p: ListItemFormProps<V>) => {
|
|
202
|
+
const {
|
|
203
|
+
text,
|
|
204
|
+
secondaryText,
|
|
205
|
+
iconName,
|
|
206
|
+
iconTooltipText,
|
|
207
|
+
className,
|
|
208
|
+
heightClassName,
|
|
209
|
+
fieldFontSizeClassName,
|
|
210
|
+
buttonFontSizeClassName,
|
|
211
|
+
fields,
|
|
212
|
+
initialFormValues,
|
|
213
|
+
onChangeEditMode,
|
|
214
|
+
loading,
|
|
215
|
+
disabled,
|
|
216
|
+
editText,
|
|
217
|
+
deleteText,
|
|
218
|
+
RightItemComponent,
|
|
219
|
+
onClickSave,
|
|
220
|
+
onClickDelete,
|
|
221
|
+
} = p;
|
|
222
|
+
const [ownEditMode, setOwnEditMode] = useState(false);
|
|
223
|
+
// The delete icon first swaps the row actions for Cancel/Confirm; only Confirm fires onClickDelete.
|
|
224
|
+
const [confirmingDelete, setConfirmingDelete] = useState(false);
|
|
225
|
+
const editMode = p.editMode ?? ownEditMode;
|
|
226
|
+
const [values, setValues] = useState<V>(initialFormValues);
|
|
227
|
+
// Latest initial values are read when edit mode turns on, without re-seeding mid-edit.
|
|
228
|
+
const latestInitialValues = useRef(initialFormValues);
|
|
229
|
+
latestInitialValues.current = initialFormValues;
|
|
230
|
+
const inactive = !!disabled || !!loading;
|
|
231
|
+
const buttonClassName = buttonFontSizeClassName ?? 'ft_xs';
|
|
232
|
+
|
|
233
|
+
useEffect(() => {
|
|
234
|
+
if (editMode) setValues(latestInitialValues.current);
|
|
235
|
+
}, [editMode]);
|
|
236
|
+
|
|
237
|
+
/*
|
|
238
|
+
* Toggle edit mode through the owner when controlled, otherwise locally.
|
|
239
|
+
*/
|
|
240
|
+
const setEditMode = (next: boolean) => {
|
|
241
|
+
if (p.editMode === undefined) setOwnEditMode(next);
|
|
242
|
+
onChangeEditMode?.(next);
|
|
243
|
+
};
|
|
244
|
+
|
|
245
|
+
/*
|
|
246
|
+
* Update one draft field without changing the other values.
|
|
247
|
+
*/
|
|
248
|
+
const onChangeField = (name: string, value: string) => {
|
|
249
|
+
setValues((prev) => ({ ...prev, [name]: value }));
|
|
250
|
+
};
|
|
251
|
+
|
|
252
|
+
/*
|
|
253
|
+
* Submit the current draft when the row is interactive.
|
|
254
|
+
*/
|
|
255
|
+
const onSave = () => {
|
|
256
|
+
if (inactive) return;
|
|
257
|
+
onClickSave(values);
|
|
258
|
+
};
|
|
259
|
+
|
|
260
|
+
/*
|
|
261
|
+
* Save from any input with Enter; Escape abandons the edit.
|
|
262
|
+
*/
|
|
263
|
+
const onKeyDown = (e: KeyboardEvent<HTMLInputElement>) => {
|
|
264
|
+
if (e.key === 'Enter') {
|
|
265
|
+
e.preventDefault();
|
|
266
|
+
onSave();
|
|
267
|
+
} else if (e.key === 'Escape') {
|
|
268
|
+
e.preventDefault();
|
|
269
|
+
setEditMode(false);
|
|
270
|
+
}
|
|
271
|
+
};
|
|
272
|
+
|
|
273
|
+
return (
|
|
274
|
+
<div className={cn('rel w_f h_item gap_10', heightClassName ?? 'h_40', className)}>
|
|
275
|
+
{editMode ? (
|
|
276
|
+
<>
|
|
277
|
+
<MoreScrollArea
|
|
278
|
+
axis="horizontal"
|
|
279
|
+
indicatorPreset="knob"
|
|
280
|
+
indicatorSizeClassName="w_22 h_30"
|
|
281
|
+
containerClassName="min_w_0 h_f"
|
|
282
|
+
className="h_f h_item gap_10"
|
|
283
|
+
showTopIndicator
|
|
284
|
+
>
|
|
285
|
+
{fields.map((field) => (
|
|
286
|
+
<ListItemFormFieldControl
|
|
287
|
+
key={field.name}
|
|
288
|
+
field={field}
|
|
289
|
+
value={values[field.name] ?? ''}
|
|
290
|
+
disabled={inactive}
|
|
291
|
+
fontSizeClassName={fieldFontSizeClassName ?? 'ft_xs'}
|
|
292
|
+
onChange={onChangeField}
|
|
293
|
+
onKeyDown={onKeyDown}
|
|
294
|
+
/>
|
|
295
|
+
))}
|
|
296
|
+
</MoreScrollArea>
|
|
297
|
+
<div className="no_shrink h_item gap_5">
|
|
298
|
+
<Button
|
|
299
|
+
size="xs"
|
|
300
|
+
preset="outline_subtle"
|
|
301
|
+
className={buttonClassName}
|
|
302
|
+
text={i18n.t('c.cancel')}
|
|
303
|
+
disabled={inactive}
|
|
304
|
+
onClick={() => setEditMode(false)}
|
|
305
|
+
/>
|
|
306
|
+
<Button
|
|
307
|
+
size="xs"
|
|
308
|
+
preset="bg_primary"
|
|
309
|
+
className={buttonClassName}
|
|
310
|
+
text={i18n.t('c.save')}
|
|
311
|
+
disabled={inactive}
|
|
312
|
+
loading={loading}
|
|
313
|
+
onClick={onSave}
|
|
314
|
+
/>
|
|
315
|
+
</div>
|
|
316
|
+
</>
|
|
317
|
+
) : (
|
|
318
|
+
<>
|
|
319
|
+
{iconName ? (
|
|
320
|
+
<ListItemFormIcon
|
|
321
|
+
iconName={iconName}
|
|
322
|
+
tooltipText={iconTooltipText}
|
|
323
|
+
/>
|
|
324
|
+
) : null}
|
|
325
|
+
<span className="f min_w_0 h_item gap_8">
|
|
326
|
+
<span className="ellip no_shrink">{text}</span>
|
|
327
|
+
{secondaryText ? <span className="ellip ft_xs cl_md">{secondaryText}</span> : null}
|
|
328
|
+
</span>
|
|
329
|
+
{confirmingDelete ? null : RightItemComponent}
|
|
330
|
+
<ListItemFormActions
|
|
331
|
+
buttonClassName={buttonClassName}
|
|
332
|
+
confirmingDelete={confirmingDelete}
|
|
333
|
+
deleteText={deleteText}
|
|
334
|
+
editText={editText}
|
|
335
|
+
inactive={inactive}
|
|
336
|
+
loading={loading}
|
|
337
|
+
onClickDelete={onClickDelete}
|
|
338
|
+
onClickEdit={() => setEditMode(true)}
|
|
339
|
+
onToggleConfirmDelete={setConfirmingDelete}
|
|
340
|
+
/>
|
|
341
|
+
</>
|
|
342
|
+
)}
|
|
343
|
+
</div>
|
|
344
|
+
);
|
|
345
|
+
};
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import type { ReactNode } from 'react';
|
|
2
|
+
import type { SelectOptionType } from './Select';
|
|
3
|
+
|
|
4
|
+
export type ListItemFormValues = Record<string, string>;
|
|
5
|
+
|
|
6
|
+
export interface ListItemFormField {
|
|
7
|
+
name: string;
|
|
8
|
+
type: 'text' | 'email' | 'select';
|
|
9
|
+
// Small caption pinned to the top of the field.
|
|
10
|
+
label?: string;
|
|
11
|
+
// Select choices; ignored for text inputs.
|
|
12
|
+
options?: SelectOptionType[];
|
|
13
|
+
placeholder?: string;
|
|
14
|
+
maxLength?: number;
|
|
15
|
+
disabled?: boolean;
|
|
16
|
+
// Width utility class of the field; defaults to w_200.
|
|
17
|
+
widthClassName?: string;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export interface ListItemFormProps<V extends ListItemFormValues> {
|
|
21
|
+
text: string;
|
|
22
|
+
// Muted text shown after the main text in display mode.
|
|
23
|
+
secondaryText?: string;
|
|
24
|
+
iconName?: string;
|
|
25
|
+
// Hover tooltip of the icon; without it the icon is plain and not interactive.
|
|
26
|
+
iconTooltipText?: string;
|
|
27
|
+
className?: string;
|
|
28
|
+
// Height utility class of the row; defaults to h_40.
|
|
29
|
+
heightClassName?: string;
|
|
30
|
+
// Font size utility class of the edit-mode inputs and select triggers; defaults to ft_xs.
|
|
31
|
+
fieldFontSizeClassName?: string;
|
|
32
|
+
// Font size utility class of the edit-mode Cancel/Save buttons; defaults to ft_sm.
|
|
33
|
+
buttonFontSizeClassName?: string;
|
|
34
|
+
fields: ListItemFormField[];
|
|
35
|
+
// Copied into the form whenever the row enters edit mode.
|
|
36
|
+
initialFormValues: V;
|
|
37
|
+
// Controlled edit mode; leave undefined to let the row own it.
|
|
38
|
+
editMode?: boolean;
|
|
39
|
+
onChangeEditMode?: (editMode: boolean) => void;
|
|
40
|
+
// True while any mutation owned by this row (save or delete) is in flight.
|
|
41
|
+
loading?: boolean;
|
|
42
|
+
disabled?: boolean;
|
|
43
|
+
editText?: string;
|
|
44
|
+
deleteText?: string;
|
|
45
|
+
// Extra display-mode content between the text and the action icons.
|
|
46
|
+
RightItemComponent?: ReactNode;
|
|
47
|
+
onClickSave: (values: V) => void;
|
|
48
|
+
// The delete icon only renders when this handler is present.
|
|
49
|
+
onClickDelete?: () => void;
|
|
50
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import i18n from '@jsb188/app/i18n/index.ts';
|
|
2
|
+
import { Icon } from '@jsb188/react-web/svgs/Icon';
|
|
3
|
+
import { useState } from 'react';
|
|
4
|
+
import { Input } from './Input';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Password input
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
export function PasswordInput(p: React.ComponentProps<typeof Input>) {
|
|
11
|
+
const [inputType, setInputType] = useState('password');
|
|
12
|
+
|
|
13
|
+
return (
|
|
14
|
+
<Input
|
|
15
|
+
{...p}
|
|
16
|
+
type={inputType}
|
|
17
|
+
>
|
|
18
|
+
<p>
|
|
19
|
+
<button
|
|
20
|
+
type="button"
|
|
21
|
+
className="btn ft_xs cl_lt mt_3 h_item no_link"
|
|
22
|
+
onClick={(e: React.MouseEvent) => {
|
|
23
|
+
// Must preventDefault() to prevent HTML <form> submission
|
|
24
|
+
e.preventDefault();
|
|
25
|
+
setInputType(inputType === 'password' ? 'text' : 'password');
|
|
26
|
+
}}
|
|
27
|
+
>
|
|
28
|
+
<span className="shift_up mr_3">
|
|
29
|
+
<Icon name={inputType === 'password' ? 'square' : 'square-check-filled'} />
|
|
30
|
+
</span>
|
|
31
|
+
{i18n.t('account.show_password')}
|
|
32
|
+
</button>
|
|
33
|
+
</p>
|
|
34
|
+
</Input>
|
|
35
|
+
);
|
|
36
|
+
}
|