I'm sure this has been addressed many times before, but my googling is failing me. I have a variable of type number that is assigned to an input field in my app. I have to assign '0' to that variable because it is a number, but I do not want '0' to get auto-filled into the input field. How do I get around this issue? This is using React.
#Number or "empty string" type
39 messages · Page 1 of 1 (latest)
with plain HTML input fields? using React? Angular forms?
sounds like your questions is missing an important part
also, not really a TS question, more about the controls you are using getting a default value depending on what types they are
that's only about the types
it does not impact runtime
if you want your input to have a default value, you should give one to it
but I don't
iirc html inputs have a default attribute
const emptyRule: Rule = {
id: 'rule-0',
title: 'Static Rule #1',
comparison: '',
blendContains: '',
containsAmount: 0,
addExclude: '',
amount: 0,
addExIngredient: ''
};
if you want blank, use a text input
ok, I will have to go that route
thank you
well, i am using a text input
but it is ttied to the 'amount' which has to be set to 0 initially
export type Rule = {
id: string;
type: string;
title: string;
comparison: string;
blendContains: string;
containsAmount: number;
addExclude: string;
amount: number;
addExIngredient: string;
};
you could maybe share the code so we can have a closer look at it
not just the props, but the component with the jsx
<input
type={'text'}
name={'new-rule-amount'}
id={'new-rule-amount'}
className={'form-control'}
placeholder={'e.g. 25'}
value={newRule.amount}
onChange={handleInputChange}
/>
well, just use a number | undefined for your value
if it's underfined, then it will display nothing in the input box (blank)
and if it's a number, it will display it
when the user submits the form, or does something, just check the value isn't undefined to see if the user actually inputed any data
also you could be using a pattern on your input to only allow numbers
Preview:```ts
import React from "react"
export type RuleProps = {
id: string
type: string
title: string
comparison: string
blendContains: string
containsAmount: number
addExclude: string
amount: number
addExIngredient: strin
...```
You can choose specific lines to embed by selecting them before copying the link.