-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(number-field): remove zagjs and use react-aria (#113)
- Loading branch information
Showing
36 changed files
with
573 additions
and
464 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { default } from "./number-field"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
import React, { useState, useId, forwardRef } from "react"; | ||
import { useNumberFieldState } from "@react-stately/numberfield"; | ||
import { useNumberField } from "@react-aria/numberfield"; | ||
import { useLocale } from "@react-aria/i18n"; | ||
import { mergeRefs } from "@react-aria/utils"; | ||
|
||
import clx from "clsx"; | ||
import { | ||
inputStyles, | ||
inputSizes, | ||
inputIntent, | ||
inputRootIntent, | ||
rootInput, | ||
rootInputFocused, | ||
} from "@/ui/text-field/text-field.css"; | ||
import FieldLabel from "@/ui/field-label"; | ||
import Stack from "@/ui/stack"; | ||
import Box from "@/ui/box"; | ||
import useTheme from "../hooks/use-theme"; | ||
import * as styles from "./number-field.css"; | ||
import type { NumberInputProps } from "./number-field.types"; | ||
|
||
const NumberInput = forwardRef<HTMLInputElement, NumberInputProps>( | ||
(props, forwardedRef) => { | ||
const { | ||
id = useId(), | ||
label, | ||
isDisabled, | ||
size = "sm", | ||
intent = "default", | ||
} = props; | ||
|
||
const { theme } = useTheme(); | ||
const { locale } = useLocale(); | ||
const [isFocused, setIsFocused] = useState<boolean>(false); | ||
|
||
const state = useNumberFieldState({ | ||
...props, | ||
locale, | ||
onFocusChange(focused) { | ||
setIsFocused(focused); | ||
}, | ||
}); | ||
|
||
const inputRef = React.useRef(null); | ||
const handleRef = mergeRefs(inputRef, forwardedRef); | ||
|
||
const { | ||
labelProps, | ||
groupProps, | ||
inputProps, | ||
incrementButtonProps, | ||
decrementButtonProps, | ||
} = useNumberField(props, state, inputRef); | ||
|
||
return ( | ||
<Box className={props?.className}> | ||
<Stack direction="vertical" space="$4"> | ||
{label && <FieldLabel htmlFor={id} label={label} {...labelProps} />} | ||
|
||
<div | ||
{...groupProps} | ||
className={clx( | ||
rootInput, | ||
isFocused ? rootInputFocused : null, | ||
props.isDisabled | ||
? inputRootIntent.disabled | ||
: inputRootIntent[props.intent], | ||
props.inputContainer | ||
)} | ||
> | ||
{props.canDecrement && React.isValidElement(props.decrementButton) | ||
? React.cloneElement(props.decrementButton, decrementButtonProps) | ||
: props?.decrementButton} | ||
|
||
<Box | ||
as="input" | ||
attributes={inputProps} | ||
ref={handleRef} | ||
textAlign={props.textAlign} | ||
fontSize={props.fontSize} | ||
className={clx( | ||
inputStyles[theme], | ||
inputSizes[size], | ||
props.isDisabled ? inputIntent.disabled : inputIntent[intent], | ||
props.inputClassName, | ||
props.borderless && styles.borderless, | ||
isDisabled && props.decrementButton | ||
? styles.withDecrementButton | ||
: null, | ||
isDisabled && props.incrementButton | ||
? styles.withIncrementButton | ||
: null | ||
)} | ||
/> | ||
|
||
{props.canIncrement && React.isValidElement(props.incrementButton) | ||
? React.cloneElement(props.incrementButton, incrementButtonProps) | ||
: props?.incrementButton} | ||
</div> | ||
</Stack> | ||
</Box> | ||
); | ||
} | ||
); | ||
|
||
export default NumberInput; |
39 changes: 39 additions & 0 deletions
39
packages/react/scaffolds/number-field/number-field.types.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { ReactNode } from "react"; | ||
import type { Sprinkles } from "@/styles/rainbow-sprinkles.css"; | ||
import type { AriaNumberFieldProps } from "@react-aria/numberfield"; | ||
|
||
export interface NumberInputProps { | ||
// ==== Core logic props | ||
defaultValue?: number; | ||
value?: number; | ||
minValue?: number; | ||
maxValue?: number; | ||
incrementButton?: ReactNode; | ||
decrementButton?: ReactNode; | ||
canIncrement?: boolean; | ||
canDecrement?: boolean; | ||
// ==== Form field props | ||
id?: string; | ||
isDisabled?: boolean; | ||
isReadOnly?: boolean; | ||
autoFocus?: boolean; | ||
formatOptions?: AriaNumberFieldProps["formatOptions"]; | ||
name?: string; | ||
label?: string; | ||
onChange?: (value: number) => void; | ||
onBlur?: (e?: any) => void; | ||
onFocus?: (e?: any) => void; | ||
onFocusChange?: (isFocused: boolean) => void; | ||
onKeyDown?: (e?: any) => void; | ||
onKeyUp?: (e?: any) => void; | ||
// ==== Style props | ||
textAlign?: Sprinkles["textAlign"]; | ||
fontSize?: Sprinkles["fontSize"]; | ||
size?: "sm" | "md"; | ||
placeholder?: string | undefined; | ||
intent?: "default" | "error"; | ||
className?: string; | ||
inputContainer?: string; | ||
inputClassName?: string; | ||
borderless?: boolean; | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.