Skip to content

Commit b233617

Browse files
committed
Re-introduce custom input size for numeric inputs (#281)
1 parent 33b796c commit b233617

File tree

6 files changed

+84
-48
lines changed

6 files changed

+84
-48
lines changed

src/lib/components/ui/TextField/README.mdx

Lines changed: 58 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,7 @@ See [API](#api) for all available options.
4141
- **Beware of the `number` input type:** it may not be always what you want.
4242
Not all number-like values are actually numbers, e.g. phone numbers, credit
4343
card numbers, or business IDs. In such cases use the most appropriate input
44-
type (probably `text` or `tel`) along with the
45-
[`pattern` attribute](https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/pattern)
44+
type (probably `text` or `tel`) along with the [`pattern` attribute][pattern]
4645
to improve the input experience for touch users.
4746

4847
- Use **short and descriptive labels**, ideally nouns rather than seemingly
@@ -64,9 +63,8 @@ See [API](#api) for all available options.
6463
entered. Be positive and focus on solutions to make the error message helpful.
6564

6665
- When asking users for their contact information or other personal information,
67-
make use of the
68-
[`autocomplete` attribute](https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/autocomplete)
69-
to help them fill the form faster.
66+
make use of the [`autocomplete` attribute][autocomplete] to help them fill the
67+
form faster.
7068

7169
## Design Variants
7270

@@ -183,14 +181,16 @@ individual text fields** using the `inputSize` property. It (obviously) sets the
183181
`size` attribute of the `input` element and is further picked up by CSS to
184182
normalize rendering across browsers.
185183

186-
👉 Remember that the
187-
[`size`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/text#size)
188-
HTML attribute **does not limit on how many characters** the user can enter. Use
189-
the `maxlength` attribute to achieve that effect.
184+
👉 Remember that the [`size`][size] and [`max`][max] HTML attributes **don't
185+
limit on how many characters** the user can enter. Use the `maxlength`
186+
attribute to achieve that effect (doesn't work for `number` input type though).
190187

191-
👉 Note that according to the HTML specification, the `inputSize` option (which
192-
is translated as `size` attribute of the input element) is
193-
[not available for the `number` input type](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/number#Controlling_input_size).
188+
👉 Note that according to the HTML specification, the `size` attribute (invoked
189+
by `inputSize` API option) is
190+
[not available for `number` input type][input-number-size]. TextField supports
191+
`inputSize` option for all types of inputs, so you can use it whenever you find
192+
it suitable. Just keep in mind the `size` attribute will not be present for
193+
numeric inputs.
194194

195195
<Playground>
196196
<TextField
@@ -202,6 +202,17 @@ is translated as `size` attribute of the input element) is
202202
label="Title"
203203
variant="filled"
204204
/>
205+
<TextField
206+
inputSize={3}
207+
label="Age"
208+
type="number"
209+
/>
210+
<TextField
211+
inputSize={3}
212+
label="Age"
213+
type="number"
214+
variant="filled"
215+
/>
205216
</Playground>
206217

207218
## Forwarding HTML Attributes
@@ -224,15 +235,26 @@ interfere with [component's API](#api) are forwarded to the native HTML input.
224235
minLength={3}
225236
maxLength={80}
226237
/>
238+
<TextField
239+
inputSize={3}
240+
min={13}
241+
max={120}
242+
label="Age"
243+
type="number"
244+
/>
245+
<TextField
246+
inputSize={3}
247+
min={13}
248+
max={120}
249+
label="Age"
250+
type="number"
251+
variant="filled"
252+
/>
227253
</Playground>
228254

229255
👉 Refer to the MDN reference for the full list of supported attributes of the
230-
[text](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/text),
231-
[email](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/email),
232-
[number](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/number),
233-
[tel](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/tel), and
234-
[password](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/password)
235-
input types.
256+
[text][input-text], [email][input-email], [number][input-number],
257+
[tel][input-tel], and [password][input-password] input types.
236258

237259
## Invisible Label
238260

@@ -355,9 +377,9 @@ filled.
355377
</Playground>
356378

357379
Keep in mind that **long help texts don't play well with small input sizes,**
358-
especially in a vertical layout. To fix this at least for the horizontal layout,
359-
the help text expands over the full field width when the desired input width
360-
(based on `inputSize` and `max` options) is 10 characters or smaller.
380+
especially in vertical layout. To fix this at least for horizontal layout, help
381+
text expands over the full field width when the desired input width (based on
382+
`inputSize` option) is 10 characters or smaller.
361383

362384
<Playground>
363385
<TextField
@@ -378,6 +400,7 @@ the help text expands over the full field width when the desired input width
378400
layout="horizontal"
379401
min={13}
380402
max={120}
403+
inputSize={3}
381404
type="number"
382405
helpText="How old do you see yourself?"
383406
/>
@@ -386,6 +409,7 @@ the help text expands over the full field width when the desired input width
386409
layout="horizontal"
387410
min={13}
388411
max={120}
412+
inputSize={3}
389413
variant="filled"
390414
type="number"
391415
helpText="How old do you see yourself?"
@@ -476,5 +500,16 @@ This is useful mainly to improve component's
476500

477501
## Theming
478502

479-
Head to [Forms Theming](/customize/theming/forms) to see shared form theming
480-
options.
503+
Head to [Forms Theming][theming-forms] to see shared form theming options.
504+
505+
[pattern]: https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/pattern
506+
[autocomplete]: https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/autocomplete
507+
[size]: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/text#size
508+
[max]: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/number#max
509+
[input-number-size]: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/number#Controlling_input_size
510+
[input-text]: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/text
511+
[input-email]: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/email
512+
[input-number]: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/number
513+
[input-tel]: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/tel
514+
[input-password]: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/password
515+
[theming-forms]: /customize/theming/forms

src/lib/components/ui/TextField/TextField.jsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ export const TextField = ({
4040
fullWidth ? styles.isRootFullWidth : '',
4141
hasSmallInput ? styles.hasRootSmallInput : '',
4242
inFormLayout ? styles.isRootInFormLayout : '',
43+
inputSize ? styles.hasRootCustomInputSize : '',
4344
layout === 'horizontal' ? styles.rootLayoutHorizontal : styles.rootLayoutVertical,
4445
disabled ? styles.isRootDisabled : '',
4546
required ? styles.isRootRequired : '',
@@ -156,7 +157,7 @@ TextField.propTypes = {
156157
*/
157158
inFormLayout: PropTypes.bool,
158159
/**
159-
* Width of the input field, translated as `size` attribute of the input.
160+
* Width of the input field. Translated as `size` attribute for input types other than `number`.
160161
*/
161162
inputSize: PropTypes.number,
162163
/**

src/lib/components/ui/TextField/TextField.scss

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020

2121
.input {
2222
@include box-field-elements.input();
23-
@include box-field-elements.input-text();
2423
}
2524

2625
.bottomLine {
@@ -32,6 +31,10 @@
3231
@include foundation.help-text();
3332
}
3433

34+
.hasRootCustomInputSize .input {
35+
@include box-field-elements.input-size();
36+
}
37+
3538
.isRootRequired .label {
3639
@include foundation.label-required();
3740
}

src/lib/components/ui/TextField/__tests__/__snapshots__/TextField.test.jsx.snap

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
exports[`rendering renders correctly mandatory props only 1`] = `
44
<label
5-
className="root rootLayoutVertical rootSizeMedium rootVariantOutline"
5+
className="root rootLayoutVertical rootSizeMedium rootVariantOutline"
66
>
77
<div
88
className="label "
@@ -31,7 +31,7 @@ exports[`rendering renders correctly mandatory props only 1`] = `
3131

3232
exports[`rendering renders correctly the number input type 1`] = `
3333
<label
34-
className="root rootLayoutVertical rootSizeMedium rootVariantOutline"
34+
className="root rootLayoutVertical rootSizeMedium rootVariantOutline"
3535
>
3636
<div
3737
className="label "
@@ -60,7 +60,7 @@ exports[`rendering renders correctly the number input type 1`] = `
6060

6161
exports[`rendering renders correctly the number input type with small input 1`] = `
6262
<label
63-
className="root hasRootSmallInput rootLayoutVertical rootSizeMedium rootVariantOutline"
63+
className="root hasRootSmallInput hasRootCustomInputSize rootLayoutVertical rootSizeMedium rootVariantOutline"
6464
style={
6565
Object {
6666
"--rui-custom-input-size": 1,
@@ -94,7 +94,7 @@ exports[`rendering renders correctly the number input type with small input 1`]
9494

9595
exports[`rendering renders correctly with all props 1`] = `
9696
<label
97-
className="root isRootFullWidth isRootInFormLayout rootLayoutHorizontal isRootDisabled isRootRequired rootSizeLarge isRootStateInvalid rootVariantFilled"
97+
className="root isRootFullWidth isRootInFormLayout hasRootCustomInputSize rootLayoutHorizontal isRootDisabled isRootRequired rootSizeLarge isRootStateInvalid rootVariantFilled"
9898
htmlFor="test"
9999
id="test__label"
100100
style={
@@ -148,7 +148,7 @@ exports[`rendering renders correctly with all props 1`] = `
148148

149149
exports[`rendering renders correctly with custom props 1`] = `
150150
<label
151-
className="root rootLayoutVertical rootSizeMedium rootVariantOutline"
151+
className="root rootLayoutVertical rootSizeMedium rootVariantOutline"
152152
>
153153
<div
154154
className="label "
@@ -179,7 +179,7 @@ exports[`rendering renders correctly with custom props 1`] = `
179179

180180
exports[`rendering renders correctly with hidden label 1`] = `
181181
<label
182-
className="root rootLayoutVertical rootSizeMedium rootVariantOutline"
182+
className="root rootLayoutVertical rootSizeMedium rootVariantOutline"
183183
>
184184
<div
185185
className="label isLabelHidden"
@@ -208,7 +208,7 @@ exports[`rendering renders correctly with hidden label 1`] = `
208208

209209
exports[`rendering renders correctly with small input 1`] = `
210210
<label
211-
className="root hasRootSmallInput rootLayoutVertical rootSizeMedium rootVariantOutline"
211+
className="root hasRootSmallInput hasRootCustomInputSize rootLayoutVertical rootSizeMedium rootVariantOutline"
212212
style={
213213
Object {
214214
"--rui-custom-input-size": 5,

src/lib/styles/settings/_form-fields.scss

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ $box-input-font-family: typography.$font-family-base;
1818
$box-input-font-weight: map.get(typography.$font-weight-values, regular);
1919
$box-input-line-height: 1.5rem; // 1.
2020
$box-field-caret-size: 2.25rem;
21-
$box-field-input-number-arrows-width: 1.25rem;
21+
$box-field-input-number-arrows-width: 1.5rem;
2222
$box-field-bottom-line-height: 2px;
2323

2424
$inline-field-inner-gap: spacing.of(2);

src/lib/styles/tools/form-fields/_box-field-elements.scss

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -43,21 +43,18 @@
4343
}
4444
}
4545

46-
@mixin input-text() {
47-
&[max],
48-
&[size] {
49-
width:
50-
calc(
51-
1ch * var(--rui-custom-input-size)
52-
+ var(--rui-local-arrows-width, 0)
53-
+ 2 * var(--rui-local-padding-x)
54-
+ 2 * #{theme.$box-border-width}
55-
); // 2.
56-
57-
min-width: auto; // 2.
58-
}
59-
60-
&[max] {
46+
@mixin input-size() {
47+
width:
48+
calc(
49+
1ch * var(--rui-custom-input-size)
50+
+ var(--rui-local-arrows-width, 0)
51+
+ 2 * var(--rui-local-padding-x)
52+
+ 2 * #{theme.$box-border-width}
53+
); // 2.
54+
55+
min-width: auto; // 2.
56+
57+
&[type="number"] {
6158
--rui-local-arrows-width: #{settings.$box-field-input-number-arrows-width};
6259
}
6360
}

0 commit comments

Comments
 (0)