Skip to content
Snippets Groups Projects
Commit ae89d321 authored by Alissa Cheng's avatar Alissa Cheng
Browse files

feat: form components required prop

parent c9cef273
No related branches found
No related tags found
1 merge request!17form components
...@@ -5,6 +5,7 @@ import { disabledBg, disabledCursor } from "./utils/classes.ts"; ...@@ -5,6 +5,7 @@ import { disabledBg, disabledCursor } from "./utils/classes.ts";
type CheckboxProps = { type CheckboxProps = {
isDisabled?: boolean; isDisabled?: boolean;
isRequired?: boolean;
isSelected: boolean; isSelected: boolean;
setIsSelected?: Dispatch<SetStateAction<boolean>>; setIsSelected?: Dispatch<SetStateAction<boolean>>;
label?: string; label?: string;
...@@ -12,12 +13,14 @@ type CheckboxProps = { ...@@ -12,12 +13,14 @@ type CheckboxProps = {
const Checkbox = ({ const Checkbox = ({
isDisabled = false, isDisabled = false,
isRequired = false,
isSelected, isSelected,
setIsSelected, setIsSelected,
label, label,
}: CheckboxProps) => { }: CheckboxProps) => {
return ( return (
<NextCheckbox <NextCheckbox
isRequired={isRequired}
isDisabled={isDisabled} isDisabled={isDisabled}
isSelected={isSelected} isSelected={isSelected}
onValueChange={setIsSelected} onValueChange={setIsSelected}
......
...@@ -8,6 +8,7 @@ import Typography from "./Typography.tsx"; ...@@ -8,6 +8,7 @@ import Typography from "./Typography.tsx";
import { textColor } from "./utils/colors.ts"; import { textColor } from "./utils/colors.ts";
type DateInputProps = { type DateInputProps = {
isRequired?: boolean;
label: string; label: string;
value?: Date; value?: Date;
onValueChange: Dispatch<SetStateAction<Date | undefined>>; onValueChange: Dispatch<SetStateAction<Date | undefined>>;
...@@ -16,6 +17,7 @@ type DateInputProps = { ...@@ -16,6 +17,7 @@ type DateInputProps = {
}; };
const DateInput = ({ const DateInput = ({
isRequired = false,
label, label,
value, value,
onValueChange, onValueChange,
...@@ -25,13 +27,14 @@ const DateInput = ({ ...@@ -25,13 +27,14 @@ const DateInput = ({
const dateFormat = "dd/MM/yyyy"; const dateFormat = "dd/MM/yyyy";
type inputProps = { type inputProps = {
isRequired: boolean;
value?: string; value?: string;
onClick?: () => void; onClick?: () => void;
}; };
const CustomInput = forwardRef( const CustomInput = forwardRef(
( (
{ value, onClick }: inputProps, { isRequired, value, onClick }: inputProps,
// @ts-expect-error the "ref" prop is needed to avoid console errors. // @ts-expect-error the "ref" prop is needed to avoid console errors.
// eslint-disable-next-line @typescript-eslint/no-unused-vars // eslint-disable-next-line @typescript-eslint/no-unused-vars
ref ref
...@@ -39,6 +42,7 @@ const DateInput = ({ ...@@ -39,6 +42,7 @@ const DateInput = ({
return ( return (
<div onClick={onClick}> <div onClick={onClick}>
<BaseInput <BaseInput
isRequired={isRequired}
label={label} label={label}
endContent={ endContent={
<Icon <Icon
...@@ -91,7 +95,7 @@ const DateInput = ({ ...@@ -91,7 +95,7 @@ const DateInput = ({
selected={value} selected={value}
onSelect={(date) => onValueChange(date)} onSelect={(date) => onValueChange(date)}
onChange={(date) => !!date && onValueChange(date)} onChange={(date) => !!date && onValueChange(date)}
customInput={<CustomInput />} customInput={<CustomInput isRequired={isRequired} />}
showPopperArrow={false} showPopperArrow={false}
calendarStartDay={1} calendarStartDay={1}
formatWeekDay={(day) => <div>{day.charAt(0)}</div>} formatWeekDay={(day) => <div>{day.charAt(0)}</div>}
......
...@@ -4,6 +4,7 @@ import { baseInputClassNames } from "./utils/baseComponents.tsx"; ...@@ -4,6 +4,7 @@ import { baseInputClassNames } from "./utils/baseComponents.tsx";
import Icon from "./Icon.tsx"; import Icon from "./Icon.tsx";
type DropdownProps = { type DropdownProps = {
isRequired?: boolean;
label?: string; label?: string;
valueOptions: { value: string; label: string }[]; valueOptions: { value: string; label: string }[];
value?: string; value?: string;
...@@ -11,6 +12,7 @@ type DropdownProps = { ...@@ -11,6 +12,7 @@ type DropdownProps = {
}; };
const Dropdown = ({ const Dropdown = ({
isRequired = false,
label, label,
valueOptions, valueOptions,
value, value,
...@@ -18,6 +20,7 @@ const Dropdown = ({ ...@@ -18,6 +20,7 @@ const Dropdown = ({
}: DropdownProps) => { }: DropdownProps) => {
return ( return (
<Autocomplete <Autocomplete
isRequired={isRequired}
label={label} label={label}
selectedKey={value} selectedKey={value}
onSelectionChange={(key) => onValueChange(key as string)} onSelectionChange={(key) => onValueChange(key as string)}
...@@ -28,6 +31,7 @@ const Dropdown = ({ ...@@ -28,6 +31,7 @@ const Dropdown = ({
inputProps={{ inputProps={{
classNames: baseInputClassNames(false), classNames: baseInputClassNames(false),
variant: "bordered", variant: "bordered",
isRequired: isRequired,
}} // use the same props as BaseInput }} // use the same props as BaseInput
selectorIcon={<Icon name="dropdown" />} selectorIcon={<Icon name="dropdown" />}
isClearable={false} // so the "cross" button does not show isClearable={false} // so the "cross" button does not show
......
...@@ -3,15 +3,27 @@ import { Dispatch, SetStateAction } from "react"; ...@@ -3,15 +3,27 @@ import { Dispatch, SetStateAction } from "react";
import { disabledBg, disabledCursor } from "./utils/classes.ts"; import { disabledBg, disabledCursor } from "./utils/classes.ts";
type RadioProps = { type RadioProps = {
isRequired?: boolean;
label?: string; label?: string;
valueOptions: { value: string; label: string; isDisabled?: boolean }[]; valueOptions: { value: string; label: string; isDisabled?: boolean }[];
value?: string; value?: string;
onValueChange?: Dispatch<SetStateAction<string | undefined>>; onValueChange?: Dispatch<SetStateAction<string | undefined>>;
}; };
const Radio = ({ label, valueOptions, value, onValueChange }: RadioProps) => { const Radio = ({
isRequired = false,
label,
valueOptions,
value,
onValueChange,
}: RadioProps) => {
return ( return (
<RadioGroup label={label} value={value} onValueChange={onValueChange}> <RadioGroup
isRequired={isRequired}
label={label}
value={value}
onValueChange={onValueChange}
>
{valueOptions.map((option) => ( {valueOptions.map((option) => (
<NextRadio <NextRadio
isDisabled={option.isDisabled || false} isDisabled={option.isDisabled || false}
......
...@@ -2,6 +2,7 @@ import { Dispatch, ReactNode, SetStateAction } from "react"; ...@@ -2,6 +2,7 @@ import { Dispatch, ReactNode, SetStateAction } from "react";
import { BaseInput } from "./utils/baseComponents.tsx"; import { BaseInput } from "./utils/baseComponents.tsx";
type TextInputProps = { type TextInputProps = {
isRequired?: boolean;
label?: string; label?: string;
endContent?: ReactNode; // such as units (as text element e.g. <span>) endContent?: ReactNode; // such as units (as text element e.g. <span>)
isClearable?: boolean; isClearable?: boolean;
...@@ -13,6 +14,7 @@ type TextInputProps = { ...@@ -13,6 +14,7 @@ type TextInputProps = {
}; };
const TextInput = ({ const TextInput = ({
isRequired = false,
label = "", label = "",
endContent, endContent,
isClearable = false, isClearable = false,
...@@ -24,6 +26,7 @@ const TextInput = ({ ...@@ -24,6 +26,7 @@ const TextInput = ({
}: TextInputProps) => { }: TextInputProps) => {
return ( return (
<BaseInput <BaseInput
isRequired={isRequired}
label={label} label={label}
endContent={endContent} endContent={endContent}
placeholder=" " placeholder=" "
......
...@@ -13,6 +13,7 @@ const Toggle = ({ isSelected, setIsSelected, label }: ToggleProps) => { ...@@ -13,6 +13,7 @@ const Toggle = ({ isSelected, setIsSelected, label }: ToggleProps) => {
: "ml-[11px] bg-grey"; : "ml-[11px] bg-grey";
return ( return (
<Switch <Switch
// Does not support "isRequired": https://github.com/nextui-org/nextui/issues/1610
isSelected={isSelected} isSelected={isSelected}
onValueChange={setIsSelected} onValueChange={setIsSelected}
color="default" color="default"
......
...@@ -15,7 +15,8 @@ type BaseInputProps = { ...@@ -15,7 +15,8 @@ type BaseInputProps = {
isInvalid: boolean; isInvalid: boolean;
errorMessage: string; errorMessage: string;
// interaction // interaction
isDisabled: boolean; isRequired?: boolean;
isDisabled?: boolean;
}; };
type baseInputClassNamesObj = { [key in InputSlots]: string }; type baseInputClassNamesObj = { [key in InputSlots]: string };
...@@ -59,7 +60,8 @@ export const BaseInput = ({ ...@@ -59,7 +60,8 @@ export const BaseInput = ({
onValueChange, onValueChange,
isInvalid, isInvalid,
errorMessage, errorMessage,
isDisabled, isRequired = false,
isDisabled = false,
}: BaseInputProps) => { }: BaseInputProps) => {
return ( return (
<Input <Input
...@@ -72,6 +74,7 @@ export const BaseInput = ({ ...@@ -72,6 +74,7 @@ export const BaseInput = ({
onValueChange={onValueChange} onValueChange={onValueChange}
isInvalid={isInvalid} isInvalid={isInvalid}
errorMessage={errorMessage} errorMessage={errorMessage}
isRequired={isRequired}
isDisabled={isDisabled} isDisabled={isDisabled}
// appearance // appearance
labelPlacement="outside" labelPlacement="outside"
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment