-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #24 from Atnic/feature/input
Feature/input
- Loading branch information
Showing
16 changed files
with
449 additions
and
128 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,12 +1,15 @@ | ||
import Input from "./input"; | ||
import InputGroup from "./input-group"; | ||
import Textarea from "./textarea"; | ||
|
||
// export types | ||
export type {InputProps} from "./input"; | ||
export type {InputGroupProps} from "./input-group"; | ||
export type {TextAreaProps} from "./textarea"; | ||
|
||
// export hooks | ||
export {useInput} from "./use-input"; | ||
export {useInputGroup} from "./use-input-group"; | ||
|
||
// export component | ||
export {Input, Textarea}; | ||
export {Input, InputGroup, Textarea}; |
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,8 @@ | ||
import {createContext} from "@jala-banyu/react-utils"; | ||
|
||
import {ContextType} from "./use-input-group"; | ||
|
||
export const [InputGroupProvider, useInputGroupContext] = createContext<ContextType>({ | ||
name: "InputGroupContext", | ||
strict: false, | ||
}); |
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,26 @@ | ||
import {forwardRef} from "@jala-banyu/system"; | ||
import React from "react"; | ||
|
||
import {InputGroupProvider} from "./input-group-context"; | ||
import {UseInputGroupProps, useInputGroup} from "./use-input-group"; | ||
|
||
export interface InputGroupProps extends UseInputGroupProps {} | ||
|
||
const InputGroup = forwardRef<"div", InputGroupProps>((props, ref) => { | ||
const {Component, domRef, context, children, getInputGroupProps} = useInputGroup({ | ||
...props, | ||
ref, | ||
}); | ||
|
||
return ( | ||
<InputGroupProvider value={context}> | ||
<Component ref={domRef} {...getInputGroupProps()}> | ||
{children} | ||
</Component> | ||
</InputGroupProvider> | ||
); | ||
}); | ||
|
||
InputGroup.displayName = "Banyu.InputGroup"; | ||
|
||
export default InputGroup; |
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,91 @@ | ||
import type {InputProps} from "./index"; | ||
import type {ReactRef} from "@jala-banyu/react-utils"; | ||
import type {InputGroupVariantProps} from "@jala-banyu/theme"; | ||
|
||
import {inputGroup} from "@jala-banyu/theme"; | ||
import {HTMLBanyuProps, PropGetter, mapPropsVariants} from "@jala-banyu/system"; | ||
import {useDOMRef} from "@jala-banyu/react-utils"; | ||
import {useMemo, useCallback} from "react"; | ||
interface Props extends HTMLBanyuProps, InputGroupVariantProps { | ||
/** | ||
* Ref to the DOM node. | ||
*/ | ||
ref?: ReactRef<HTMLDivElement | null>; | ||
/** | ||
* Whether the buttons are disabled. | ||
* @default false | ||
*/ | ||
isDisabled?: InputProps["isDisabled"]; | ||
} | ||
|
||
export type ContextType = { | ||
size?: InputProps["size"]; | ||
variant?: InputProps["variant"]; | ||
radius?: InputProps["radius"]; | ||
isDisabled?: InputProps["isDisabled"]; | ||
disableAnimation?: InputProps["disableAnimation"]; | ||
}; | ||
|
||
export type UseInputGroupProps = Props & | ||
Partial<Pick<InputProps, "size" | "label" | "radius" | "variant" | "disableAnimation">>; | ||
|
||
export function useInputGroup(originalProps: UseInputGroupProps) { | ||
const [props, variantProps] = mapPropsVariants(originalProps, inputGroup.variantKeys); | ||
|
||
const { | ||
ref, | ||
as, | ||
children, | ||
size = "md", | ||
variant = "flat", | ||
isDisabled = false, | ||
disableAnimation = false, | ||
className, | ||
...otherProps | ||
} = props; | ||
|
||
const Component = as || "div"; | ||
|
||
const domRef = useDOMRef(ref); | ||
|
||
const classNames = useMemo( | ||
() => | ||
inputGroup({ | ||
...variantProps, | ||
className, | ||
}), | ||
[...Object.values(variantProps), className], | ||
); | ||
|
||
const context = useMemo<ContextType>( | ||
() => ({ | ||
size, | ||
isDisabled, | ||
disableAnimation, | ||
fullWidth: !!originalProps?.fullWidth, | ||
}), | ||
[size, variant, isDisabled, disableAnimation, originalProps?.fullWidth], | ||
); | ||
|
||
const getInputGroupProps: PropGetter = useCallback( | ||
() => ({ | ||
role: "group", | ||
"data-slot": "input-group", | ||
class: classNames, | ||
...otherProps, | ||
}), | ||
[otherProps], | ||
); | ||
|
||
return { | ||
Component, | ||
children, | ||
domRef, | ||
// label, | ||
context, | ||
classNames, | ||
getInputGroupProps, | ||
}; | ||
} | ||
|
||
export type UseButtonGroupReturn = ReturnType<typeof useInputGroup>; |
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,72 @@ | ||
/* eslint-disable jsx-a11y/interactive-supports-focus */ | ||
/* eslint-disable jsx-a11y/click-events-have-key-events */ | ||
import React from "react"; | ||
// @ts-ignore | ||
import {Meta} from "@storybook/react"; | ||
import {inputGroup} from "@jala-banyu/theme"; | ||
import {Button} from "@jala-banyu/button"; | ||
import {ArrowRightIcon} from "@jala-banyu/shared-icons"; | ||
|
||
import {Input, InputGroup} from "../src"; | ||
|
||
export default { | ||
title: "Components/InputGroup", | ||
component: InputGroup, | ||
argTypes: { | ||
size: { | ||
control: { | ||
type: "select", | ||
}, | ||
options: ["sm", "md", "lg"], | ||
}, | ||
isDisabled: { | ||
control: { | ||
type: "boolean", | ||
}, | ||
}, | ||
}, | ||
decorators: [ | ||
(Story) => ( | ||
<div className="w-400"> | ||
<Story /> | ||
</div> | ||
), | ||
], | ||
} as Meta<typeof InputGroup>; | ||
|
||
const defaultProps = { | ||
...inputGroup.defaultVariants, | ||
}; | ||
|
||
const Template = (args) => ( | ||
<div className="w-[600px]"> | ||
<InputGroup> | ||
{/* eslint-disable-next-line jsx-a11y/label-has-associated-control */} | ||
<label className="flex items-center bg-neutral-200 text-neutral-800 px-3 text-sm borber-r border-brand"> | ||
Label | ||
</label> | ||
<Input {...args} placeholder="Firstname" /> | ||
{/* eslint-disable-next-line jsx-a11y/label-has-associated-control */} | ||
<label className="flex items-center bg-neutral-200 text-neutral-800 px-3 text-sm borber-r border-brand"> | ||
<ArrowRightIcon /> | ||
</label> | ||
<Input {...args} placeholder="Lastname" /> | ||
<Button {...args} className="min-w-[72px] !border-transparent" radius="none"> | ||
Button | ||
</Button> | ||
</InputGroup> | ||
</div> | ||
); | ||
|
||
export const Default = { | ||
render: Template, | ||
parameters: { | ||
design: { | ||
type: "figma", | ||
url: "https://www.figma.com/file/T0TUGURgVGElV6MtU2EPYU/%5BJDS%5D-Design-System---Banyu-1.0?type=design&node-id=654-97121&mode=dev", | ||
}, | ||
}, | ||
args: { | ||
...defaultProps, | ||
}, | ||
}; |
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
Oops, something went wrong.