import * as React from 'react'; import classNames from 'classnames'; import { useStepper } from './useStepper'; import { FieldDescription, FieldErrorMessage } from '../Field'; import { FieldGroup } from '../FieldGroup'; import { FieldGroupIconButton } from '../FieldGroupIcon'; import { Flex } from '../Flex'; import { IconAdd, IconRemove } from '../Icon/internal'; import { Input } from '../Input'; import { Label } from '../Label'; import { ForwardRefPrimitive, Primitive } from '../types/view'; import { BaseStepperFieldProps, StepperFieldProps, } from '../types/stepperField'; import { classNameModifier, classNameModifierByFlag } from '../shared/utils'; import { ComponentClassNames, ComponentText } from '../shared/constants'; import { splitPrimitiveProps } from '../utils/splitPrimitiveProps'; import { useStableId } from '../utils/useStableId'; export const DECREASE_ICON = 'decrease-icon'; export const INCREASE_ICON = 'increase-icon'; const StepperFieldPrimitive: Primitive = ( props, ref ) => { const { className, // destructure to prevent `defaultValue` from being passed to underlying `Input` via `_rest` defaultValue, descriptiveText, errorMessage, hasError = false, id, inputStyles, isDisabled, isReadOnly, isRequired, increaseButtonLabel = ComponentText.StepperField.increaseButtonLabel, decreaseButtonLabel = ComponentText.StepperField.decreaseButtonLabel, label, labelHidden = false, // destructure to prevent `onStepChange` from being passed to underlying `Input` via `_rest` onStepChange, size, testId, // this is only required in useStepper hook but deconstruct here to remove its existence in rest value: controlledValue, variation, ..._rest } = props; const fieldId = useStableId(id); const descriptionId = useStableId(); const ariaDescribedBy = descriptiveText ? descriptionId : undefined; const { styleProps, rest } = splitPrimitiveProps(_rest); const { step, value, inputValue, handleDecrease, handleIncrease, handleOnBlur, handleOnChange, handleOnWheel, setInputValue, shouldDisableDecreaseButton, shouldDisableIncreaseButton, } = useStepper({ ...props, defaultValue, onStepChange }); React.useEffect(() => { const isControlled = controlledValue !== undefined; if (isControlled) { setInputValue(controlledValue); } }, [controlledValue, setInputValue]); return ( } outerEndComponent={ } > ); }; /** * [📖 Docs](https://ui.docs.amplify.aws/react/components/stepperfield) */ export const StepperField: ForwardRefPrimitive = React.forwardRef(StepperFieldPrimitive); StepperField.displayName = 'StepperField';