Skip to content

Commit

Permalink
support read only
Browse files Browse the repository at this point in the history
  • Loading branch information
lilyvc committed May 22, 2024
1 parent c43b828 commit 0a7d48e
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 4 deletions.
8 changes: 4 additions & 4 deletions packages/lab/src/stepper-input/StepperInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import stepperInputCss from "./StepperInput.css";
const withBaseName = makePrefixer("saltStepperInput");

export interface StepperInputProps
extends Omit<InputProps, "onChange" | "readOnly" | "emptyReadOnlyMarker"> {
extends Omit<InputProps, "onChange" | "emptyReadOnlyMarker"> {
/**
* A multiplier applied to the `step` when the value is incremented or decremented using the PageDown/PageUp keys.
*/
Expand Down Expand Up @@ -65,12 +65,12 @@ export interface StepperInputProps
export const StepperInput = forwardRef<HTMLDivElement, StepperInputProps>(
function StepperInput(props, ref) {
const {
textAlign = "left",
className,
hideButtons,
onBlur,
onChange,
onFocus,
readOnly,
...rest
} = props;

Expand All @@ -91,10 +91,10 @@ export const StepperInput = forwardRef<HTMLDivElement, StepperInputProps>(
onBlur={onBlur}
onFocus={onFocus}
ref={inputRef}
textAlign={textAlign}
readOnly={readOnly}
{...getInputProps(rest)}
/>
{!hideButtons && (
{!hideButtons && !readOnly && (
<div className={withBaseName("buttonContainer")}>
<Button
aria-label="increment value"
Expand Down
2 changes: 2 additions & 0 deletions packages/lab/src/stepper-input/useStepperInput.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ export const useStepperInput = (
};

const setNextValue = (modifiedValue: number) => {
if (props.readOnly) return;
let nextValue = modifiedValue;
if (nextValue < min) nextValue = min;
if (nextValue > max) nextValue = max;
Expand Down Expand Up @@ -233,6 +234,7 @@ export const useStepperInput = (
onChange: callAll(inputProps.onChange, handleInputChange),
onFocus: inputProps.onFocus,
onKeyDown: callAll(inputProps.onKeyDown, handleInputKeyDown),
textAlign: inputProps.textAlign,
value: String(currentValue),
};
};
Expand Down
9 changes: 9 additions & 0 deletions packages/lab/stories/stepper-input/stepper-input.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -167,3 +167,12 @@ export const HideButtons: StoryFn = (args) => {
</FormField>
);
};

export const ReadOnly: StoryFn = (args) => {
return (
<FormField>
<FormFieldLabel>Stepper Input</FormFieldLabel>
<StepperInput {...args} readOnly />
</FormField>
);
};

0 comments on commit 0a7d48e

Please sign in to comment.