Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/warm-cars-fry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@shopify/polaris': patch
---

Added `onBlur` prop to numerical steppers (`Spinner` component of `TextField`) to remove multi focus issue in `TextField`.
25 changes: 18 additions & 7 deletions polaris-react/src/components/TextField/TextField.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,28 @@ export function Default() {

export function Number() {
const [value, setValue] = useState('1');
const [value1, setValue1] = useState('1');

const handleChange = useCallback((newValue) => setValue(newValue), []);
const handleChange1 = useCallback((newValue) => setValue1(newValue), []);

return (
<TextField
label="Quantity"
type="number"
value={value}
onChange={handleChange}
autoComplete="off"
/>
<Stack vertical>
<TextField
label="First Quantity"
type="number"
value={value}
onChange={handleChange}
autoComplete="off"
/>
<TextField
label="Second Quantity"
type="number"
value={value1}
onChange={handleChange1}
autoComplete="off"
/>
</Stack>
);
}

Expand Down
17 changes: 9 additions & 8 deletions polaris-react/src/components/TextField/TextField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,7 @@ export function TextField({
onMouseDown={handleButtonPress}
onMouseUp={handleButtonRelease}
ref={spinnerRef}
onBlur={handleOnBlur}
/>
) : null;

Expand Down Expand Up @@ -477,14 +478,6 @@ export function TextField({
}
};

const handleOnBlur = (event: React.FocusEvent) => {
setFocus(false);

if (onBlur) {
onBlur(event);
}
};

const input = createElement(multiline ? 'textarea' : 'input', {
name,
id,
Expand Down Expand Up @@ -636,6 +629,14 @@ export function TextField({
event.preventDefault();
}

function handleOnBlur(event: React.FocusEvent) {
setFocus(false);

if (onBlur) {
onBlur(event);
}
}

function isInput(target: HTMLElement | EventTarget) {
return (
target instanceof HTMLElement &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ export interface SpinnerProps {
onClick?(event: React.MouseEvent): void;
onMouseDown(onChange: HandleStepFn): void;
onMouseUp(): void;
onBlur(event: React.FocusEvent): void;
}

export const Spinner = React.forwardRef<HTMLDivElement, SpinnerProps>(
function Spinner({onChange, onClick, onMouseDown, onMouseUp}, ref) {
function Spinner({onChange, onClick, onMouseDown, onMouseUp, onBlur}, ref) {
Copy link
Contributor Author

@ginabak ginabak Jan 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📣 The Spinner component within TextField is the 🔼 and 🔽 when the type='number' not sure why it's called Spinner 😄 🤷🏽‍♀️ I'm currently making a PR to rename it to Stepper 😅

function handleStep(step: number) {
return () => onChange(step);
}
Expand All @@ -35,6 +36,7 @@ export const Spinner = React.forwardRef<HTMLDivElement, SpinnerProps>(
onClick={handleStep(1)}
onMouseDown={handleMouseDown(handleStep(1))}
onMouseUp={onMouseUp}
onBlur={onBlur}
>
<div className={styles.SpinnerIcon}>
<Icon source={CaretUpMinor} />
Expand All @@ -47,6 +49,7 @@ export const Spinner = React.forwardRef<HTMLDivElement, SpinnerProps>(
onClick={handleStep(-1)}
onMouseDown={handleMouseDown(handleStep(-1))}
onMouseUp={onMouseUp}
onBlur={onBlur}
>
<div className={styles.SpinnerIcon}>
<Icon source={CaretDownMinor} />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,18 @@ import {mountWithApp} from 'tests/utilities';
import {Spinner} from '../Spinner';

describe('<Spinner />', () => {
const defaultProps = {
onChange: noop,
onMouseDown: noop,
onMouseUp: noop,
onBlur: noop,
};

describe('onChange', () => {
it('adds a change callback', () => {
const spy = jest.fn();
const spinner = mountWithApp(
<Spinner onChange={spy} onMouseDown={noop} onMouseUp={noop} />,
<Spinner {...defaultProps} onChange={spy} />,
);

spinner.find('div', {role: 'button'})!.trigger('onClick');
Expand All @@ -19,14 +26,7 @@ describe('<Spinner />', () => {
describe('onClick', () => {
it('adds a click callback', () => {
const spy = jest.fn();
const spinner = mountWithApp(
<Spinner
onClick={spy}
onChange={noop}
onMouseDown={noop}
onMouseUp={noop}
/>,
);
const spinner = mountWithApp(<Spinner {...defaultProps} onClick={spy} />);
spinner.find('div', {className: 'Spinner'})!.trigger('onClick');
expect(spy).toHaveBeenCalledTimes(1);
});
Expand All @@ -40,9 +40,9 @@ describe('<Spinner />', () => {
const changeSpy = jest.fn();
const spinner = mountWithApp(
<Spinner
{...defaultProps}
onChange={changeSpy}
onMouseDown={mouseDownSpy}
onMouseUp={noop}
/>,
);

Expand All @@ -61,9 +61,9 @@ describe('<Spinner />', () => {
const changeSpy = jest.fn();
const spinner = mountWithApp(
<Spinner
{...defaultProps}
onChange={changeSpy}
onMouseDown={mouseDownSpy}
onMouseUp={noop}
/>,
);

Expand All @@ -80,14 +80,25 @@ describe('<Spinner />', () => {
it('adds a mouse up callback', () => {
const spy = jest.fn();
const spinner = mountWithApp(
<Spinner onChange={noop} onMouseDown={noop} onMouseUp={spy} />,
<Spinner {...defaultProps} onMouseUp={spy} />,
);

spinner.find('div', {role: 'button'})!.trigger('onMouseUp');

expect(spy).toHaveBeenCalledTimes(1);
});
});

describe('onBlur()', () => {
it('is called when the stepper is blurred', () => {
const onBlurSpy = jest.fn();
const spinner = mountWithApp(
<Spinner {...defaultProps} onBlur={onBlurSpy} />,
);
spinner.find('div', {role: 'button'})!.trigger('onBlur');
expect(onBlurSpy).toHaveBeenCalledTimes(1);
});
});
});

function noop() {}