tgui-core 3.0.2 → 3.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -12,26 +12,45 @@ type Props = Partial<{
12
12
  onEnter: (value: number) => void;
13
13
  /** Fires once the escape key is pressed */
14
14
  onEscape: (value: number) => void;
15
+ /** Fires on input validation change */
16
+ onValidationChange: (isValid: boolean) => void;
15
17
  /**
16
- * Generally, input can handle its own state value.
18
+ * Generally, input can handle its own state value. You might not NEED this.
17
19
  *
18
- * Use this if you want to hold the value in the parent for external manipulation.
20
+ * Use this if you want to hold the value in the parent for external
21
+ * manipulation. For instance:
19
22
  *
23
+ * ### Clearing the input
20
24
  * ```tsx
21
- * const [value, setValue] = useState(1;
25
+ * const [value, setValue] = useState(1);
22
26
  *
23
27
  * return (
24
28
  * <>
25
29
  * <Button onClick={() => act('inputVal', {inputVal: value})}>
26
30
  * Submit
27
31
  * </Button>
28
- * <RestrictedInput value={value} onChange={setValue} />
32
+ * <RestrictedInput
33
+ * value={value}
34
+ * onChange={setValue} />
29
35
  * <Button onClick={() => setValue(1)}>
30
36
  * Clear
31
37
  * </Button>
32
38
  * </>
33
39
  * )
34
40
  * ```
41
+ *
42
+ * ### Updating the value from the backend
43
+ * ```tsx
44
+ * const { data } = useBackend<Data>();
45
+ * const { valveSetting } = data;
46
+ *
47
+ * return (
48
+ * <RestrictedInput
49
+ * value={valveSetting}
50
+ * onEnter={(value) => act('submit', { valveSetting: value })}
51
+ * />
52
+ * )
53
+ * ```
35
54
  */
36
55
  value: number;
37
56
  }> & BaseInputProps;
@@ -40,6 +59,8 @@ type Props = Partial<{
40
59
  *
41
60
  * Creates a numerical input which rejects improper keys.
42
61
  *
62
+ * Has a special event for changes in validation states - `onValidationChange`.
63
+ *
43
64
  * @see https://github.com/tgstation/tgui-core/blob/main/lib/components/RestrictedInput.tsx
44
65
  */
45
66
  export declare function RestrictedInput(props: Props): import("react/jsx-runtime").JSX.Element;
@@ -1 +1 @@
1
- import*as e from"react/jsx-runtime";import*as t from"../common/math.js";import*as r from"../common/react.js";import*as u from"../common/timer.js";import*as n from"../common/ui.js";import*as o from"react";import*as m from"../common/keys.js";let s=(0,u.debounce)(e=>e(),250);function a(u){let{allowFloats:a,autoFocus:c,autoSelect:i,className:l,disabled:f,expensive:p,fluid:d,maxValue:v=1e4,minValue:b=0,monospace:j,onChange:E,onEnter:y,onEscape:I,...k}=u,x=(0,o.useRef)(null),[N,h]=(0,o.useState)(u.value??b),D=(0,o.useMemo)(()=>(0,n.computeBoxProps)(k),[k]),M=(0,o.useMemo)(()=>(0,r.classes)(["Input","RestrictedInput",f&&"Input--disabled",d&&"Input--fluid",j&&"Input--monospace",l]),[l,d,j]);function C(e){if(e===N)return;let r=e;Number.isNaN(e)?r=b:a||(r=Math.round(e)),h(r=(0,t.clamp)(r,b,v)),E&&(p?s(()=>E(r)):E(r))}return(0,o.useEffect)(()=>{let e;return(c||i)&&(e=setTimeout(()=>{x.current?.focus(),i&&x.current?.select()},1)),()=>clearTimeout(e)},[]),(0,o.useEffect)(()=>{document.activeElement!==x.current&&u.value!==N&&h(u.value??b)},[u.value]),(0,e.jsx)("input",{...D,autoComplete:"off",className:M,disabled:f,max:v,min:b,onChange:function(e){C(Number(e.target.value))},onKeyDown:function(e){if(e.key===m.KEY.Enter){e.preventDefault(),y?.(N),x.current?.blur();return}if((0,m.isEscape)(e.key)){e.preventDefault(),I?.(N),x.current?.blur();return}if(e.key===m.KEY.Minus){e.preventDefault(),C(-1*N);return}},ref:x,spellCheck:!1,type:"number",value:N})}export{a as RestrictedInput};
1
+ import*as e from"react/jsx-runtime";import*as t from"../common/react.js";import*as r from"../common/timer.js";import*as u from"../common/ui.js";import*as n from"react";import*as o from"../common/keys.js";let s=(0,r.debounce)(e=>e(),250);function a(r){let{allowFloats:a,autoFocus:c,autoSelect:m,className:i,disabled:l,expensive:f,fluid:p,maxValue:d=1e4,minValue:v=0,monospace:y,onChange:E,onEnter:b,onEscape:I,onValidationChange:j,...k}=r,x=(0,n.useRef)(null),[D,R]=(0,n.useState)(r.value??v),[C,K]=(0,n.useState)(!0),M=(0,n.useMemo)(()=>(0,u.computeBoxProps)(k),[k]),g=(0,n.useMemo)(()=>(0,t.classes)(["Input","RestrictedInput",l&&"Input--disabled",p&&"Input--fluid",y&&"Input--monospace",i,!C&&"RestrictedInput--invalid"]),[i,l,p,C,y]);return(0,n.useEffect)(()=>{let e;return(c||m)&&(e=setTimeout(()=>{x.current?.focus(),m&&x.current?.select()},1)),()=>clearTimeout(e)},[]),(0,n.useEffect)(()=>{if(x.current){let e=x.current.validity.valid;C!==e&&(K(e),j?.(e))}},[D]),(0,n.useEffect)(()=>{x.current&&document.activeElement!==x.current&&r.value!==D&&R(r.value??v)},[r.value]),(0,e.jsx)("input",{...M,autoComplete:"off",className:g,disabled:l,max:d,min:v,onChange:function(e){let t=Number(e.target.value);R(t),E&&(f?s(()=>E(t)):E(t))},onKeyDown:function(e){if(e.key===o.KEY.Enter){e.preventDefault(),b?.(D),x.current?.blur();return}if((0,o.isEscape)(e.key)){e.preventDefault(),I?.(D),x.current?.blur();return}if(e.key===o.KEY.Minus){e.preventDefault(),R(-1*D);return}},ref:x,spellCheck:!1,step:a?"any":"1",type:"number",value:D})}export{a as RestrictedInput};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tgui-core",
3
- "version": "3.0.2",
3
+ "version": "3.0.4",
4
4
  "description": "TGUI core component library",
5
5
  "keywords": ["TGUI", "library", "typescript"],
6
6
  "files": ["dist", "styles"],
@@ -43,3 +43,10 @@
43
43
  margin: 0;
44
44
  }
45
45
  }
46
+
47
+ .RestrictedInput--invalid {
48
+ border-color: var(--restricted-input-border-color-invalid);
49
+ &:focus-within {
50
+ border-color: var(--restricted-input-border-color-invalid);
51
+ }
52
+ }
@@ -141,6 +141,7 @@
141
141
  --restricted-input-border-color-focus: hsl(
142
142
  from var(--color-good) h s calc(l + 10)
143
143
  );
144
+ --restricted-input-border-color-invalid: var(--color-bad);
144
145
 
145
146
  /* Round Gauge */
146
147
  --round-gauge-ring-color: var(--input-border-color);