fix field phone

This commit is contained in:
rizky 2024-08-18 20:28:10 -07:00
parent 98a2de1149
commit cbc22411b7
3 changed files with 44 additions and 25 deletions

View File

@ -9,6 +9,7 @@ import { FieldMoney } from "./TypeMoney";
import { FieldRichText } from "./TypeRichText"; import { FieldRichText } from "./TypeRichText";
import { FieldUpload } from "./TypeUpload"; import { FieldUpload } from "./TypeUpload";
import { KeyValue } from "./KeyValue"; import { KeyValue } from "./KeyValue";
import { InputMask, format } from "@react-input/mask";
export type PropTypeInput = { export type PropTypeInput = {
type: "input"; type: "input";
@ -29,7 +30,8 @@ export type PropTypeInput = {
| "password" | "password"
| "import" | "import"
| "monthly" | "monthly"
| "key-value"; | "key-value"
| "phone";
placeholder?: string; placeholder?: string;
onFocus?: (e: FocusEvent<HTMLDivElement>) => void; onFocus?: (e: FocusEvent<HTMLDivElement>) => void;
onBlur?: (e: FocusEvent<HTMLDivElement>) => void; onBlur?: (e: FocusEvent<HTMLDivElement>) => void;
@ -229,6 +231,44 @@ export const FieldTypeInput: FC<{
}} }}
/> />
); );
case "phone":
return (
<div className="c-flex c-relative c-flex-1">
<InputMask
mask="____-____-_______"
replacement={{ _: /\d/ }}
onChange={(ev) => {
fm.data[field.name] = ev.currentTarget.value.replace(/\D/g, "");
renderOnChange();
if (prop.onChange) {
prop.onChange(fm.data[field.name]);
}
}}
value={format(value, {
mask: "____-____-_______",
replacement: { _: /\d/ },
})}
placeholder={prop.placeholder || arg.placeholder || ""}
disabled={disabled}
className="c-flex-1 c-transition-all c-bg-transparent c-outline-none c-px-2 c-text-sm c-w-full"
spellCheck={false}
onFocus={(e) => {
field.focused = true;
field.render();
prop.onFocus?.(e);
}}
onKeyDown={(e) => {
if (e.key === "Enter" && fm.status === "ready") fm.submit();
}}
onBlur={(e) => {
field.focused = false;
field.render();
prop.onBlur?.(e);
}}
/>
</div>
);
case "monthly": { case "monthly": {
return ( return (
<Datepicker <Datepicker
@ -276,27 +316,6 @@ export const FieldTypeInput: FC<{
prop.onChange(fm.data[field.name]); prop.onChange(fm.data[field.name]);
} }
}} }}
onInput={
arg.sub_type === "phone"
? (event) => {
const target = event.currentTarget;
var matrix = target.defaultValue,
i = 0,
def = matrix.replace(/\D/g, ""),
val = target.value.replace(/\D/g, "");
def.length >= val.length && (val = def);
matrix = matrix.replace(/[_\d]/g, function (a) {
return val.charAt(i++) || "_";
});
target.value = matrix;
i = matrix.lastIndexOf(val.substr(-1));
i < matrix.length && matrix != target.defaultValue
? i++
: (i = matrix.indexOf("_"));
setCursorPosition(i, target);
}
: undefined
}
placeholder={prop.placeholder || arg.placeholder || ""} placeholder={prop.placeholder || arg.placeholder || ""}
value={value} value={value}
disabled={disabled} disabled={disabled}

View File

@ -163,13 +163,14 @@ export const TableList: FC<TableListProp> = ({
last_length: 0, last_length: 0,
scroll: (currentTarget: HTMLDivElement) => { scroll: (currentTarget: HTMLDivElement) => {
if ( if (
local.data.length < local.paging.take ||
local.data.length === 0 || local.data.length === 0 ||
local.status !== "ready" || local.status !== "ready" ||
!isAtBottom(currentTarget) || !isAtBottom(currentTarget) ||
local.reloading local.reloading
) )
return; return;
if (local.paging.last_length <= local.data.length) { if (local.paging.last_length <= local.data.length) {
local.paging.skip = local.data.length; local.paging.skip = local.data.length;
local.reload(); local.reload();
@ -788,8 +789,6 @@ export const TableList: FC<TableListProp> = ({
return ( return (
<> <>
{local.paging.skip} {local.paging.last_length} {local.data.length}{" "}
{local.status}
<div <div
className={cx( className={cx(
"table-list c-w-full c-h-full c-flex-1 c-relative c-overflow-hidden", "table-list c-w-full c-h-full c-flex-1 c-relative c-overflow-hidden",

View File

@ -11,6 +11,7 @@
"@radix-ui/react-navigation-menu": "^1.2.0", "@radix-ui/react-navigation-menu": "^1.2.0",
"@radix-ui/react-popover": "^1.0.7", "@radix-ui/react-popover": "^1.0.7",
"@radix-ui/react-progress": "^1.1.0", "@radix-ui/react-progress": "^1.1.0",
"@react-input/mask": "^1.2.5",
"@radix-ui/react-scroll-area": "^1.1.0", "@radix-ui/react-scroll-area": "^1.1.0",
"@radix-ui/react-slider": "^1.1.2", "@radix-ui/react-slider": "^1.1.2",
"@radix-ui/react-slot": "^1.0.2", "@radix-ui/react-slot": "^1.0.2",