Skip to content

Commit abcd259

Browse files
NumberField default/min width (#1544)
* NumberField min width * Add min width * Fix side label * fix lint Co-authored-by: Robert Snow <[email protected]>
1 parent 85ecf65 commit abcd259

File tree

6 files changed

+44
-9
lines changed

6 files changed

+44
-9
lines changed

packages/@adobe/spectrum-css-temp/components/fieldlabel/index.css

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,12 @@ governing permissions and limitations under the License.
6363
* label will wrap within this width. The width of the whole field can be overridden by the user,
6464
* and this causes both the label and inner field to resize as well. */
6565
.spectrum-Field {
66+
--spectrum-field-default-width: var(--spectrum-component-single-line-width);
67+
6668
&.spectrum-Field--positionTop {
6769
display: inline-flex;
6870
flex-direction: column;
69-
width: var(--spectrum-component-single-line-width);
71+
width: var(--spectrum-field-default-width);
7072

7173
.spectrum-Field-field {
7274
/* If the user overrides the width of the field, propagate to the inner component */

packages/@adobe/spectrum-css-temp/components/stepper/index.css

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ governing permissions and limitations under the License.
2020
--spectrum-stepper-border-radius-reset: 0;
2121
--spectrum-stepper-border-size-reset: 0;
2222
--spectrum-stepper-border-size-default: 1px;
23+
--spectrum-stepper-input-min-width: var(--spectrum-component-single-line-height);
24+
--spectrum-stepper-default-width-desktop: calc(var(--spectrum-component-single-line-height) * 4);
25+
--spectrum-stepper-default-width-mobile: calc(var(--spectrum-component-single-line-height) * 5);
2326
}
2427

2528
.spectrum-Stepper {
@@ -30,12 +33,24 @@ governing permissions and limitations under the License.
3033
'field increment'
3134
'field decrement';
3235

33-
width: var(--spectrum-component-single-line-width);
36+
inline-size: var(--spectrum-stepper-default-width-desktop);
37+
min-inline-size: unset;
3438
line-height: 0;
3539
border-radius: var(--spectrum-border-radius);
3640
transition: border-color var(--spectrum-global-animation-duration-100) ease-in-out, box-shadow var(--spectrum-global-animation-duration-100) ease-in-out;
3741
}
3842

43+
.spectrum-Stepper-container {
44+
/* ensure we have higher specificity than .spectrum-Field */
45+
&.spectrum-Stepper-container {
46+
/* override the default width of the field container, only with labelPosition=top. */
47+
--spectrum-field-default-width: var(--spectrum-stepper-default-width-desktop);
48+
}
49+
50+
&.spectrum-Stepper-container--isMobile {
51+
--spectrum-field-default-width: var(--spectrum-stepper-default-width-mobile);
52+
}
53+
}
3954

4055
.spectrum-Stepper-button {
4156
@inherit: %spectrum-BaseButton;
@@ -92,7 +107,7 @@ governing permissions and limitations under the License.
92107
grid-area: field;
93108

94109
inline-size: unset;
95-
min-inline-size: unset;
110+
min-inline-size: var(--spectrum-stepper-input-min-width);
96111
}
97112

98113
.spectrum-Stepper--showStepper {
@@ -111,6 +126,7 @@ governing permissions and limitations under the License.
111126
grid-template-rows: auto;
112127
grid-template-columns: auto 1fr auto;
113128
grid-template-areas: 'decrement field increment';
129+
inline-size: var(--spectrum-stepper-default-width-mobile);
114130

115131
.spectrum-Stepper-input {
116132
border-start-start-radius: var(--spectrum-stepper-border-radius-reset);

packages/@react-spectrum/label/src/Field.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ function Field(props: SpectrumFieldProps, ref: RefObject<HTMLElement>) {
2828
children,
2929
labelProps,
3030
elementType,
31+
wrapperClassName,
3132
...otherProps
3233
} = props;
3334
let {styleProps} = useStyleProps(otherProps);
@@ -40,7 +41,8 @@ function Field(props: SpectrumFieldProps, ref: RefObject<HTMLElement>) {
4041
'spectrum-Field--positionTop': labelPosition === 'top',
4142
'spectrum-Field--positionSide': labelPosition === 'side'
4243
},
43-
styleProps.className
44+
styleProps.className,
45+
wrapperClassName
4446
);
4547

4648
children = React.cloneElement(children, mergeProps(children.props, {

packages/@react-spectrum/numberfield/src/NumberField.tsx

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,14 @@ function NumberField(props: SpectrumNumberFieldProps, ref: FocusableRef<HTMLElem
8585
<Field
8686
{...props as Omit<SpectrumNumberFieldProps, 'onChange'>}
8787
labelProps={labelProps}
88-
ref={domRef}>
88+
ref={domRef}
89+
wrapperClassName={classNames(
90+
stepperStyle,
91+
'spectrum-Stepper-container',
92+
{
93+
'spectrum-Stepper-container--isMobile': isMobile
94+
}
95+
)}>
8996
<NumberFieldInput
9097
{...props}
9198
groupProps={mergeProps(groupProps, hoverProps)}
@@ -122,8 +129,7 @@ const NumberFieldInput = React.forwardRef(function NumberFieldInput(props: Numbe
122129
autoFocus,
123130
isQuiet,
124131
hideStepper,
125-
validationState,
126-
label
132+
validationState
127133
} = props;
128134
let showStepper = !hideStepper;
129135

@@ -137,7 +143,7 @@ const NumberFieldInput = React.forwardRef(function NumberFieldInput(props: Numbe
137143
<div
138144
{...groupProps}
139145
ref={ref as RefObject<HTMLDivElement>}
140-
{...(label ? {style: {...style, width: '100%'}} : {style})}
146+
style={style}
141147
className={className}>
142148
<TextFieldBase
143149
UNSAFE_className={classNames(stepperStyle, 'spectrum-Stepper-field')}

packages/@react-spectrum/numberfield/stories/NumberField.stories.tsx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,10 @@ storiesOf('NumberField', module)
173173
'custom width no visible label',
174174
() => renderNoLabel({width: 'size-3000', isRequired: true, 'aria-label': 'Width'})
175175
)
176+
.add(
177+
'custom width, labelPosition=side',
178+
() => render({width: 'size-3000', labelPosition: 'side'})
179+
)
176180
.add(
177181
'controlled',
178182
() => <NumberFieldControlled />
@@ -184,6 +188,10 @@ storiesOf('NumberField', module)
184188
.add(
185189
'flexed',
186190
() => renderSet()
191+
)
192+
.add(
193+
'min width',
194+
() => render({width: 0})
187195
);
188196

189197
function render(props: any = {}) {

packages/@react-types/label/src/index.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,6 @@ export interface SpectrumLabelProps extends LabelProps, DOMProps, StyleProps, HT
3131
export interface SpectrumFieldProps extends SpectrumLabelProps {
3232
children: ReactElement,
3333
label?: ReactNode,
34-
labelProps: HTMLAttributes<HTMLElement>
34+
labelProps: HTMLAttributes<HTMLElement>,
35+
wrapperClassName?: string
3536
}

0 commit comments

Comments
 (0)