-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
281 additions
and
168 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,76 +1,51 @@ | ||
import { useId } from '@radix-ui/react-id' | ||
import { useCombinedState } from '@spark-ui/use-combined-state' | ||
import { cx } from 'class-variance-authority' | ||
import { | ||
ChangeEvent, | ||
ComponentPropsWithoutRef, | ||
forwardRef, | ||
PropsWithChildren, | ||
ReactNode, | ||
useState, | ||
} from 'react' | ||
import { ComponentPropsWithoutRef, FocusEvent, forwardRef, PropsWithChildren } from 'react' | ||
|
||
import { inputStyles, labelTextStyles } from './Input.styles' | ||
import { InputFieldset } from './InputFieldset' | ||
import { inputStyles, InputStylesProps } from './Input.styles' | ||
import { useInputGroup } from './InputGroupContext' | ||
|
||
export interface InputProps extends ComponentPropsWithoutRef<'input'> { | ||
leftAddon?: ReactNode | ||
rightAddon?: ReactNode | ||
} | ||
export interface InputProps extends ComponentPropsWithoutRef<'input'>, InputStylesProps {} | ||
|
||
export const Input = forwardRef<HTMLInputElement, PropsWithChildren<InputProps>>( | ||
( | ||
{ | ||
className, | ||
value: valueProp, | ||
defaultValue, | ||
placeholder, | ||
leftAddon, | ||
rightAddon, | ||
children, | ||
...others | ||
}, | ||
ref | ||
) => { | ||
const id = useId() | ||
const [value, setValue] = useCombinedState(valueProp, defaultValue) | ||
const [isFocused, setIsFocused] = useState(false) | ||
const isExpanded = isFocused || !!value || !!placeholder || !!leftAddon | ||
|
||
const handleFocus = () => { | ||
setIsFocused(true) | ||
({ className, intent = 'neutral', onFocus, onBlur, ...others }, ref) => { | ||
const group = useInputGroup() | ||
const { isLeftAddonVisible, isRightAddonVisible, isHovered } = group | ||
|
||
const handleFocus = (event: FocusEvent<HTMLInputElement>) => { | ||
if (onFocus) { | ||
onFocus(event) | ||
} | ||
|
||
if (group.onFocus) { | ||
group.onFocus() | ||
} | ||
} | ||
|
||
const handleBlur = () => { | ||
setIsFocused(false) | ||
} | ||
const handleBlur = (event: FocusEvent<HTMLInputElement>) => { | ||
if (onBlur) { | ||
onBlur(event) | ||
} | ||
|
||
const handleChange = (event: ChangeEvent<HTMLInputElement>) => { | ||
setValue(event.target.value) | ||
if (group.onBlur) { | ||
group.onBlur() | ||
} | ||
} | ||
|
||
return ( | ||
<div className={cx(inputStyles({ isFocused }), className)}> | ||
{leftAddon} | ||
<input | ||
id={id} | ||
ref={ref} | ||
value={value} | ||
placeholder={placeholder} | ||
className="box-content h-full w-full text-ellipsis bg-transparent outline-0" | ||
onFocus={handleFocus} | ||
onBlur={handleBlur} | ||
onChange={handleChange} | ||
{...others} | ||
/> | ||
<InputFieldset label={children} isExpanded={isExpanded} /> | ||
|
||
<label htmlFor={id} className={labelTextStyles({ isExpanded })}> | ||
{children} | ||
</label> | ||
|
||
{rightAddon} | ||
</div> | ||
<input | ||
ref={ref} | ||
className={inputStyles({ | ||
className, | ||
intent, | ||
isHovered: !!isHovered, | ||
isLeftAddonVisible: !!isLeftAddonVisible, | ||
isRightAddonVisible: !!isRightAddonVisible, | ||
})} | ||
onFocus={handleFocus} | ||
onBlur={handleBlur} | ||
{...others} | ||
/> | ||
) | ||
} | ||
) | ||
|
||
Input.displayName = 'Input' |
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,34 @@ | ||
import { cva, VariantProps } from 'class-variance-authority' | ||
|
||
export const inputAddonStyles = cva(['border-sm', 'flex', 'items-center', 'rounded-lg'], { | ||
variants: { | ||
intent: { | ||
neutral: ['border-outline'], | ||
success: ['border-success'], | ||
alert: ['border-alert'], | ||
error: ['border-error'], | ||
}, | ||
isFocused: { | ||
true: [], | ||
false: [], | ||
}, | ||
isHovered: { | ||
true: [], | ||
false: [], | ||
}, | ||
}, | ||
compoundVariants: [ | ||
{ | ||
intent: 'neutral', | ||
isHovered: true, | ||
class: '!border-outline-high', | ||
}, | ||
{ | ||
intent: 'neutral', | ||
isFocused: true, | ||
class: '!border-outline-high', | ||
}, | ||
], | ||
}) | ||
|
||
export type InputAddonStylesProps = VariantProps<typeof inputAddonStyles> |
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,24 @@ | ||
import { ComponentPropsWithoutRef, forwardRef, PropsWithChildren } from 'react' | ||
|
||
import { inputAddonStyles, InputAddonStylesProps } from './InputAddon.styles' | ||
import { useInputGroup } from './InputGroupContext' | ||
|
||
export interface InputAddonProps | ||
extends ComponentPropsWithoutRef<'div'>, | ||
Omit<InputAddonStylesProps, 'intent' | 'isHovered' | 'isFocused'> {} | ||
|
||
export const InputAddon = forwardRef<HTMLDivElement, PropsWithChildren<InputAddonProps>>( | ||
({ className, ...others }, ref) => { | ||
const { intent, isHovered, isFocused } = useInputGroup() | ||
|
||
return ( | ||
<div | ||
ref={ref} | ||
className={inputAddonStyles({ className, intent, isHovered, isFocused })} | ||
{...others} | ||
/> | ||
) | ||
} | ||
) | ||
|
||
InputAddon.displayName = 'InputAddon' |
Empty file.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,18 @@ | ||
import { cva, VariantProps } from 'class-variance-authority' | ||
|
||
export const inputGroupStyles = cva(['inline-flex', 'outline-2', 'rounded-lg', 'outline-primary'], { | ||
variants: { | ||
isFocused: { | ||
true: ['ring-1'], | ||
false: [], | ||
}, | ||
intent: { | ||
neutral: ['ring-outline-high'], | ||
success: ['ring-success'], | ||
alert: ['ring-alert'], | ||
error: ['ring-error'], | ||
}, | ||
}, | ||
}) | ||
|
||
export type InputGroupStylesProps = VariantProps<typeof inputGroupStyles> |
Oops, something went wrong.