Skip to content

Commit

Permalink
fix: number input borderless and typography props, upgrade deps (#109)
Browse files Browse the repository at this point in the history
  • Loading branch information
yyyyaaa authored Oct 23, 2023
1 parent 571450f commit 4e99963
Show file tree
Hide file tree
Showing 10 changed files with 208 additions and 193 deletions.
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@
"@vanilla-extract/dynamic": "^2.0.3",
"@vitejs/plugin-vue": "^4.1.0",
"@vue/babel-preset-app": "^5.0.8",
"@zag-js/number-input": "^0.15.0",
"@zag-js/react": "^0.15.0",
"@zag-js/vue": "^0.15.0",
"@zag-js/number-input": "^0.26.0",
"@zag-js/react": "^0.26.0",
"@zag-js/vue": "^0.26.0",
"autoprefixer": "^10.4.14",
"command-line-args": "^5.2.1",
"copy-to-clipboard": "^3.3.3",
Expand Down
4 changes: 2 additions & 2 deletions packages/react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@
"@vanilla-extract/css-utils": "^0.1.3",
"@vanilla-extract/dynamic": "^2.0.3",
"@vanilla-extract/recipes": "^0.4.0",
"@zag-js/number-input": "^0.15.0",
"@zag-js/react": "^0.15.0",
"@zag-js/number-input": "^0.26.0",
"@zag-js/react": "^0.26.0",
"animejs": "^3.2.1",
"bignumber.js": "^9.1.1",
"clsx": "^1.2.1",
Expand Down
7 changes: 4 additions & 3 deletions packages/react/scaffolds/number-input/number-input.css.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { style } from "@vanilla-extract/css";

export const borderless = style({
border: "0",
padding: "0",
border: "0 !important",
paddingTop: "0 !important",
paddingBottom: "0 !important",
selectors: {
"&:focus": {
boxShadow: "none",
boxShadow: "none !important",
},
},
});
Expand Down
100 changes: 52 additions & 48 deletions packages/react/scaffolds/number-input/number-input.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useId, useEffect, forwardRef } from "react";
import React, { useId, forwardRef } from "react";
import clx from "clsx";
import * as numberInput from "@zag-js/number-input";
import { useMachine, normalizeProps } from "@zag-js/react";
Expand All @@ -11,13 +11,17 @@ import {
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-input.css";

const NumberInput = forwardRef<HTMLInputElement, NumberInputProps>(
(props, forwardedRef) => {
const {
id = useId(),
label,
disabled,
readOnly,
value,
Expand All @@ -26,7 +30,6 @@ const NumberInput = forwardRef<HTMLInputElement, NumberInputProps>(
step,
onChange,
onFocus,
onBlur,
size = "sm",
intent = "default",
name,
Expand All @@ -35,6 +38,7 @@ const NumberInput = forwardRef<HTMLInputElement, NumberInputProps>(
} = props;

const { theme } = useTheme();

const [state, send] = useMachine(
numberInput.machine({
id,
Expand All @@ -45,65 +49,65 @@ const NumberInput = forwardRef<HTMLInputElement, NumberInputProps>(
max,
step,
name,
minFractionDigits,
maxFractionDigits,
onChange: (details) => {
onChange?.(details);
},
onFocus: (details) => {
onFocus?.(details);
},
onBlur: (details) => {
onBlur?.(details);
formatOptions: {
maximumFractionDigits: maxFractionDigits,
minimumFractionDigits: minFractionDigits,
},
onValueChange: onChange,
onFocusChange: onFocus,
})
);

const api = numberInput.connect(state, send, normalizeProps);

useEffect(() => {
if (!api.isFocused && value) {
api.setValue(value);
}
}, [value]);

return (
<div {...api.rootProps} className={props?.className}>
<div
className={clx(
rootInput,
api.isFocused ? rootInputFocused : null,
props.disabled
? inputRootIntent.disabled
: inputRootIntent[props.intent],
props.inputContainer
<Stack direction="vertical" space="$4">
{label && (
<FieldLabel htmlFor={id} label={label} {...api.labelProps} />
)}
>
{props.canDecrese && React.isValidElement(props.startAddon)
? React.cloneElement(props.startAddon, api.decrementTriggerProps)
: props?.startAddon}

<input
{...api.inputProps}
ref={forwardedRef}
disabled={disabled}
id={id}
value={value}
<div
className={clx(
inputStyles[theme],
inputSizes[size],
props.disabled ? inputIntent.disabled : inputIntent[intent],
props.inputClassName,
props.borderless && styles.borderless,
disabled && props.startAddon ? styles.withStartAddon : null,
disabled && props.endAddon ? styles.withEndAddon : null
rootInput,
api.isFocused ? rootInputFocused : null,
props.disabled
? inputRootIntent.disabled
: inputRootIntent[props.intent],
props.inputContainer
)}
/>
>
{props.canDecrese && React.isValidElement(props.startAddon)
? React.cloneElement(props.startAddon, api.decrementTriggerProps)
: props?.startAddon}

<Box
as="input"
attributes={{
...api.inputProps,
id,
disabled,
value,
}}
ref={forwardedRef}
textAlign={props.textAlign}
fontSize={props.fontSize}
className={clx(
inputStyles[theme],
inputSizes[size],
props.disabled ? inputIntent.disabled : inputIntent[intent],
props.inputClassName,
props.borderless && styles.borderless,
disabled && props.startAddon ? styles.withStartAddon : null,
disabled && props.endAddon ? styles.withEndAddon : null
)}
/>

{props.canIncrease && React.isValidElement(props.endAddon)
? React.cloneElement(props.endAddon, api.decrementTriggerProps)
: props?.endAddon}
</div>
{props.canIncrease && React.isValidElement(props.endAddon)
? React.cloneElement(props.endAddon, api.decrementTriggerProps)
: props?.endAddon}
</div>
</Stack>
</div>
);
}
Expand Down
17 changes: 8 additions & 9 deletions packages/react/scaffolds/number-input/number-input.types.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import { ReactNode } from "react";
import type { Sprinkles } from "@/styles/rainbow-sprinkles.css";

type Value = {
value: string;
valueAsNumber: number;
};

type FocusChangeValue = {
focused: boolean;
} & Value;

export interface NumberInputProps {
id?: string;
/**
Expand Down Expand Up @@ -38,15 +43,9 @@ export interface NumberInputProps {
/**
* Function invoked when the number input is focused
*/
onFocus?: (
details: Value & {
srcElement: HTMLElement | null;
}
) => void;
/**
* The value of the input when it is blurred
*/
onBlur?: (details: Value) => void;
onFocus?: (details: FocusChangeValue) => void;
textAlign?: Sprinkles["textAlign"];
fontSize?: Sprinkles["fontSize"];
size?: "sm" | "md";
placeholder?: string | undefined;
intent?: "default" | "error";
Expand Down
37 changes: 37 additions & 0 deletions packages/react/stories/NumberInput.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import React from "react";
import type { Meta, StoryObj } from "@storybook/react";

import NumberInput from "../src/ui/number-input";

const meta: Meta<typeof NumberInput> = {
component: NumberInput,
title: "NumberInput",
tags: ["autodocs"],
argTypes: {},
};

export default meta;

type Story = StoryObj<typeof meta>;

export const Primary: Story = {
args: {},
render: () => {
const [value, setValue] = React.useState("0");

return (
<NumberInput
id="my-num-input"
label="My Amount"
min={0}
max={2000}
textAlign="right"
value={value}
onChange={(details) => {
console.log("Change", details.value);
setValue(details.value);
}}
/>
);
},
};
2 changes: 1 addition & 1 deletion packages/vue/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"@vanilla-extract/css": "^1.12.0",
"@vanilla-extract/dynamic": "^2.0.3",
"@vanilla-extract/recipes": "^0.4.0",
"@zag-js/vue": "^0.2.10",
"@zag-js/vue": "^0.26.0",
"animejs": "^3.2.1",
"copy-to-clipboard": "^3.3.3",
"immer": "^9.0.19",
Expand Down
11 changes: 9 additions & 2 deletions src/ui/field-label/field-label.lite.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ export default function FieldLabel(props: FieldLabelProps) {
<Show
when={props.htmlFor === false}
else={
<label id={props.id} htmlFor={props.htmlFor as any}>
<label
id={props.id}
htmlFor={props.htmlFor as any}
{...props.labelAttributes}
>
<span
className={clx(
fieldlabelStyle,
Expand All @@ -30,7 +34,10 @@ export default function FieldLabel(props: FieldLabelProps) {
</label>
}
>
<p className={clx(fieldlabelStyle, fieldLabelSizes[props.size])}>
<p
className={clx(fieldlabelStyle, fieldLabelSizes[props.size])}
{...props.labelAttributes}
>
<span>{props.label}</span>
</p>
</Show>
Expand Down
2 changes: 1 addition & 1 deletion src/ui/field-label/field-label.types.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ export interface FieldLabelProps extends BaseComponentProps {
size?: "sm" | "md" | "lg";
description?: BaseComponentProps["children"];
descriptionId?: string;
data?: any;
attributes?: any;
labelAttributes?: any;
}
Loading

0 comments on commit 4e99963

Please sign in to comment.