Skip to content

Commit

Permalink
[EuiRange/EuiDualRange] Improve UX of input widths on EuiRange/EuiDua…
Browse files Browse the repository at this point in the history
…lRange

- account for `invalid` icon (which now automatically displays for out of range inputs)

- dynamically adjust width based on # of characters in actual input

- adjust width affordances based on `compressed`

- width changes are especially a readability improvement in Firefox
  • Loading branch information
cee-chen committed Apr 11, 2023
1 parent 5f08352 commit e094b78
Show file tree
Hide file tree
Showing 7 changed files with 203 additions and 23 deletions.
16 changes: 12 additions & 4 deletions src/components/form/range/__snapshots__/dual_range.test.tsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,14 @@ exports[`EuiDualRange input props can be applied to min and max inputs 1`] = `
class="euiFormControlLayout__childrenWrapper"
>
<input
aria-invalid="false"
aria-label="Min value"
class="euiFieldNumber euiRangeInput euiRangeInput--min emotion-euiRangeInput"
max="8"
min="1"
name="name-minValue"
step="1"
style="inline-size:3.6em"
style="inline-size:calc(12px + 1ch + 2em + 0px)"
type="number"
value="1"
/>
Expand Down Expand Up @@ -112,13 +113,14 @@ exports[`EuiDualRange input props can be applied to min and max inputs 1`] = `
class="euiFormControlLayout__childrenWrapper"
>
<input
aria-invalid="false"
aria-label="Max value"
class="euiFieldNumber euiRangeInput euiRangeInput--max emotion-euiRangeInput"
max="10"
min="1"
name="name-maxValue"
step="1"
style="inline-size:3.6em"
style="inline-size:calc(12px + 1ch + 2em + 0px)"
type="number"
value="8"
/>
Expand Down Expand Up @@ -321,13 +323,14 @@ exports[`EuiDualRange props inputs should render 1`] = `
class="euiFormControlLayout__childrenWrapper"
>
<input
aria-invalid="false"
aria-label="aria-label"
class="euiFieldNumber euiRangeInput euiRangeInput--min emotion-euiRangeInput"
max="8"
min="0"
name="name-minValue"
step="1"
style="inline-size:4.4em"
style="inline-size:calc(12px + 1ch + 2em + 0px)"
type="number"
value="1"
/>
Expand Down Expand Up @@ -372,13 +375,14 @@ exports[`EuiDualRange props inputs should render 1`] = `
class="euiFormControlLayout__childrenWrapper"
>
<input
aria-invalid="false"
aria-label="aria-label"
class="euiFieldNumber euiRangeInput euiRangeInput--max emotion-euiRangeInput"
max="100"
min="1"
name="name-maxValue"
step="1"
style="inline-size:4.4em"
style="inline-size:calc(12px + 1ch + 2em + 0px)"
type="number"
value="8"
/>
Expand Down Expand Up @@ -528,6 +532,7 @@ exports[`EuiDualRange props loading should display when showInput="inputWithPopo
class="euiFormControlLayout__childrenWrapper"
>
<input
aria-invalid="false"
class="euiFieldNumber euiFormControlLayoutDelimited__input emotion-euiRangeInput"
max="8"
min="0"
Expand All @@ -547,6 +552,7 @@ exports[`EuiDualRange props loading should display when showInput="inputWithPopo
</span>
</div>
<input
aria-invalid="false"
class="euiFieldNumber euiFormControlLayoutDelimited__input emotion-euiRangeInput"
max="100"
min="1"
Expand Down Expand Up @@ -616,6 +622,7 @@ exports[`EuiDualRange props slider should display in popover 1`] = `
class="euiFormControlLayout__childrenWrapper"
>
<input
aria-invalid="false"
class="euiFieldNumber euiFormControlLayoutDelimited__input emotion-euiRangeInput"
max="8"
min="0"
Expand All @@ -635,6 +642,7 @@ exports[`EuiDualRange props slider should display in popover 1`] = `
</span>
</div>
<input
aria-invalid="false"
class="euiFieldNumber euiFormControlLayoutDelimited__input emotion-euiRangeInput"
max="100"
min="1"
Expand Down
5 changes: 4 additions & 1 deletion src/components/form/range/__snapshots__/range.test.tsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,7 @@ exports[`EuiRange props input should render 1`] = `
class="euiFormControlLayout__childrenWrapper"
>
<input
aria-invalid="false"
aria-label="aria-label"
class="euiFieldNumber euiRangeInput euiRangeInput--max emotion-euiRangeInput"
data-test-subj="test subject string"
Expand All @@ -263,7 +264,7 @@ exports[`EuiRange props input should render 1`] = `
min="0"
name="name"
step="1"
style="inline-size:4.4em"
style="inline-size:calc(12px + 1ch + 2em + 0px)"
type="number"
value="8"
/>
Expand Down Expand Up @@ -354,6 +355,7 @@ exports[`EuiRange props loading should display when showInput="inputWithPopover"
class="euiFormControlLayout__childrenWrapper"
>
<input
aria-invalid="false"
aria-label="aria-label"
class="euiFieldNumber euiRangeInput euiRangeInput--max emotion-euiRangeInput euiFormControlLayout--1icons euiFieldNumber-isLoading"
data-test-subj="test subject string"
Expand Down Expand Up @@ -426,6 +428,7 @@ exports[`EuiRange props slider should display in popover 1`] = `
class="euiFormControlLayout__childrenWrapper"
>
<input
aria-invalid="false"
aria-label="aria-label"
class="euiFieldNumber euiRangeInput euiRangeInput--max emotion-euiRangeInput"
data-test-subj="test subject string"
Expand Down
21 changes: 21 additions & 0 deletions src/components/form/range/__snapshots__/range_input.test.tsx.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`EuiRangeInput renders 1`] = `
<div
class="euiFormControlLayout"
>
<div
class="euiFormControlLayout__childrenWrapper"
>
<input
aria-invalid="false"
class="euiFieldNumber euiRangeInput euiRangeInput--max emotion-euiRangeInput"
max="100"
min="0"
style="inline-size: calc(12px + 1ch + 2em + 0px);"
type="number"
value="0"
/>
</div>
</div>
`;
3 changes: 0 additions & 3 deletions src/components/form/range/dual_range.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,6 @@ export class EuiDualRangeClass extends Component<

const { id } = this.state;

const digitTolerance = Math.max(String(min).length, String(max).length);
const showInputOnly = showInput === 'inputWithPopover';
const canShowDropdown = showInputOnly && !readOnly && !disabled;

Expand All @@ -479,7 +478,6 @@ export class EuiDualRangeClass extends Component<
aria-label={this.props['aria-label']}
{...minInputProps}
// Non-overridable props
digitTolerance={digitTolerance}
side="min"
min={min}
max={Number(this.upperValue)}
Expand Down Expand Up @@ -510,7 +508,6 @@ export class EuiDualRangeClass extends Component<
aria-label={this.props['aria-label']}
{...maxInputProps}
// Non-overridable props
digitTolerance={digitTolerance}
side="max"
min={Number(this.lowerValue)}
max={max}
Expand Down
2 changes: 0 additions & 2 deletions src/components/form/range/range.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,6 @@ export class EuiRangeClass extends Component<

const { id } = this.state;

const digitTolerance = Math.max(String(min).length, String(max).length);
const showInputOnly = showInput === 'inputWithPopover';
const canShowDropdown = showInputOnly && !readOnly && !disabled;

Expand All @@ -156,7 +155,6 @@ export class EuiRangeClass extends Component<
id={id}
min={min}
max={max}
digitTolerance={digitTolerance}
step={step}
value={value}
readOnly={readOnly}
Expand Down
114 changes: 114 additions & 0 deletions src/components/form/range/range_input.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import React from 'react';
import { render } from '../../../test/rtl';
import { shouldRenderCustomStyles } from '../../../test/internal';

import { EuiRangeInput } from './range_input';

const requiredProps = {
min: 0,
max: 100,
value: 0,
onChange: () => {},
};

describe('EuiRangeInput', () => {
shouldRenderCustomStyles(<EuiRangeInput {...requiredProps} />);

it('renders', () => {
const { container } = render(<EuiRangeInput {...requiredProps} />);
expect(container.firstChild).toMatchSnapshot();
});

describe('widthStyle', () => {
it('does not set a width style if the `autoSize` is set to false', () => {
const { container } = render(
<EuiRangeInput {...requiredProps} autoSize={false} />
);
const widthStyle = container
.querySelector('.euiRangeInput')
?.getAttribute('style');

expect(widthStyle).toBeFalsy();
});

it('increases input character width dynamically based on value', () => {
const { rerender, container } = render(
<EuiRangeInput {...requiredProps} value="0" />
);
const getWidthStyle = () =>
container.querySelector('.euiRangeInput')?.getAttribute('style');

expect(getWidthStyle()).toMatchInlineSnapshot(
'"inline-size: calc(12px + 1ch + 2em + 0px);"'
);

rerender(<EuiRangeInput {...requiredProps} value="10" />);
expect(getWidthStyle()).toMatchInlineSnapshot(
'"inline-size: calc(12px + 2ch + 2em + 0px);"'
);

rerender(<EuiRangeInput {...requiredProps} value="100" />);
expect(getWidthStyle()).toMatchInlineSnapshot(
'"inline-size: calc(12px + 3ch + 2em + 0px);"'
);

rerender(<EuiRangeInput {...requiredProps} value="1000" />);
expect(getWidthStyle()).toMatchInlineSnapshot(
'"inline-size: calc(12px + 4ch + 2em + 22px);"'
);

// Should not go above 4 characters in width
rerender(<EuiRangeInput {...requiredProps} value="10000" />);
expect(getWidthStyle()).toMatchInlineSnapshot(
'"inline-size: calc(12px + 4ch + 2em + 22px);"'
);
});

test('compressed', () => {
const { container } = render(
<EuiRangeInput {...requiredProps} compressed />
);
const widthStyle = container
.querySelector('.euiRangeInput')
?.getAttribute('style');

expect(widthStyle).toMatchInlineSnapshot(
'"inline-size: calc(8px + 1ch + 2em + 0px);"'
);
});

test('invalid', () => {
const { container } = render(
<EuiRangeInput {...requiredProps} isInvalid />
);
const widthStyle = container
.querySelector('.euiRangeInput')
?.getAttribute('style');

expect(widthStyle).toMatchInlineSnapshot(
'"inline-size: calc(12px + 1ch + 2em + 22px);"'
);
});

test('invalid + compressed', () => {
const { container } = render(
<EuiRangeInput {...requiredProps} isInvalid compressed />
);
const widthStyle = container
.querySelector('.euiRangeInput')
?.getAttribute('style');

expect(widthStyle).toMatchInlineSnapshot(
'"inline-size: calc(8px + 1ch + 2em + 18px);"'
);
});
});
});
Loading

0 comments on commit e094b78

Please sign in to comment.