Skip to content

Commit

Permalink
Improve types and multiselect styles
Browse files Browse the repository at this point in the history
  • Loading branch information
majakomel committed Aug 6, 2024
1 parent a71ecb0 commit d7965df
Show file tree
Hide file tree
Showing 12 changed files with 53 additions and 74 deletions.
9 changes: 4 additions & 5 deletions src/components/Checkbox.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import React, { forwardRef } from 'react'
import { twMerge } from 'tailwind-merge'
export interface CheckboxProps {
import ErrorMessage from './ErrorMessage'

type CheckboxProps = Omit<React.InputHTMLAttributes<HTMLInputElement>, "type"> & {
error?: string
name: string
label: string
disabled?: boolean
className?: string
}

const Checkbox = forwardRef<HTMLInputElement, CheckboxProps>(
Expand Down Expand Up @@ -50,8 +49,8 @@ const Checkbox = forwardRef<HTMLInputElement, CheckboxProps>(
<div className="ml-2">
<label htmlFor={name}>{label}</label>
</div>
{/* {error} */}
</div>
{error && <ErrorMessage>{error}</ErrorMessage>}
</>
),
)
Expand Down
11 changes: 4 additions & 7 deletions src/components/ErrorMessage.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
import React, { ReactNode } from 'react'
import React from 'react'
import { twMerge } from 'tailwind-merge'

interface ErrorMessageProps {
children: ReactNode
}

const ErrorMessage = ({ children }: ErrorMessageProps) => {
return <p className="mt-1 text-sm text-red-700">{children}</p>
const ErrorMessage = ({ children, className }: React.HTMLAttributes<HTMLElement>) => {
return <p className={twMerge("mt-1 text-sm text-red-700", className)}>{children}</p>
}

export default ErrorMessage
2 changes: 1 addition & 1 deletion src/components/IconButton.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React, { ReactNode } from 'react'

export interface IconButtonProps {
export interface IconButtonProps extends React.HtmlHTMLAttributes<HTMLButtonElement> {
icon: ReactNode
}

Expand Down
6 changes: 2 additions & 4 deletions src/components/Input.tsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
import React, { forwardRef } from 'react'
import ErrorMessage from './ErrorMessage'

export interface InputProps {
type InputProps = Omit<React.InputHTMLAttributes<HTMLInputElement>, "type"> & {
error?: string
label?: string
name: string
className?: string
}

const Input = forwardRef<HTMLInputElement, InputProps>(
({ error, name, label, className, ...props }, ref) => {
return (
<div className={className}>
{label && (
<label className="font-semibold mb-1 block" htmlFor={name}>
<label className="font-semibold mb-1 block leading-none" htmlFor={name}>
{label}
</label>
)}
Expand Down
17 changes: 0 additions & 17 deletions src/components/Label.tsx

This file was deleted.

28 changes: 17 additions & 11 deletions src/components/MultiSelect.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React from 'react'
import Select from 'react-select'
import { twMerge } from 'tailwind-merge'
import ErrorMessage from './ErrorMessage'

export interface MultiSelectProps {
Expand All @@ -20,21 +21,22 @@ const MultiSelect = ({
}: MultiSelectProps) => (
<div className={className}>
{label && (
<label className="font-semibold mb-1 block" htmlFor={name}>
<label className="font-semibold mb-1 block leading-none" htmlFor={name}>
{label}
</label>
)}
<Select
options={options}
isMulti
styles={{
control: (baseStyles) => ({
...baseStyles,
control: () => ({
outline: '0',
transition: 'all 100ms',
borderStyle: 'solid',
boxSizing: 'border-box',
borderRadius: '32px',
minHeight: '36.5px',
borderWidth: '1px',
boxShadow: 'none',
paddingLeft: '5px',
}),
indicatorSeparator: () => ({
display: 'none',
Expand All @@ -52,17 +54,21 @@ const MultiSelect = ({
'&:before': {
content: '"✕"',
fontSize: '80%',
padding: '0 6px 0 4px',
padding: '0 8px 0 4px',
},
}),
}}
classNames={{
control: (state) =>
state.isFocused
? 'border-blue-500 hover:border-blue-500'
: 'border-grey-700 hover:border-gray-800',
control: (state) =>(
twMerge(
'flex flex-wrap cursor-default items-center relative border',
state.isFocused
? 'border-blue-500 hover:border-blue-500'
: 'border-gray-600 hover:border-gray-800'
)
),
multiValue: () => 'bg-gray-300',
multiValueRemove: () => 'hover:cursor-pointer text-red-700',
multiValueRemove: () => 'hover:cursor-pointer hover:text-red-700 self-center',
}}
{...props}
/>
Expand Down
28 changes: 17 additions & 11 deletions src/components/MultiSelectCreatable.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React, { FocusEvent, KeyboardEventHandler, useEffect } from 'react'
import { MultiValue } from 'react-select'
import CreatableSelect from 'react-select/creatable'
import { twMerge } from 'tailwind-merge'
import ErrorMessage from './ErrorMessage'

interface Option {
Expand Down Expand Up @@ -64,7 +65,7 @@ const MultiSelectCreatable = ({
return (
<div className={className}>
{label && (
<label className="font-semibold mb-1 block" htmlFor={name}>
<label className="font-semibold mb-1 block leading-none" htmlFor={name}>
{label}
</label>
)}
Expand All @@ -79,21 +80,26 @@ const MultiSelectCreatable = ({
inputValue={inputValue}
value={value}
classNames={{
control: (state) =>
state.isFocused
? 'border-blue-500 hover:border-blue-500'
: 'border-gray-600 hover:border-gray-800',
control: (state) =>(
twMerge(
'flex flex-wrap cursor-default items-center relative border',
state.isFocused
? 'border-blue-500 hover:border-blue-500'
: 'border-gray-600 hover:border-gray-800'
)
),
multiValue: () => 'bg-gray-300',
multiValueRemove: () => 'hover:cursor-pointer text-red-700',
multiValueRemove: () => 'hover:cursor-pointer hover:text-red-700 self-center',
}}
styles={{
control: (baseStyles) => ({
...baseStyles,
control: () => ({
outline: '0',
transition: 'all 100ms',
borderStyle: 'solid',
boxSizing: 'border-box',
borderRadius: '32px',
minHeight: '36.5px',
borderWidth: '1px',
boxShadow: 'none',
paddingLeft: '5px',
}),
indicatorSeparator: () => ({
display: 'none',
Expand All @@ -111,7 +117,7 @@ const MultiSelectCreatable = ({
'&:before': {
content: '"✕"',
fontSize: '80%',
padding: '0 6px 0 4px',
padding: '0 8px 0 4px',
},
}),
}}
Expand Down
5 changes: 2 additions & 3 deletions src/components/RadioButton.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import React, { forwardRef } from 'react'

export interface RadioButtonProps extends React.HTMLProps<HTMLInputElement> {
label?: string
name: string
type RadioButtonProps = Omit<React.InputHTMLAttributes<HTMLInputElement>, "type"> & {
label: string
}

const RadioButton = forwardRef<HTMLInputElement, RadioButtonProps>(
Expand Down
7 changes: 1 addition & 6 deletions src/components/RadioGroup.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
import React, { ChangeEvent, Children, ReactNode, cloneElement } from 'react'
import { twMerge } from 'tailwind-merge'

export interface RadioGroupProps {
children?: ReactNode
name?: string
value?: string
type RadioGroupProps = React.InputHTMLAttributes<HTMLInputElement> & {
flexDirection?: 'row' | 'column'
className?: string
onChange: (arg: string) => void
}

const RadioGroup = ({
Expand Down
6 changes: 3 additions & 3 deletions src/components/Select.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import React, { forwardRef } from 'react'
import ErrorMessage from './ErrorMessage'

export interface SelectProps extends React.HTMLProps<HTMLSelectElement> {
label?: string
type SelectProps = React.InputHTMLAttributes<HTMLSelectElement> & {
error?: string
label?: string
}

const Select = forwardRef<HTMLSelectElement, SelectProps>(
({ label, name, className, error, ...props }, ref) => (
<div className={className}>
{label && (
<label className="font-semibold mb-1 block" htmlFor={name}>
<label className="font-semibold mb-1 block leading-none" htmlFor={name}>
{label}
</label>
)}
Expand Down
7 changes: 2 additions & 5 deletions src/components/Textarea.tsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
import React, { forwardRef } from 'react'
import ErrorMessage from './ErrorMessage'

export interface TextareaProps {
type TextareaProps = React.TextareaHTMLAttributes<HTMLTextAreaElement> & {
error?: string
fontSize?: number
label?: string
name: string
className?: string
}

const Textarea = forwardRef<HTMLTextAreaElement, TextareaProps>(
({ error, label, name, className, ...props }, ref) => {
return (
<div className={className}>
{label && (
<label className="font-semibold mb-1 block" htmlFor={name}>
<label className="font-semibold mb-1 block leading-none" htmlFor={name}>
{label}
</label>
)}
Expand Down
1 change: 0 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
export { default as Checkbox } from './components/Checkbox'
export { default as Input } from './components/Input'
export { default as Label } from './components/Label'
export { default as LogoOONIRun } from './components/LogoOONIRun'
export { default as Modal } from './components/Modal'
export { default as MultiSelect } from './components/MultiSelect'
Expand Down

0 comments on commit d7965df

Please sign in to comment.