/*! * Copyright (c) Microsoft. All rights reserved. * Licensed under the MIT license. See LICENSE file in the project. */ import type { Step } from '@datashaper/workflow' import { produce } from 'immer' import { useCallback } from 'react' import type { StepChangeFunction } from '../../types.js' export type SpinButtonChangeHandler = ( event: React.SyntheticEvent, newValue?: string, ) => void /** * Enforces numeric values for a SpinButton onChange. * @param step - the step object * @param updateFn - the update function * @param onChange -the onchange handler * @returns */ export function useSpinButtonChangeHandler( step: Step, updateFn: (step: Step, newValue: string | undefined) => void, onChange?: StepChangeFunction, ): SpinButtonChangeHandler { /* eslint-disable-next-line react-hooks/exhaustive-deps */ return useCallback(getSpinButtonChangeHandler(step, updateFn, onChange), [ step, onChange, updateFn, ]) } function getSpinButtonChangeHandler( step: Step, updateFn: (step: Step, newValue: string | undefined) => void, onChange?: StepChangeFunction, ): SpinButtonChangeHandler { return (_event, newValue) => { onChange?.( produce(step, (draft) => { updateFn(draft as Step, newValue) }), ) } }