react-better-html 1.1.43 → 1.1.44
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.
|
@@ -8,7 +8,7 @@ type InternalToggleInputProps<Value> = {
|
|
|
8
8
|
infoText?: string;
|
|
9
9
|
value?: Value;
|
|
10
10
|
onChange?: (checked: boolean, value?: Value) => void;
|
|
11
|
-
} & OmitProps<React.ComponentProps<"input">, "style" | "value" | "ref" | "onChange"
|
|
11
|
+
} & OmitProps<React.ComponentProps<"input">, "style" | "value" | "ref" | "onChange"> & ComponentStyle & ComponentHoverStyle;
|
|
12
12
|
export type ToggleInputProps<Value> = ComponentPropWithRef<ToggleInputRef, OmitProps<InternalToggleInputProps<Value>, "type">>;
|
|
13
13
|
type ToggleInputComponentType = <Value>(props: ToggleInputProps<Value>) => React.ReactElement;
|
|
14
14
|
declare const _default: {
|
|
@@ -11,13 +11,17 @@ const Text_1 = __importDefault(require("./Text"));
|
|
|
11
11
|
const Div_1 = __importDefault(require("./Div"));
|
|
12
12
|
const Icon_1 = __importDefault(require("./Icon"));
|
|
13
13
|
const BetterHtmlProvider_1 = require("./BetterHtmlProvider");
|
|
14
|
+
const Label_1 = __importDefault(require("./Label"));
|
|
15
|
+
const componentSize = 26;
|
|
16
|
+
const switchComponentBallGap = 3;
|
|
17
|
+
const switchComponentMouseDownDifference = 4;
|
|
14
18
|
const InputElement = styled_components_1.default.input.withConfig({
|
|
15
19
|
shouldForwardProp: (prop) => !["theme", "normalStyle", "hoverStyle"].includes(prop),
|
|
16
20
|
}) `
|
|
17
21
|
position: relative;
|
|
18
22
|
appearance: none;
|
|
19
|
-
width:
|
|
20
|
-
height:
|
|
23
|
+
width: ${componentSize}px;
|
|
24
|
+
height: ${componentSize}px;
|
|
21
25
|
background-color: ${(props) => props.theme.colors.backgroundContent};
|
|
22
26
|
border: 1px solid ${(props) => props.theme.colors.border};
|
|
23
27
|
border-radius: ${(props) => props.theme.styles.borderRadius / 2}px;
|
|
@@ -46,13 +50,15 @@ const InputElement = styled_components_1.default.input.withConfig({
|
|
|
46
50
|
}
|
|
47
51
|
`;
|
|
48
52
|
const SwitchElement = styled_components_1.default.div.withConfig({
|
|
49
|
-
shouldForwardProp: (prop) => !["theme", "checked", "disabled", "normalStyle", "hoverStyle"].includes(prop),
|
|
53
|
+
shouldForwardProp: (prop) => !["theme", "checked", "disabled", "isMouseDown", "normalStyle", "hoverStyle"].includes(prop),
|
|
50
54
|
}) `
|
|
55
|
+
--width: ${(props) => componentSize * 2 - props.theme.styles.gap / 2}px;
|
|
56
|
+
|
|
51
57
|
position: relative;
|
|
52
|
-
width:
|
|
53
|
-
height:
|
|
58
|
+
width: var(--width);
|
|
59
|
+
height: ${componentSize}px;
|
|
54
60
|
background-color: ${(props) => (props.checked ? props.theme.colors.primary : props.theme.colors.border)};
|
|
55
|
-
border-radius:
|
|
61
|
+
border-radius: 999px;
|
|
56
62
|
cursor: ${(props) => (props.disabled ? "not-allowed" : "pointer")};
|
|
57
63
|
opacity: ${(props) => (props.disabled ? 0.5 : 1)};
|
|
58
64
|
transition: ${(props) => props.theme.styles.transition};
|
|
@@ -60,14 +66,18 @@ const SwitchElement = styled_components_1.default.div.withConfig({
|
|
|
60
66
|
&::before {
|
|
61
67
|
content: "";
|
|
62
68
|
position: absolute;
|
|
63
|
-
width:
|
|
64
|
-
height:
|
|
65
|
-
background-color:
|
|
66
|
-
border-radius:
|
|
67
|
-
top:
|
|
68
|
-
left:
|
|
69
|
+
width: ${(props) => componentSize - switchComponentBallGap * 2 + (props.isMouseDown ? switchComponentMouseDownDifference : 0)}px;
|
|
70
|
+
height: ${componentSize - switchComponentBallGap * 2}px;
|
|
71
|
+
background-color: ${(props) => props.theme.colors.base};
|
|
72
|
+
border-radius: 999px;
|
|
73
|
+
top: ${switchComponentBallGap}px;
|
|
74
|
+
left: ${switchComponentBallGap}px;
|
|
75
|
+
transform: translateX(
|
|
76
|
+
${(props) => props.checked
|
|
77
|
+
? `calc(var(--width) - ${componentSize + (props.isMouseDown ? switchComponentMouseDownDifference : 0)}px)`
|
|
78
|
+
: "0px"}
|
|
79
|
+
);
|
|
69
80
|
transition: ${(props) => props.theme.styles.transition};
|
|
70
|
-
transform: translateX(${(props) => (props.checked ? "16px" : "0")});
|
|
71
81
|
}
|
|
72
82
|
|
|
73
83
|
${(props) => props.normalStyle}
|
|
@@ -85,19 +95,19 @@ const ToggleInputComponent = (0, react_1.forwardRef)(function ToggleInput({ labe
|
|
|
85
95
|
const restProps = (0, hooks_1.useComponentPropsWithoutStyle)(props);
|
|
86
96
|
const [internalChecked, setInternalChecked] = (0, react_1.useState)(false);
|
|
87
97
|
const onChangeElement = (0, react_1.useCallback)((event) => {
|
|
88
|
-
const
|
|
98
|
+
const newIsChecked = event.target.checked;
|
|
89
99
|
if (controlledChecked === undefined)
|
|
90
|
-
setInternalChecked(
|
|
91
|
-
onChange?.(
|
|
100
|
+
setInternalChecked(newIsChecked);
|
|
101
|
+
onChange?.(newIsChecked, value);
|
|
92
102
|
}, [onChange, controlledChecked, value]);
|
|
93
103
|
const checked = controlledChecked ?? internalChecked;
|
|
94
104
|
const onClickText = (0, react_1.useCallback)(() => {
|
|
95
|
-
const
|
|
105
|
+
const newIsChecked = !checked;
|
|
96
106
|
if (controlledChecked === undefined)
|
|
97
|
-
setInternalChecked(
|
|
98
|
-
onChange?.(
|
|
107
|
+
setInternalChecked(newIsChecked);
|
|
108
|
+
onChange?.(newIsChecked, value);
|
|
99
109
|
}, [checked, controlledChecked, onChange, value]);
|
|
100
|
-
return ((0, jsx_runtime_1.jsxs)(Div_1.default.column, { width: "100%", gap: theme.styles.gap / 2, ...styledComponentStylesWithExcluded, children: [label && (
|
|
110
|
+
return ((0, jsx_runtime_1.jsxs)(Div_1.default.column, { width: "100%", gap: theme.styles.gap / 2, ...styledComponentStylesWithExcluded, children: [label && (0, jsx_runtime_1.jsx)(Label_1.default, { text: label, required: required, isError: !!errorText }), (0, jsx_runtime_1.jsxs)(Div_1.default.row, { alignItems: "center", gap: theme.styles.gap, children: [(0, jsx_runtime_1.jsxs)(Div_1.default.row, { position: "relative", alignItems: "center", children: [(0, jsx_runtime_1.jsx)(InputElement, { theme: theme, type: props.type ?? "checkbox", checked: checked, onChange: onChangeElement, ...styledComponentStyles, ...dataProps, ...ariaProps, ...restProps }), props.type === "checkbox" ? ((0, jsx_runtime_1.jsx)(Icon_1.default, { name: "check", position: "absolute", top: "50%", left: "50%", color: theme.colors.base, size: 14, transform: `translate(-50%, -50%)${checked ? "" : " scale(0.4)"}`, opacity: checked ? 1 : 0, pointerEvents: "none", transition: theme.styles.transition })) : props.type === "radio" ? ((0, jsx_runtime_1.jsx)(Div_1.default, { position: "absolute", width: 10, height: 10, top: "50%", left: "50%", backgroundColor: theme.colors.base, borderRadius: 999, transform: `translate(-50%, -50%)${checked ? "" : " scale(0.4)"}`, opacity: checked ? 1 : 0, pointerEvents: "none" })) : undefined] }), text && ((0, jsx_runtime_1.jsx)(Text_1.default, { userSelect: "none", cursor: "pointer", onClick: onClickText, children: text }))] }), (errorText || infoText) && ((0, jsx_runtime_1.jsx)(Text_1.default, { as: "span", display: "block", fontSize: 14, color: errorText ? theme.colors.error : theme.colors.textSecondary, children: errorText || infoText }))] }));
|
|
101
111
|
});
|
|
102
112
|
exports.default = {
|
|
103
113
|
checkbox: (0, react_1.forwardRef)(function Checkbox(props, ref) {
|
|
@@ -106,7 +116,7 @@ exports.default = {
|
|
|
106
116
|
radiobutton: (0, react_1.forwardRef)(function RadioButton(props, ref) {
|
|
107
117
|
return (0, jsx_runtime_1.jsx)(ToggleInputComponent, { type: "radio", ref: ref, ...props });
|
|
108
118
|
}),
|
|
109
|
-
switch: (0, react_1.forwardRef)(function Switch({ label, errorText, infoText, disabled, onChange, checked: controlledChecked, required, ...props }, ref) {
|
|
119
|
+
switch: (0, react_1.forwardRef)(function Switch({ label, errorText, infoText, disabled, value, onChange, checked: controlledChecked, required, ...props }, ref) {
|
|
110
120
|
const theme = (0, BetterHtmlProvider_1.useTheme)();
|
|
111
121
|
const styledComponentStyles = (0, hooks_1.useStyledComponentStyles)(props, theme, true);
|
|
112
122
|
const styledComponentStylesWithExcluded = (0, hooks_1.useComponentPropsWithExcludedStyle)(props);
|
|
@@ -114,15 +124,16 @@ exports.default = {
|
|
|
114
124
|
const ariaProps = (0, hooks_1.useComponentPropsWithPrefix)(props, "aria");
|
|
115
125
|
const restProps = (0, hooks_1.useComponentPropsWithoutStyle)(props);
|
|
116
126
|
const [internalChecked, setInternalChecked] = (0, hooks_1.useBooleanState)();
|
|
127
|
+
const [isMouseDown, setIsMouseDown] = (0, hooks_1.useBooleanState)();
|
|
117
128
|
const checked = controlledChecked ?? internalChecked;
|
|
118
129
|
const onClickElement = (0, react_1.useCallback)(() => {
|
|
119
130
|
if (disabled)
|
|
120
131
|
return;
|
|
121
|
-
const
|
|
132
|
+
const newIsChecked = !checked;
|
|
122
133
|
if (controlledChecked === undefined)
|
|
123
|
-
setInternalChecked.setState(
|
|
124
|
-
onChange?.(
|
|
125
|
-
}, [disabled, checked, onChange, controlledChecked]);
|
|
126
|
-
return ((0, jsx_runtime_1.jsxs)(Div_1.default.column, { width: "100%", gap: theme.styles.gap / 2, ...styledComponentStylesWithExcluded, children: [label && (
|
|
134
|
+
setInternalChecked.setState(newIsChecked);
|
|
135
|
+
onChange?.(newIsChecked, value);
|
|
136
|
+
}, [disabled, checked, onChange, controlledChecked, value]);
|
|
137
|
+
return ((0, jsx_runtime_1.jsxs)(Div_1.default.column, { width: "100%", gap: theme.styles.gap / 2, ...styledComponentStylesWithExcluded, children: [label && (0, jsx_runtime_1.jsx)(Label_1.default, { text: label, required: required, isError: !!errorText }), (0, jsx_runtime_1.jsx)(Div_1.default.row, { alignItems: "center", gap: theme.styles.gap, onMouseDown: setIsMouseDown.setTrue, onMouseUp: setIsMouseDown.setFalse, onMouseOut: setIsMouseDown.setFalse, onTouchStart: setIsMouseDown.setTrue, onTouchEnd: setIsMouseDown.setFalse, onTouchCancel: setIsMouseDown.setFalse, children: (0, jsx_runtime_1.jsx)(SwitchElement, { theme: theme, checked: checked, disabled: disabled ?? false, isMouseDown: isMouseDown, onClick: onClickElement, ...styledComponentStyles, ...dataProps, ...ariaProps, ...restProps }) }), (errorText || infoText) && ((0, jsx_runtime_1.jsx)(Text_1.default, { as: "span", display: "block", fontSize: 14, color: errorText ? theme.colors.error : theme.colors.textSecondary, children: errorText || infoText }))] }));
|
|
127
138
|
}),
|
|
128
139
|
};
|
package/dist/utils/hooks.d.ts
CHANGED
|
@@ -62,6 +62,8 @@ export declare function useForm<FormFields extends Record<string, string | numbe
|
|
|
62
62
|
getTextAreaProps: <FieldName extends keyof FormFields>(field: FieldName) => ComponentPropWithRef<HTMLTextAreaElement, TextareaFieldProps>;
|
|
63
63
|
getDropdownFieldProps: <FieldName extends keyof FormFields>(field: FieldName) => OmitProps<ComponentPropWithRef<HTMLDivElement, DropdownProps<FormFields[FieldName], unknown>>, "options">;
|
|
64
64
|
getCheckboxProps: <FieldName extends keyof FormFields>(field: FieldName) => ComponentPropWithRef<ToggleInputRef, ToggleInputProps<FormFields[FieldName]>>;
|
|
65
|
+
getRadioButtonProps: <FieldName extends keyof FormFields>(field: FieldName, value: FormFields[FieldName]) => ComponentPropWithRef<ToggleInputRef, ToggleInputProps<FormFields[FieldName]>>;
|
|
66
|
+
getSwitchProps: <FieldName extends keyof FormFields>(field: FieldName) => ComponentPropWithRef<ToggleInputRef, ToggleInputProps<FormFields[FieldName]>>;
|
|
65
67
|
focusField: (field: keyof FormFields) => void;
|
|
66
68
|
onSubmit: (event: React.FormEvent<HTMLFormElement>) => Promise<void>;
|
|
67
69
|
reset: () => void;
|
package/dist/utils/hooks.js
CHANGED
|
@@ -255,6 +255,28 @@ function useForm({ defaultValues, requiredFields, onSubmit, validate, }) {
|
|
|
255
255
|
errorText: errors[field],
|
|
256
256
|
};
|
|
257
257
|
}, [values, errors, setFieldValue]);
|
|
258
|
+
const getRadioButtonProps = (0, react_1.useCallback)((field, value) => {
|
|
259
|
+
return {
|
|
260
|
+
required: requiredFields?.includes(field),
|
|
261
|
+
checked: values[field] === value,
|
|
262
|
+
name: field.toString(),
|
|
263
|
+
value,
|
|
264
|
+
onChange: (checked, newValue) => {
|
|
265
|
+
setFieldValue(field, checked ? newValue : undefined);
|
|
266
|
+
},
|
|
267
|
+
errorText: errors[field],
|
|
268
|
+
};
|
|
269
|
+
}, [values, errors, setFieldValue]);
|
|
270
|
+
const getSwitchProps = (0, react_1.useCallback)((field) => {
|
|
271
|
+
return {
|
|
272
|
+
required: requiredFields?.includes(field),
|
|
273
|
+
checked: values[field],
|
|
274
|
+
onChange: (checked) => {
|
|
275
|
+
setFieldValue(field, checked);
|
|
276
|
+
},
|
|
277
|
+
errorText: errors[field],
|
|
278
|
+
};
|
|
279
|
+
}, [values, errors, setFieldValue]);
|
|
258
280
|
const focusField = (0, react_1.useCallback)((field) => {
|
|
259
281
|
inputFieldRefs.current[field]?.focus();
|
|
260
282
|
}, []);
|
|
@@ -290,6 +312,8 @@ function useForm({ defaultValues, requiredFields, onSubmit, validate, }) {
|
|
|
290
312
|
getTextAreaProps,
|
|
291
313
|
getDropdownFieldProps,
|
|
292
314
|
getCheckboxProps,
|
|
315
|
+
getRadioButtonProps,
|
|
316
|
+
getSwitchProps,
|
|
293
317
|
focusField,
|
|
294
318
|
onSubmit: onSubmitFunction,
|
|
295
319
|
reset,
|