@react-spectrum/numberfield 3.10.4 → 3.11.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,196 +0,0 @@
1
- /*
2
- * Copyright 2020 Adobe. All rights reserved.
3
- * This file is licensed to you under the Apache License, Version 2.0 (the "License");
4
- * you may not use this file except in compliance with the License. You may obtain a copy
5
- * of the License at http://www.apache.org/licenses/LICENSE-2.0
6
- *
7
- * Unless required by applicable law or agreed to in writing, software distributed under
8
- * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9
- * OF ANY KIND, either express or implied. See the License for the specific language
10
- * governing permissions and limitations under the License.
11
- */
12
-
13
- import {AriaButtonProps} from '@react-types/button';
14
- import {classNames, useFocusableRef, useStyleProps} from '@react-spectrum/utils';
15
- import {Field} from '@react-spectrum/label';
16
- import {FocusableRef, RefObject} from '@react-types/shared';
17
- import {FocusRing} from '@react-aria/focus';
18
- import {mergeProps} from '@react-aria/utils';
19
- import {NumberFieldState, useNumberFieldState} from '@react-stately/numberfield';
20
- import React, {HTMLAttributes, InputHTMLAttributes, Ref, useRef} from 'react';
21
- import {SpectrumNumberFieldProps} from '@react-types/numberfield';
22
- import {StepButton} from './StepButton';
23
- import stepperStyle from '@adobe/spectrum-css-temp/components/stepper/vars.css';
24
- import {TextFieldBase} from '@react-spectrum/textfield';
25
- import {useFormProps} from '@react-spectrum/form';
26
- import {useHover} from '@react-aria/interactions';
27
- import {useLocale} from '@react-aria/i18n';
28
- import {useNumberField} from '@react-aria/numberfield';
29
- import {useProvider, useProviderProps} from '@react-spectrum/provider';
30
-
31
- /**
32
- * NumberFields allow users to enter a number, and increment or decrement the value using stepper buttons.
33
- */
34
- export const NumberField = React.forwardRef(function NumberField(props: SpectrumNumberFieldProps, ref: FocusableRef<HTMLElement>) {
35
- props = useProviderProps(props);
36
- props = useFormProps(props);
37
- let provider = useProvider();
38
- let {
39
- isQuiet,
40
- isReadOnly,
41
- isDisabled,
42
- hideStepper
43
- } = props;
44
-
45
- let {styleProps: style} = useStyleProps(props);
46
-
47
- let {locale} = useLocale();
48
- let state = useNumberFieldState({...props, locale});
49
- let inputRef = useRef<HTMLInputElement>(null);
50
- let domRef = useFocusableRef<HTMLElement>(ref, inputRef);
51
- let {
52
- groupProps,
53
- labelProps,
54
- inputProps,
55
- incrementButtonProps,
56
- decrementButtonProps,
57
- descriptionProps,
58
- errorMessageProps,
59
- isInvalid,
60
- validationErrors,
61
- validationDetails
62
- } = useNumberField(props, state, inputRef);
63
- let isMobile = provider.scale === 'large';
64
- let showStepper = !hideStepper;
65
-
66
- let {isHovered, hoverProps} = useHover({isDisabled});
67
-
68
- let validationState = props.validationState || (isInvalid ? 'invalid' : undefined);
69
- let className =
70
- classNames(
71
- stepperStyle,
72
- 'spectrum-Stepper',
73
- // because FocusRing won't pass along the className from Field, we have to handle that ourselves
74
- !props.label && style.className ? style.className : '',
75
- {
76
- 'spectrum-Stepper--isQuiet': isQuiet,
77
- 'is-disabled': isDisabled,
78
- 'spectrum-Stepper--readonly': isReadOnly,
79
- 'is-invalid': validationState === 'invalid' && !isDisabled,
80
- 'spectrum-Stepper--showStepper': showStepper,
81
- 'spectrum-Stepper--isMobile': isMobile,
82
- 'is-hovered': isHovered
83
- }
84
- );
85
-
86
- return (
87
- <Field
88
- {...props as Omit<SpectrumNumberFieldProps, 'onChange'>}
89
- descriptionProps={descriptionProps}
90
- errorMessageProps={errorMessageProps}
91
- isInvalid={isInvalid}
92
- validationErrors={validationErrors}
93
- validationDetails={validationDetails}
94
- labelProps={labelProps}
95
- ref={domRef}
96
- wrapperClassName={classNames(
97
- stepperStyle,
98
- 'spectrum-Stepper-container',
99
- {
100
- 'spectrum-Stepper-container--isMobile': isMobile
101
- }
102
- )}>
103
- <NumberFieldInput
104
- {...props}
105
- groupProps={mergeProps(groupProps, hoverProps)}
106
- inputProps={inputProps}
107
- inputRef={inputRef}
108
- incrementProps={incrementButtonProps}
109
- decrementProps={decrementButtonProps}
110
- className={className}
111
- style={style}
112
- state={state}
113
- validationState={validationState} />
114
- </Field>
115
- );
116
- });
117
-
118
-
119
- interface NumberFieldInputProps extends SpectrumNumberFieldProps {
120
- groupProps: HTMLAttributes<HTMLDivElement>,
121
- inputProps: InputHTMLAttributes<HTMLInputElement>,
122
- inputRef: RefObject<HTMLInputElement | HTMLTextAreaElement | null>,
123
- incrementProps: AriaButtonProps,
124
- decrementProps: AriaButtonProps,
125
- className?: string,
126
- style?: React.CSSProperties,
127
- state: NumberFieldState
128
- }
129
-
130
- const NumberFieldInput = React.forwardRef(function NumberFieldInput(props: NumberFieldInputProps, ref: Ref<HTMLDivElement>) {
131
- let {
132
- groupProps,
133
- inputProps,
134
- inputRef,
135
- incrementProps,
136
- decrementProps,
137
- className,
138
- style,
139
- autoFocus,
140
- isQuiet,
141
- isDisabled,
142
- hideStepper,
143
- validationState,
144
- name,
145
- state
146
- } = props;
147
- let showStepper = !hideStepper;
148
-
149
- return (
150
- <FocusRing
151
- within
152
- isTextInput
153
- focusClass={classNames(stepperStyle, 'is-focused')}
154
- focusRingClass={classNames(stepperStyle, 'focus-ring')}
155
- autoFocus={autoFocus}>
156
- <div
157
- {...groupProps}
158
- ref={ref}
159
- style={style}
160
- className={className}>
161
- <TextFieldBase
162
- UNSAFE_className={
163
- classNames(
164
- stepperStyle,
165
- 'spectrum-Stepper-field'
166
- )
167
- }
168
- inputClassName={
169
- classNames(
170
- stepperStyle,
171
- 'spectrum-Stepper-input'
172
- )
173
- }
174
- validationIconClassName={
175
- classNames(
176
- stepperStyle,
177
- 'spectrum-Stepper-icon'
178
- )
179
- }
180
- isQuiet={isQuiet}
181
- inputRef={inputRef}
182
- validationState={validationState}
183
- inputProps={inputProps}
184
- isDisabled={isDisabled}
185
- disableFocusRing />
186
- {showStepper &&
187
- <>
188
- <StepButton direction="up" isQuiet={isQuiet} {...incrementProps} />
189
- <StepButton direction="down" isQuiet={isQuiet} {...decrementProps} />
190
- </>
191
- }
192
- {name && <input type="hidden" name={name} form={props.form} value={isNaN(state.numberValue) ? '' : state.numberValue} />}
193
- </div>
194
- </FocusRing>
195
- );
196
- });
@@ -1,81 +0,0 @@
1
- /*
2
- * Copyright 2020 Adobe. All rights reserved.
3
- * This file is licensed to you under the Apache License, Version 2.0 (the "License");
4
- * you may not use this file except in compliance with the License. You may obtain a copy
5
- * of the License at http://www.apache.org/licenses/LICENSE-2.0
6
- *
7
- * Unless required by applicable law or agreed to in writing, software distributed under
8
- * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9
- * OF ANY KIND, either express or implied. See the License for the specific language
10
- * governing permissions and limitations under the License.
11
- */
12
-
13
- import Add from '@spectrum-icons/workflow/Add';
14
- import {AriaButtonProps} from '@react-types/button';
15
- import ChevronDownSmall from '@spectrum-icons/ui/ChevronDownSmall';
16
- import ChevronUpSmall from '@spectrum-icons/ui/ChevronUpSmall';
17
- import {classNames, useFocusableRef} from '@react-spectrum/utils';
18
- import {FocusableRef} from '@react-types/shared';
19
- import {FocusRing} from '@react-aria/focus';
20
- import {mergeProps} from '@react-aria/utils';
21
- import React, {ReactElement} from 'react';
22
- import Remove from '@spectrum-icons/workflow/Remove';
23
- import stepperStyle from '@adobe/spectrum-css-temp/components/stepper/vars.css';
24
- import {useButton} from '@react-aria/button';
25
- import {useHover} from '@react-aria/interactions';
26
- import {useProvider, useProviderProps} from '@react-spectrum/provider';
27
-
28
- interface StepButtonProps extends AriaButtonProps {
29
- isQuiet?: boolean,
30
- direction: 'up' | 'down'
31
- }
32
-
33
- /**
34
- * Buttons for NumberField.
35
- */
36
- export const StepButton = React.forwardRef(function StepButton(props: StepButtonProps, ref: FocusableRef<HTMLDivElement>) {
37
- props = useProviderProps(props);
38
- let {scale} = useProvider();
39
- let {direction, isDisabled, isQuiet} = props;
40
- let domRef = useFocusableRef(ref);
41
- /**
42
- * Must use div for now because Safari pointer event bugs on disabled form elements.
43
- * Link https://bugs.webkit.org/show_bug.cgi?id=219188.
44
- */
45
- let {buttonProps, isPressed} = useButton({...props, elementType: 'div'}, domRef);
46
- let {hoverProps, isHovered} = useHover(props);
47
- return (
48
- <FocusRing focusRingClass={classNames(stepperStyle, 'focus-ring')}>
49
- <div
50
- className={
51
- classNames(
52
- stepperStyle,
53
- 'spectrum-Stepper-button',
54
- {
55
- 'spectrum-Stepper-button--stepUp': direction === 'up',
56
- 'spectrum-Stepper-button--stepDown': direction === 'down',
57
- 'spectrum-Stepper-button--isQuiet': isQuiet,
58
- 'is-hovered': isHovered,
59
- 'is-active': isPressed,
60
- 'is-disabled': isDisabled
61
- }
62
- )
63
- }
64
- {...mergeProps(hoverProps, buttonProps)}
65
- ref={domRef}>
66
- {direction === 'up' && scale === 'large' &&
67
- <Add UNSAFE_className={classNames(stepperStyle, 'spectrum-Stepper-button-icon', 'spectrum-Stepper-stepUpIcon')} size="S" />
68
- }
69
- {direction === 'up' && scale === 'medium' &&
70
- <ChevronUpSmall UNSAFE_className={classNames(stepperStyle, 'spectrum-Stepper-button-icon', 'spectrum-Stepper-stepUpIcon')} />
71
- }
72
- {direction === 'down' && scale === 'large' &&
73
- <Remove UNSAFE_className={classNames(stepperStyle, 'spectrum-Stepper-button-icon', 'spectrum-Stepper-stepDownIcon')} size="S" />
74
- }
75
- {direction === 'down' && scale === 'medium' &&
76
- <ChevronDownSmall UNSAFE_className={classNames(stepperStyle, 'spectrum-Stepper-button-icon', 'spectrum-Stepper-stepDownIcon')} />
77
- }
78
- </div>
79
- </FocusRing>
80
- );
81
- }) as (props: StepButtonProps & {ref?: FocusableRef<HTMLDivElement>}) => ReactElement;