diff --git a/packages/react-core/src/components/NumberInput/examples/NumberInput.md b/packages/react-core/src/components/NumberInput/examples/NumberInput.md index dec8b098ab3..ef7f961cdd0 100644 --- a/packages/react-core/src/components/NumberInput/examples/NumberInput.md +++ b/packages/react-core/src/components/NumberInput/examples/NumberInput.md @@ -9,479 +9,40 @@ propComponents: ['NumberInput'] ### Default -```js -import React from 'react'; -import { NumberInput } from '@patternfly/react-core'; - -class BasicNumberInput extends React.Component { - constructor(props) { - super(props); - this.state = { - value: 90 - }; - - this.onMinus = () => { - this.setState({ - value: this.state.value - 1 - }); - }; - - this.onChange = event => { - this.setState({ - value: Number(event.target.value) - }); - }; - - this.onPlus = () => { - this.setState({ - value: this.state.value + 1 - }); - }; - } - - render() { - const { value } = this.state; - return ( - - ); - } -} +```ts file="./NumberInputDefault.tsx" ``` ### With unit -```js -import React from 'react'; -import { NumberInput } from '@patternfly/react-core'; - -class UnitNumberInput extends React.Component { - constructor(props) { - super(props); - this.state = { - value1: 90, - value2: (1.0).toFixed(2) - }; - - this.onMinus1 = () => { - this.setState({ - value1: this.state.value1 - 1 - }); - }; - - this.onChange1 = event => { - this.setState({ - value1: Number(event.target.value) - }); - }; - - this.onPlus1 = () => { - this.setState({ - value1: this.state.value1 + 1 - }); - }; - - this.onMinus2 = () => { - this.setState({ - value2: (Number(this.state.value2) - 0.01).toFixed(2) - }); - }; - - this.onChange2 = event => { - this.setState({ - value2: event.target.value - }); - }; - - this.onPlus2 = () => { - this.setState({ - value2: (Number(this.state.value2) + 0.01).toFixed(2) - }); - }; - } - - render() { - const { value1, value2 } = this.state; - return ( - - -
-
- -
- ); - } -} +```ts file="./NumberInputUnit.tsx" ``` ### With unit and thresholds -```js -import React from 'react'; -import { NumberInput } from '@patternfly/react-core'; - -class UnitThresholdNumberInput extends React.Component { - constructor(props) { - super(props); - this.state = { - value: 0 - }; - this.minValue = 0; - this.maxValue = 10; - - this.normalizeBetween = (value, min, max) => { - if (min !== undefined && max !== undefined) { - return Math.max(Math.min(value, max), min); - } else if (value <= min) { - return min; - } else if (value >= max) { - return max; - } - return value; - }; - - this.onMinus = () => { - this.setState({ - value: this.normalizeBetween(this.state.value - 1, this.minValue, this.maxValue) - }); - }; - - this.onChange = event => { - const newValue = isNaN(event.target.value) ? 0 : Number(event.target.value); - this.setState({ - value: newValue > this.maxValue ? this.maxValue : newValue < this.minValue ? this.minValue : newValue - }); - }; - - this.onPlus = () => { - this.setState({ - value: this.normalizeBetween(this.state.value + 1, this.minValue, this.maxValue) - }); - }; - } - - render() { - const { value } = this.state; - - return ( - - With a minimum value of 0 and maximum value of 10 -
- -
- ); - } -} +```ts file="./NumberInputUnitThreshold.tsx" ``` ### Disabled -```js -import React from 'react'; -import { NumberInput } from '@patternfly/react-core'; - -class DisabledNumberInput extends React.Component { - constructor(props) { - super(props); - this.state = { - value: 100 - }; - - this.onMinus = () => { - this.setState({ - value: this.state.value - 1 - }); - }; - - this.onChange = event => { - this.setState({ - value: Number(event.target.value) - }); - }; - - this.onPlus = () => { - this.setState({ - value: this.state.value + 1 - }); - }; - } - - render() { - const { value } = this.state; - const minValue = 0; - const maxValue = 100; - - return ( - - ); - } -} - +```ts file="./NumberInputDisabled.tsx" ``` + ### With status -```ts file="./WithStatus.tsx" +```ts file="./NumberInputWithStatus.tsx" ``` ### Varying sizes -```js -import React from 'react'; -import { NumberInput } from '@patternfly/react-core'; - -class NumberInputSizes extends React.Component { - constructor(props) { - super(props); - this.state = { - input1Value: 1, - input2Value: 1234567890, - input3Value: 5, - input4Value: 12345 - }; - - this.onChange = event => { - this.setState({ - [`${event.target.name}Value`]: Number(event.target.value) - }); - }; - - this.onMinus = (e, inputName) => { - this.setState({ - [`${inputName}Value`]: this.state[`${inputName}Value`] - 1 - }); - }; - - this.onPlus = (e, inputName) => { - this.setState({ - [`${inputName}Value`]: this.state[`${inputName}Value`] + 1 - }); - }; - } - - render() { - const { input1Value, input2Value, input3Value, input4Value } = this.state; - - return ( - - -
-
- -
-
- -
-
- -
- ); - } -} +```ts file="./NumberInputVaryingSizes.tsx" ``` ### Custom increment/decrement -```js -import React from 'react'; -import { NumberInput } from '@patternfly/react-core'; - -class CustomStepNumberInput extends React.Component { - constructor(props) { - super(props); - this.state = { - value: 90, - step: 3 - }; - - this.stepper = step => () => { - this.setState(prev => ({ ...prev, value: prev.value + step })); - }; - - this.onChange = event => { - this.setState({ - value: Number(event.target.value) - }); - }; - } - - render() { - const { value } = this.state; - return ( - - ); - } -} +```ts file="./NumberInputCustomStep.tsx" ``` ### Custom increment/decrement and thresholds -```js -import React from 'react'; -import { NumberInput } from '@patternfly/react-core'; - -class CustomStepNumberInput extends React.Component { - constructor(props) { - super(props); - this.state = { - value: 90, - step: 3 - }; - this.minValue = 90; - this.maxValue = 100; - - this.normalizeBetween = (value, min, max) => { - if (min !== undefined && max !== undefined) { - return Math.max(Math.min(value, max), min); - } else if (value <= min) { - return min; - } else if (value >= max) { - return max; - } - return value; - }; - - this.stepper = step => () => { - this.setState(prev => ({ - ...prev, - value: this.normalizeBetween(prev.value + step, this.minValue, this.maxValue) - })); - }; - - this.onChange = event => { - const newValue = isNaN(event.target.value) ? 0 : Number(event.target.value); - this.setState({ - value: newValue - }); - }; - - this.onBlur = event => { - const newValue = isNaN(event.target.value) ? 0 : Number(event.target.value); - this.setState({ - value: newValue > this.maxValue ? this.maxValue : newValue < this.minValue ? this.minValue : newValue - }); - }; - } - - render() { - const { value } = this.state; - return ( - - ); - } -} +```ts file="./NumberInputCustomStepAndThreshold.tsx" ``` diff --git a/packages/react-core/src/components/NumberInput/examples/NumberInputCustomStep.tsx b/packages/react-core/src/components/NumberInput/examples/NumberInputCustomStep.tsx new file mode 100644 index 00000000000..d46391fa724 --- /dev/null +++ b/packages/react-core/src/components/NumberInput/examples/NumberInputCustomStep.tsx @@ -0,0 +1,29 @@ +import React from 'react'; +import { NumberInput } from '@patternfly/react-core'; + +export const NumberInputCustomStep: React.FunctionComponent = () => { + const [value, setValue] = React.useState(90); + const step = 3; + + const stepper = (stepValue: number) => { + setValue(value + stepValue); + }; + + const onChange = (event: React.FormEvent) => { + const target = event.target as HTMLInputElement; + setValue(Number(target.value)); + }; + + return ( + stepper(-step)} + onChange={onChange} + onPlus={() => stepper(step)} + inputName="input" + inputAriaLabel="number input" + minusBtnAriaLabel="minus" + plusBtnAriaLabel="plus" + /> + ); +}; diff --git a/packages/react-core/src/components/NumberInput/examples/NumberInputCustomStepAndThreshold.tsx b/packages/react-core/src/components/NumberInput/examples/NumberInputCustomStepAndThreshold.tsx new file mode 100644 index 00000000000..a77f875dc3a --- /dev/null +++ b/packages/react-core/src/components/NumberInput/examples/NumberInputCustomStepAndThreshold.tsx @@ -0,0 +1,52 @@ +import React from 'react'; +import { NumberInput } from '@patternfly/react-core'; + +export const NumberInputCustomStepAndThreshold: React.FunctionComponent = () => { + const [value, setValue] = React.useState(90); + const minValue = 90; + const maxValue = 100; + const step = 3; + + const normalizeBetween = (value, min, max) => { + if (min !== undefined && max !== undefined) { + return Math.max(Math.min(value, max), min); + } else if (value <= min) { + return min; + } else if (value >= max) { + return max; + } + return value; + }; + + const stepper = (stepValue: number) => { + setValue(value + stepValue); + }; + + const onChange = (event: React.FormEvent) => { + const target = event.target as HTMLInputElement; + const newValue = normalizeBetween(isNaN(+target.value) ? 0 : Number(target.value), minValue, maxValue); + setValue(newValue); + }; + + const onBlur = (event: React.FormEvent) => { + const target = event.target as HTMLInputElement; + const newValue = normalizeBetween(isNaN(+target.value) ? 0 : Number(target.value), minValue, maxValue); + setValue(newValue); + }; + + return ( + stepper(-step)} + onChange={onChange} + onBlur={onBlur} + onPlus={() => stepper(step)} + inputName="input" + inputAriaLabel="number input" + minusBtnAriaLabel="minus" + plusBtnAriaLabel="plus" + /> + ); +}; diff --git a/packages/react-core/src/components/NumberInput/examples/NumberInputDefault.tsx b/packages/react-core/src/components/NumberInput/examples/NumberInputDefault.tsx new file mode 100644 index 00000000000..a3e973a79f2 --- /dev/null +++ b/packages/react-core/src/components/NumberInput/examples/NumberInputDefault.tsx @@ -0,0 +1,34 @@ +import React from 'react'; +import { NumberInput } from '@patternfly/react-core'; + +export const NumberInputDefault: React.FunctionComponent = () => { + const [value, setValue] = React.useState(90); + + const onMinus = () => { + const newValue = value - 1; + setValue(newValue); + }; + + const onChange = (event: React.FormEvent) => { + const target = event.target as HTMLInputElement; + setValue(Number(target.value)); + }; + + const onPlus = () => { + const newValue = value + 1; + setValue(newValue); + }; + + return ( + + ); +}; diff --git a/packages/react-core/src/components/NumberInput/examples/NumberInputDisabled.tsx b/packages/react-core/src/components/NumberInput/examples/NumberInputDisabled.tsx new file mode 100644 index 00000000000..20b35f919d2 --- /dev/null +++ b/packages/react-core/src/components/NumberInput/examples/NumberInputDisabled.tsx @@ -0,0 +1,40 @@ +import React from 'react'; +import { NumberInput } from '@patternfly/react-core'; + +export const NumberInputDisabled: React.FunctionComponent = () => { + const [value, setValue] = React.useState(100); + const minValue = 0; + const maxValue = 100; + + const onMinus = () => { + const newValue = value - 1; + setValue(newValue); + }; + + const onChange = (event: React.FormEvent) => { + const target = event.target as HTMLInputElement; + setValue(Number(target.value)); + }; + + const onPlus = () => { + const newValue = value + 1; + setValue(newValue); + }; + + return ( + + ); +}; diff --git a/packages/react-core/src/components/NumberInput/examples/NumberInputUnit.tsx b/packages/react-core/src/components/NumberInput/examples/NumberInputUnit.tsx new file mode 100644 index 00000000000..1fcb8a79c0c --- /dev/null +++ b/packages/react-core/src/components/NumberInput/examples/NumberInputUnit.tsx @@ -0,0 +1,57 @@ +import React from 'react'; +import { NumberInput } from '@patternfly/react-core'; + +export const NumberInputUnit: React.FunctionComponent = () => { + const [value1, setValue1] = React.useState(90); + const [value2, setValue2] = React.useState(Number((1.0).toFixed(2))); + + const onMinus1 = () => setValue1(value1 - 1); + const onChange1 = (event: React.FormEvent) => { + const target = event.target as HTMLInputElement; + setValue1(Number(target.value)); + }; + const onPlus1 = () => setValue1(value1 + 1); + + const onMinus2 = () => { + const newValue = Number((value2 - 0.01).toFixed(2)); + setValue2(newValue); + }; + const onChange2 = (event: React.FormEvent) => { + const target = event.target as HTMLInputElement; + setValue2(Number(target.value)); + }; + const onPlus2 = () => { + const newValue = Number((value2 + 0.01).toFixed(2)); + setValue2(newValue); + }; + + return ( + + +
+
+ +
+ ); +}; diff --git a/packages/react-core/src/components/NumberInput/examples/NumberInputUnitThreshold.tsx b/packages/react-core/src/components/NumberInput/examples/NumberInputUnitThreshold.tsx new file mode 100644 index 00000000000..5ef9de026d2 --- /dev/null +++ b/packages/react-core/src/components/NumberInput/examples/NumberInputUnitThreshold.tsx @@ -0,0 +1,55 @@ +import React from 'react'; +import { NumberInput } from '@patternfly/react-core'; + +export const NumberInputUnitThreshold: React.FunctionComponent = () => { + const [value, setValue] = React.useState(0); + const minValue = 0; + const maxValue = 10; + + const normalizeBetween = (value, min, max) => { + if (min !== undefined && max !== undefined) { + return Math.max(Math.min(value, max), min); + } else if (value <= min) { + return min; + } else if (value >= max) { + return max; + } + return value; + }; + + const onMinus = () => { + const newValue = normalizeBetween(value - 1, minValue, maxValue); + setValue(newValue); + }; + + const onChange = (event: React.FormEvent) => { + const target = event.target as HTMLInputElement; + const newValue = normalizeBetween(isNaN(+target.value) ? 0 : Number(target.value), minValue, maxValue); + setValue(newValue); + }; + + const onPlus = () => { + const newValue = normalizeBetween(value + 1, minValue, maxValue); + setValue(newValue); + }; + + return ( + + With a minimum value of 0 and maximum value of 10 +
+ +
+ ); +}; diff --git a/packages/react-core/src/components/NumberInput/examples/NumberInputVaryingSizes.tsx b/packages/react-core/src/components/NumberInput/examples/NumberInputVaryingSizes.tsx new file mode 100644 index 00000000000..ddfa0c9209c --- /dev/null +++ b/packages/react-core/src/components/NumberInput/examples/NumberInputVaryingSizes.tsx @@ -0,0 +1,80 @@ +import React from 'react'; +import { NumberInput } from '@patternfly/react-core'; + +export const NumberInputVaryingSizes: React.FunctionComponent = () => { + const [input1Value, setInput1Value] = React.useState(1); + const [input2Value, setInput2Value] = React.useState(1234567890); + const [input3Value, setInput3Value] = React.useState(5); + const [input4Value, setInput4Value] = React.useState(12345); + + const onChange = ( + event: React.FormEvent, + updateFunction: React.Dispatch> + ) => { + const target = event.target as HTMLInputElement; + updateFunction(Number(target.value)); + }; + + const onMinus = (value, updateFunction: React.Dispatch>) => { + updateFunction(value - 1); + }; + + const onPlus = (value, updateFunction: React.Dispatch>) => { + updateFunction(value + 1); + }; + + return ( + + onMinus(input1Value, setInput1Value)} + onChange={event => onChange(event, setInput1Value)} + onPlus={() => onPlus(input1Value, setInput1Value)} + inputName="input1" + inputAriaLabel="number input 1" + minusBtnAriaLabel="input 2 minus" + plusBtnAriaLabel="input 2 plus" + widthChars={1} + /> +
+
+ onMinus(input2Value, setInput2Value)} + onChange={event => onChange(event, setInput2Value)} + onPlus={() => onPlus(input2Value, setInput2Value)} + inputName="input2" + inputAriaLabel="number input 2" + minusBtnAriaLabel="input 2 minus" + plusBtnAriaLabel="input 2 plus" + widthChars={10} + /> +
+
+ onMinus(input3Value, setInput3Value)} + onChange={event => onChange(event, setInput3Value)} + onPlus={() => onPlus(input3Value, setInput3Value)} + inputName="input3" + inputAriaLabel="number input 3" + minusBtnAriaLabel="input 3 minus" + plusBtnAriaLabel="input 3 plus" + widthChars={5} + /> +
+
+ onMinus(input4Value, setInput4Value)} + onChange={event => onChange(event, setInput4Value)} + onPlus={() => onPlus(input4Value, setInput4Value)} + inputName="input4" + inputAriaLabel="number input 4" + minusBtnAriaLabel="input 4 minus" + plusBtnAriaLabel="input 4 plus" + widthChars={5} + /> +
+ ); +}; diff --git a/packages/react-core/src/components/NumberInput/examples/WithStatus.tsx b/packages/react-core/src/components/NumberInput/examples/NumberInputWithStatus.tsx similarity index 84% rename from packages/react-core/src/components/NumberInput/examples/WithStatus.tsx rename to packages/react-core/src/components/NumberInput/examples/NumberInputWithStatus.tsx index 88584de1ab2..b5b6570a0f8 100644 --- a/packages/react-core/src/components/NumberInput/examples/WithStatus.tsx +++ b/packages/react-core/src/components/NumberInput/examples/NumberInputWithStatus.tsx @@ -1,7 +1,7 @@ import React from 'react'; import { NumberInput, ValidatedOptions } from '@patternfly/react-core'; -export const WithStatus: React.FunctionComponent = () => { +export const NumberInputWithStatus: React.FunctionComponent = () => { const max = 10; const min = 0; @@ -20,8 +20,9 @@ export const WithStatus: React.FunctionComponent = () => { validate(newVal); }; - const onChange = event => { - const newVal = isNaN(event.target.value) ? 5 : Number(event.target.value); + const onChange = (event: React.FormEvent) => { + const target = event.target as HTMLInputElement; + const newVal = isNaN(+target.value) ? 5 : Number(target.value); setValue(newVal); validate(newVal); };