gtsocial-umbx

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

text.jsx (2182B)


      1 /*
      2 	GoToSocial
      3 	Copyright (C) GoToSocial Authors admin@gotosocial.org
      4 	SPDX-License-Identifier: AGPL-3.0-or-later
      5 
      6 	This program is free software: you can redistribute it and/or modify
      7 	it under the terms of the GNU Affero General Public License as published by
      8 	the Free Software Foundation, either version 3 of the License, or
      9 	(at your option) any later version.
     10 
     11 	This program is distributed in the hope that it will be useful,
     12 	but WITHOUT ANY WARRANTY; without even the implied warranty of
     13 	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     14 	GNU Affero General Public License for more details.
     15 
     16 	You should have received a copy of the GNU Affero General Public License
     17 	along with this program.  If not, see <http://www.gnu.org/licenses/>.
     18 */
     19 
     20 "use strict";
     21 
     22 const React = require("react");
     23 
     24 const _default = "";
     25 module.exports = function useTextInput({ name, Name }, {
     26 	initialValue = _default,
     27 	dontReset = false,
     28 	validator,
     29 	showValidation = true,
     30 	initValidation
     31 } = {}) {
     32 
     33 	const [text, setText] = React.useState(initialValue);
     34 	const textRef = React.useRef(null);
     35 
     36 	const [validation, setValidation] = React.useState(initValidation ?? "");
     37 	const [_isValidating, startValidation] = React.useTransition();
     38 	let valid = validation == "";
     39 
     40 	function onChange(e) {
     41 		let input = e.target.value;
     42 		setText(input);
     43 
     44 		if (validator) {
     45 			startValidation(() => {
     46 				setValidation(validator(input));
     47 			});
     48 		}
     49 	}
     50 
     51 	function reset() {
     52 		if (!dontReset) {
     53 			setText(initialValue);
     54 		}
     55 	}
     56 
     57 	React.useEffect(() => {
     58 		if (validator && textRef.current) {
     59 			if (showValidation) {
     60 				textRef.current.setCustomValidity(validation);
     61 			} else {
     62 				textRef.current.setCustomValidity("");
     63 			}
     64 		}
     65 	}, [validation, validator, showValidation]);
     66 
     67 	// Array / Object hybrid, for easier access in different contexts
     68 	return Object.assign([
     69 		onChange,
     70 		reset,
     71 		{
     72 			[name]: text,
     73 			[`${name}Ref`]: textRef,
     74 			[`set${Name}`]: setText,
     75 			[`${name}Valid`]: valid,
     76 		}
     77 	], {
     78 		onChange,
     79 		reset,
     80 		name,
     81 		value: text,
     82 		ref: textRef,
     83 		setter: setText,
     84 		valid,
     85 		validate: () => setValidation(validator(text)),
     86 		hasChanged: () => text != initialValue,
     87 		_default
     88 	});
     89 };