1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 |
/** * Input Float numeric Plain React Hooks function * * props: * value * numeralDecimalScale * onChange * id * className * style */ function InputFloat(props) { const e = React.createElement; const refInput = React.useRef(null); const numeralDecimalScale = React.useRef(props.numeralDecimalScale ? props.numeralDecimalScale : 2); const SaveValue = React.useRef(format(props.value)); const Start = React.useRef(0); const End = React.useRef(0); const [value, setValue] = React.useState({ value: format(props.value) }); // Prop value changed React.useEffect(() => { if (props == undefined || props.value === undefined) { return; } // console.log("change props", props, props.value); if (float(props.value) == float(SaveValue.current)) { // console.log("not change props.value", props, props.value); return; } const v = format(props.value); setV(v, 0, 0); setValue({ value: v }); }, [props.value]); // Set cursor position after change state value React.useEffect(() => { refInput.current.setSelectionRange(Start.current, End.current); }, [value]); function setV(v, start, end) { // console.log("setV", v, start, end); SaveValue.current = v; Start.current = start; End.current = end; } /** * Using: * agh.sprintf.js * https://github.com/akinomyoga/agh.sprintf.js */ function format(v) { // console.log("format from", v); v += ""; const sign = v.indexOf("-") >= 0; const f = float(v); v = sprintf("%'0.0" + numeralDecimalScale.current + "f", f); if (sign && f == 0) { v = "-" + v; } return v; } // convert to float function float(v) { v += ""; const dot = dotPosition(v); let f = parseFloat(v.substring(0, dot + numeralDecimalScale.current + 1).replaceAll(",", "")); if (isNaN(f)) { f = 0; } return f; } function dotPosition(v) { v += ""; let dot = v.indexOf("."); if (dot == -1) { dot = v.length; } return dot; } /** * position より前の位置にある , の数を数える * Count the number of "," before the position of v */ function countComma(v, position) { const targetStr = ","; return (v.substring(0, position).match(new RegExp(targetStr, "g")) || []).length; } function replace(src, position, s) { return format(src.substring(0, position) + s + src.substring(position + s.length)); } function insert(src, position, s) { return format(src.substring(0, position) + s + src.substring(position)); } // src の position 位置から n 文字削除 // Delete n characters from position in src function remove(src, position, n) { return src.substring(0, position) + src.substring(position + n); } function handleKeyDown(e) { // console.log("handleKeyDown", e); // console.log("Key pressed: ", e.key); // console.log("Key pressed: ", e.which); let v = e.target.value; let c = e.key; let start = refInput.current.selectionStart; let end = refInput.current.selectionEnd; if (c == "Process" || c == "Control") { return false; } // console.log("c", c, "start", start, "end", end, "dom", v, "v", v); // Selected if (start != end) { if ((c >= "0" || c <= "9") || c == "-") { const co1 = countComma(v, start); v = remove(v, start, end - start); v = insert(v, start, c); v = format(v); const co2 = countComma(v, start - 1); const co = co1 - co2 - 1; setV(v, start - co, start - co); return; } // No change setV(v, start, end); return; } const dot = dotPosition(v); // console.log("c", c, "start", start, "end", end, "dot", dot, "dom", v, "v", v); if (c == "Delete") { const del = v.substring(start + 1, start); // console.log("del", del); if (del == ".") { // don't delete float point dot // No change setV(v, start, end); return; } if (start > dot) { // decimal part v = replace(v, start, "0"); setV(v, start + 1, end + 1); return; } if (del == ",") { const co1 = countComma(v, start + 1); v = remove(v, start + 1, 1); v = format(v); const co2 = countComma(v, start); const co = 1 - (co1 - co2); setV(v, start + co, end + co); return; } const co1 = countComma(v, start); v = remove(v, start, 1); v = format(v); const co2 = countComma(v, start - 1); const co = (co1 - co2); setV(v, start - co, end - co); return; } if (c == "Backspace") { if (dot + 1 == start) { // don't delete float point dot // No change setV(v, start - 1, end - 1); return; } if (start > dot) { // decimal part v = replace(v, start - 1, "0"); setV(v, start - 1, end - 1); return; } // real part const del = v.substring(start - 1, start); let cursorBack = 1; if (del == ",") { start--; end--; cursorBack = 2; } const co1 = countComma(v, start); v = remove(v, start - 1, 1); v = format(v); const co2 = countComma(v, start - 1); const co = cursorBack + (co1 - co2); setV(v, start - co, end - co); return; } // Input text ------------------ // console.log("c", c, "dot", dot, "start", start, "end", end, "dom", v, "v", v); if (c == ".") { // No change and Move cursor setV(v, dot + 1, dot + 1); refInput.current.setSelectionRange(dot + 1, dot + 1); return; } if (c == "-") { if (float(v) >= 0) { v = format("-" + v); setV(v, start + 1, end + 1); } else { // No change setV(v, start, end); } return; } if (c == "+") { if (float(v) < 0) { v = format(v.replace("-", "")); setV(v, start - 1, end - 1); } else { // No change setV(v, start, end); } return; } if (c == "0") { if (start > dot) { // decimal part v = replace(v, start, c); setV(v, start + 1, end + 1); return; } v = insert(v, start, c); setV(v, start + 1, end + 1); return; } if (c >= "1" && c <= "9") { if (start > dot) { // decimal part v = replace(v, start, c); setV(v, start + 1, end + 1); return; } // if real part is 0 if (parseInt(float(SaveValue.current)) == 0 && start <= dot) { // |0.?? or 0|.?? v = replace(v, dot - 1, c); setV(v, dot, dot); return; } if (start <= dot) { const c1 = countComma(v, start); v = insert(v, start, c); const c2 = countComma(v, start + 1); if (c2 > c1) { start++; end++; } setV(v, start + 1, end + 1); return; } } // No change setV(v, start, end); } // End of long long handleKeyDown // Select all text const handleFocus = () => { refInput.current.setSelectionRange(0, refInput.current.value.length); }; const handleChange = (event) => { // console.log("handleChange"); setValue({ value: SaveValue.current }); // Call props.onChange event.target.rawValue = float(SaveValue.current); event.target.value = format(SaveValue.current); props.onChange(event); }; // Render return e("input", { ref: refInput, id: props.id, className: props.className, style: props.style ? props.style : { textAlign: "right", imeMode: "disabled" }, value: value.value, onKeyDown: e => { handleKeyDown(e); }, onChange: e => { handleChange(e); }, onFocus: () => { handleFocus(); }, }); } export default InputFloat; |
ウェブ検索では見つけられなかったので作ってみたが
こんな感じいいのか? Chrome, Edge, Firefox では動作している