/* * Copyright 2022 Adobe. All rights reserved. * This file is licensed to you under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. You may obtain a copy * of the License at http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software distributed under * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS * OF ANY KIND, either express or implied. See the License for the specific language * governing permissions and limitations under the License. */ import {AriaNumberFieldProps, useNumberField} from 'react-aria/useNumberField'; import {ButtonContext} from './Button'; import { ClassNameOrFunction, ContextValue, dom, Provider, RACValidation, removeDataAttributes, RenderProps, SlotProps, useContextProps, useRenderProps, useSlot, useSlottedContext } from './utils'; import {FieldErrorContext} from './FieldError'; import {filterDOMProps} from 'react-aria/filterDOMProps'; import {FormContext} from './Form'; import {forwardRefType, GlobalDOMAttributes, InputDOMProps} from '@react-types/shared'; import {GroupContext} from './Group'; import {InputContext} from './Input'; import {LabelContext} from './Label'; import {NumberFieldState, useNumberFieldState} from 'react-stately/useNumberFieldState'; import React, {createContext, ForwardedRef, forwardRef, useRef} from 'react'; import {TextContext} from './Text'; import {useLocale} from 'react-aria/I18nProvider'; export interface NumberFieldRenderProps { /** * Whether the number field is disabled. * * @selector [data-disabled] */ isDisabled: boolean; /** * Whether the number field is invalid. * * @selector [data-invalid] */ isInvalid: boolean; /** * Whether the number field is read only. * * @selector [data-readonly] */ isReadOnly: boolean; /** * Whether the number field is required. * * @selector [data-required] */ isRequired: boolean; /** * State of the number field. */ state: NumberFieldState; } export interface NumberFieldProps extends Omit< AriaNumberFieldProps, | 'label' | 'placeholder' | 'description' | 'errorMessage' | 'validationState' | 'validationBehavior' >, RACValidation, InputDOMProps, RenderProps, SlotProps, GlobalDOMAttributes { /** * The CSS [className](https://developer.mozilla.org/en-US/docs/Web/API/Element/className) for the * element. A function may be provided to compute the class based on component state. * * @default 'react-aria-NumberField' */ className?: ClassNameOrFunction; } export const NumberFieldContext = createContext>(null); export const NumberFieldStateContext = createContext(null); /** * A number field allows a user to enter a number, and increment or decrement the value using * stepper buttons. */ export const NumberField = /*#__PURE__*/ (forwardRef as forwardRefType)(function NumberField( props: NumberFieldProps, ref: ForwardedRef ) { [props, ref] = useContextProps(props, ref, NumberFieldContext); let {validationBehavior: formValidationBehavior} = useSlottedContext(FormContext) || {}; let validationBehavior = props.validationBehavior ?? formValidationBehavior ?? 'native'; let {locale} = useLocale(); let state = useNumberFieldState({ ...props, locale, validationBehavior }); let inputRef = useRef(null); let [labelRef, label] = useSlot(!props['aria-label'] && !props['aria-labelledby']); let { labelProps, groupProps, inputProps, incrementButtonProps, decrementButtonProps, descriptionProps, errorMessageProps, ...validation } = useNumberField( { ...removeDataAttributes(props), label, validationBehavior }, state, inputRef ); let renderProps = useRenderProps({ ...props, values: { state, isDisabled: props.isDisabled || false, isInvalid: validation.isInvalid || false, isRequired: props.isRequired || false, isReadOnly: props.isReadOnly || false }, defaultClassName: 'react-aria-NumberField' }); let DOMProps = filterDOMProps(props, {global: true}); delete DOMProps.id; return ( {props.name && ( )} ); });