MCPcopy Create free account
hub / github.com/cesanta/mongoose / TextValue

Function TextValue

tutorials/http/device-dashboard/web_root/components.js:177–206  ·  view source on GitHub ↗
({value, setfn, disabled, placeholder, type, addonRight, addonLeft, attr, min, max, step, mult})

Source from the content-addressed store, hash-verified

175};
176
177export function TextValue({value, setfn, disabled, placeholder, type, addonRight, addonLeft, attr, min, max, step, mult}) {
178 const [bg, setBg] = useState('bg-white');
179 useEffect(() => { if (type == 'number') checkval(+min, +max, +value); }, []);
180 step ||= '1', mult ||= 1;
181 const checkval = function(min, max, v) {
182 setBg('bg-white');
183 if (min && v < min) setBg('bg-red-100 border-red-200');
184 if (max && v > max) setBg('bg-red-100 border-red-200');
185 };
186 const m = step.match(/^.+\.(.+)/);
187 const digits = m ? m[1].length : 0;
188 const onchange = ev => {
189 let v = ev.target.value;
190 if (type == 'number') {
191 checkval(+min, +max, +v);
192 v = +(parseFloat(v) / mult).toFixed(digits);
193 }
194 setfn(v);
195 };
196 if (type == 'number') value = +(value * mult).toFixed(digits);
197 return html`
198<div class="flex w-full items-center rounded border shadow-sm ${bg}">
199 ${addonLeft && html`<span class="inline-flex font-normal truncate py-1 border-r bg-slate-100 items-center border-gray-300 px-2 text-gray-500 text-xs">${addonLeft}<//>` }
200 <input type=${type || 'text'} disabled=${disabled} value=${value}
201 step=${step} min=${min} max=${max}
202 onchange=${onchange} ...${attr}
203 class="${bg} font-normal text-sm rounded w-full flex-1 py-0.5 px-2 text-gray-700 placeholder:text-gray-400 focus:outline-none disabled:cursor-not-allowed disabled:bg-gray-100 disabled:text-gray-500" placeholder=${placeholder} />
204 ${addonRight && html`<span class="inline-flex font-normal truncate py-1 border-l bg-slate-100 items-center border-gray-300 px-2 text-gray-500 text-xs overflow-scroll" style="min-width: 50%;">${addonRight}<//>` }
205<//>`;
206};
207
208export function SelectValue({value, setfn, options, disabled}) {
209 const toInt = x => x == parseInt(x) ? parseInt(x) : x;

Callers

nothing calls this directly

Calls 1

checkvalFunction · 0.70

Tested by

no test coverage detected