#Number or "empty string" type

39 messages · Page 1 of 1 (latest)

west moat
#

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.

echo walrus
#

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

west moat
#

Sorry, this is React

#

well, I though I could do something like input: number | ''

echo walrus
#

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

west moat
#

but I don't

echo walrus
#

iirc html inputs have a default attribute

west moat
#

I want a blank input

#

but it has to be 0

echo walrus
#

with numbers? don't think that's possible

#

huh?

#

blank or 0?

west moat
#

blank

#

but Typescript forces me to input a zero as the default value

echo walrus
#

right

#

if it's a number input

#

it must have a number

west moat
#
const emptyRule: Rule = {
    id: 'rule-0',
    title: 'Static Rule #1',
    comparison: '',
    blendContains: '',
    containsAmount: 0,
    addExclude: '',
    amount: 0,
    addExIngredient: ''
};
echo walrus
#

if you want blank, use a text input

west moat
#

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

echo walrus
#

oh, but that's not on the html

#

it's your own types/props

west moat
#
export type Rule = {
    id: string;
    type: string;
    title: string;
    comparison: string;
    blendContains: string;
    containsAmount: number;
    addExclude: string;
    amount: number;
    addExIngredient: string;
};
echo walrus
#

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

west moat
#
<input
                    type={'text'}
                    name={'new-rule-amount'}
                    id={'new-rule-amount'}
                    className={'form-control'}
                    placeholder={'e.g. 25'}
                    value={newRule.amount}
                    onChange={handleInputChange}
/>
echo walrus
#

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

clear stumpBOT
#
ascor8522#0

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
...```