|
| 1 | +import { |
| 2 | + type EnsembleAction, |
| 3 | + useRegisterBindings, |
| 4 | +} from "@ensembleui/react-framework"; |
| 5 | +import { InputOTP } from "antd-input-otp"; |
| 6 | +import { useState } from "react"; |
| 7 | +import { Form } from "antd"; |
| 8 | +import { useEnsembleAction } from "../runtime/hooks/useEnsembleAction"; |
| 9 | +import { WidgetRegistry } from "../registry"; |
| 10 | +import type { EnsembleWidgetProps } from "../util/utils"; |
| 11 | +import type { TextStyles } from "./Text"; |
| 12 | + |
| 13 | +interface ConfirmationInputStyles { |
| 14 | + inputType?: "number" | "text"; |
| 15 | + fieldWidth?: number | string; |
| 16 | + fieldHeight?: number | string; |
| 17 | + gap?: number; |
| 18 | + spaceEvenly?: boolean; |
| 19 | + defaultFieldBorderColor?: string; |
| 20 | + activeFieldBorderColor?: string; |
| 21 | + filledFieldBorderColor?: string; |
| 22 | + defaultFieldBackgroundColor?: string; |
| 23 | + activeFieldBackgroundColor?: string; |
| 24 | + filledFieldBackgroundColor?: string; |
| 25 | + textStyle?: TextStyles; |
| 26 | + |
| 27 | + backgroundColor?: string; |
| 28 | + margin?: number | string; |
| 29 | + padding?: number | string; |
| 30 | + |
| 31 | + [key: string]: unknown; |
| 32 | +} |
| 33 | + |
| 34 | +const defaultStyles: ConfirmationInputStyles = { |
| 35 | + inputType: "number", |
| 36 | + fieldWidth: 50, |
| 37 | + fieldHeight: 50, |
| 38 | + gap: 10, |
| 39 | + spaceEvenly: true, |
| 40 | + defaultFieldBorderColor: "rgba(0, 0, 0, 0.451)", |
| 41 | + activeFieldBorderColor: "black", |
| 42 | + filledFieldBorderColor: "transparent", |
| 43 | + defaultFieldBackgroundColor: "transparent", |
| 44 | + activeFieldBackgroundColor: "transparent", |
| 45 | + filledFieldBackgroundColor: "transparent", |
| 46 | + textStyle: { |
| 47 | + color: "black", |
| 48 | + fontFamily: "inherit", |
| 49 | + fontSize: 25, |
| 50 | + fontWeight: "bold", |
| 51 | + backgroundColor: "transparent", |
| 52 | + }, |
| 53 | + |
| 54 | + backgroundColor: "transparent", |
| 55 | + margin: 0, |
| 56 | + padding: 0, |
| 57 | +}; |
| 58 | + |
| 59 | +type ConfirmationInputProps = { |
| 60 | + length?: number; |
| 61 | + onChange?: EnsembleAction; |
| 62 | + onComplete?: EnsembleAction; |
| 63 | +} & EnsembleWidgetProps<ConfirmationInputStyles>; |
| 64 | + |
| 65 | +export const ConfirmationInput: React.FC<ConfirmationInputProps> = (props) => { |
| 66 | + const { id, length, onChange, onComplete, styles, ...otherProps } = props; |
| 67 | + |
| 68 | + const [form] = Form.useForm(); |
| 69 | + |
| 70 | + const [text, setText] = useState(""); |
| 71 | + |
| 72 | + const onChangeAction = useEnsembleAction(onChange); |
| 73 | + const onCompleteAction = useEnsembleAction(onComplete); |
| 74 | + |
| 75 | + useRegisterBindings({ text }, id, { setText }); |
| 76 | + |
| 77 | + const handleChange = (value: string[]): void => { |
| 78 | + setText(value.join("")); |
| 79 | + |
| 80 | + onChangeAction?.callback({ |
| 81 | + [id as string]: { |
| 82 | + text: value.join(""), |
| 83 | + setText, |
| 84 | + }, |
| 85 | + }); |
| 86 | + }; |
| 87 | + |
| 88 | + const handleComplete = (values: Record<string, string[]>): unknown => |
| 89 | + onCompleteAction?.callback({ |
| 90 | + [id as string]: { |
| 91 | + text: values["confirmationItem"].join(""), |
| 92 | + setText, |
| 93 | + }, |
| 94 | + }); |
| 95 | + |
| 96 | + const customStyles = ` |
| 97 | + .confirmationInput:focus { |
| 98 | + border-color: ${ |
| 99 | + styles?.activeFieldBorderColor || defaultStyles.activeFieldBorderColor |
| 100 | + } !important; |
| 101 | + background-color: ${ |
| 102 | + styles?.activeFieldBackgroundColor || |
| 103 | + defaultStyles.activeFieldBackgroundColor |
| 104 | + } !important; |
| 105 | + } |
| 106 | +
|
| 107 | + .confirmationinput[value]:not([value=""]):not(.confirmationinput:where(.ant-input:focus), .confirmationinput:where(.ant-input-focused)) { |
| 108 | + border-color: ${ |
| 109 | + styles?.filledFieldBorderColor || defaultStyles.filledFieldBorderColor |
| 110 | + } !important; |
| 111 | + background-color: ${ |
| 112 | + styles?.filledFieldBackgroundColor || |
| 113 | + defaultStyles.filledFieldBackgroundColor |
| 114 | + } !important; |
| 115 | + } |
| 116 | + `; |
| 117 | + |
| 118 | + return ( |
| 119 | + <> |
| 120 | + <style>{customStyles}</style> |
| 121 | + <Form form={form} onFinish={handleComplete}> |
| 122 | + <Form.Item name="confirmationItem"> |
| 123 | + <InputOTP |
| 124 | + length={length || 4} |
| 125 | + onChange={handleChange} |
| 126 | + autoSubmit={form} |
| 127 | + inputType={styles?.inputType === "text" ? "all" : "numeric"} |
| 128 | + inputClassName="confirmationInput" |
| 129 | + wrapperStyle={{ |
| 130 | + backgroundColor: |
| 131 | + styles?.backgroundColor || defaultStyles.backgroundColor, |
| 132 | + margin: styles?.margin || defaultStyles.margin, |
| 133 | + padding: styles?.padding || defaultStyles.padding, |
| 134 | + gap: |
| 135 | + styles?.gap && !styles?.spaceEvenly |
| 136 | + ? styles?.gap |
| 137 | + : defaultStyles.gap, |
| 138 | + ...(styles?.spaceEvenly && { |
| 139 | + flex: 1, |
| 140 | + alignItems: "center", |
| 141 | + justifyContent: "space-evenly", |
| 142 | + }), |
| 143 | + }} |
| 144 | + inputStyle={{ |
| 145 | + padding: 0, |
| 146 | + margin: 0, |
| 147 | + width: styles?.fieldWidth || defaultStyles.fieldWidth, |
| 148 | + height: styles?.fieldHeight || defaultStyles.fieldHeight, |
| 149 | + borderColor: |
| 150 | + styles?.defaultFieldBorderColor || |
| 151 | + defaultStyles.defaultFieldBorderColor, |
| 152 | + backgroundColor: |
| 153 | + styles?.defaultFieldBackgroundColor || |
| 154 | + defaultStyles.defaultFieldBackgroundColor, |
| 155 | + fontFamily: |
| 156 | + styles?.textStyle?.fontFamily || |
| 157 | + defaultStyles.textStyle?.fontFamily, |
| 158 | + fontSize: |
| 159 | + styles?.textStyle?.fontSize || |
| 160 | + defaultStyles.textStyle?.fontSize, |
| 161 | + fontWeight: |
| 162 | + styles?.textStyle?.fontWeight || |
| 163 | + defaultStyles.textStyle?.fontWeight, |
| 164 | + color: styles?.textStyle?.color || defaultStyles.textStyle?.color, |
| 165 | + }} |
| 166 | + {...otherProps} |
| 167 | + /> |
| 168 | + </Form.Item> |
| 169 | + </Form> |
| 170 | + </> |
| 171 | + ); |
| 172 | +}; |
| 173 | + |
| 174 | +WidgetRegistry.register("ConfirmationInput", ConfirmationInput); |
0 commit comments