Skip to content

Commit d6819ba

Browse files
committed
Add form input components with field wrapper for Medusa UI
1 parent 8a9be4a commit d6819ba

6 files changed

Lines changed: 144 additions & 0 deletions

File tree

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { CurrencyInput as MedusaCurrencyInput } from '@medusajs/ui';
2+
import { type FC } from 'react';
3+
import { FieldWrapper } from './FieldWrapper';
4+
import type { BasicFieldProps, MedusaCurrencyInputProps } from './types';
5+
6+
export type Props = MedusaCurrencyInputProps & BasicFieldProps & {
7+
ref?: React.Ref<HTMLInputElement>;
8+
};
9+
10+
const Wrapper = FieldWrapper<Props>;
11+
12+
export const CurrencyInput: FC<Props> = ({ ref, ...props }) => (
13+
<Wrapper {...props}>{(inputProps) => <MedusaCurrencyInput {...inputProps} ref={ref} />}</Wrapper>
14+
);
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { DatePicker } from '@medusajs/ui';
2+
import { type FC } from 'react';
3+
import { FieldWrapper } from './FieldWrapper';
4+
import type { BasicFieldProps, DatePickerProps } from './types';
5+
6+
export type Props = DatePickerProps & BasicFieldProps & {
7+
ref?: React.Ref<HTMLInputElement>;
8+
};
9+
10+
const Wrapper = FieldWrapper<Props>;
11+
12+
export const DatePickerInput: FC<Props> = ({ ref, ...props }) => {
13+
return <Wrapper {...props}>{(inputProps) => <DatePicker {...{ ...inputProps, ref }} />}</Wrapper>;
14+
};
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import { Checkbox as MedusaCheckbox } from '@medusajs/ui';
2+
import clsx from 'clsx';
3+
import { FieldWrapper } from './FieldWrapper';
4+
import { Label } from './Label';
5+
import type { BasicFieldProps } from './types';
6+
7+
export type CheckedState = boolean | 'indeterminate';
8+
export type FieldCheckboxProps = BasicFieldProps & {
9+
checked?: CheckedState;
10+
onChange?: (checked: CheckedState) => void;
11+
ref?: React.Ref<HTMLButtonElement>;
12+
};
13+
14+
export const FieldCheckbox: React.FC<FieldCheckboxProps> = ({
15+
label,
16+
labelClassName,
17+
labelTooltip,
18+
wrapperClassName,
19+
errorClassName,
20+
formErrors,
21+
onChange,
22+
ref,
23+
...props
24+
}) => {
25+
return (
26+
<FieldWrapper<FieldCheckboxProps>
27+
wrapperClassName={wrapperClassName}
28+
errorClassName={errorClassName}
29+
formErrors={formErrors}
30+
{...props}
31+
>
32+
{(fieldProps) => (
33+
<div className="flex items-center">
34+
<MedusaCheckbox
35+
{...fieldProps}
36+
ref={ref}
37+
checked={props.checked}
38+
onChange={(e) => {}}
39+
onCheckedChange={(checked) => {
40+
onChange?.(checked);
41+
}}
42+
/>
43+
44+
{label && (
45+
<Label
46+
htmlFor={props.name}
47+
onClick={() => {
48+
onChange?.(!props.checked);
49+
}}
50+
className={clsx('ml-2 !mb-0 font-normal [&>label]:!cursor-pointer', labelClassName)}
51+
>
52+
{label}
53+
</Label>
54+
)}
55+
</div>
56+
)}
57+
</FieldWrapper>
58+
);
59+
};
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { Input as MedusaInput } from '@medusajs/ui';
2+
import { FieldWrapper } from './FieldWrapper';
3+
import type { BasicFieldProps, MedusaInputProps } from './types';
4+
5+
export type Props = MedusaInputProps & BasicFieldProps & {
6+
ref?: React.Ref<HTMLInputElement>;
7+
};
8+
9+
const Wrapper = FieldWrapper<Props>;
10+
11+
export const Input: React.FC<Props> = ({ ref, ...props }) => (
12+
<Wrapper {...props}>{(inputProps) => <MedusaInput {...inputProps} ref={ref} />}</Wrapper>
13+
);
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { Select as MedusaSelect } from '@medusajs/ui';
2+
import { FieldWrapper } from './FieldWrapper';
3+
import type { BasicFieldProps, SelectProps } from './types';
4+
5+
export type Props = SelectProps & BasicFieldProps & {
6+
ref?: React.Ref<unknown>;
7+
};
8+
9+
const Wrapper = FieldWrapper<Props>;
10+
11+
const SelectComponent: React.FC<Props> = ({ ref, ...props }) => {
12+
return (
13+
<Wrapper {...props}>
14+
{(inputProps) => <MedusaSelect {...{ ...inputProps, ref }}>{props.children}</MedusaSelect>}
15+
</Wrapper>
16+
);
17+
};
18+
19+
type SelectComponent = typeof SelectComponent & {
20+
Trigger: typeof MedusaSelect.Trigger;
21+
Value: typeof MedusaSelect.Value;
22+
Content: typeof MedusaSelect.Content;
23+
Item: typeof MedusaSelect.Item;
24+
};
25+
26+
export const Select: SelectComponent = Object.assign(SelectComponent, {
27+
Trigger: MedusaSelect.Trigger,
28+
Value: MedusaSelect.Value,
29+
Content: MedusaSelect.Content,
30+
Item: MedusaSelect.Item,
31+
});
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { Textarea } from '@medusajs/ui';
2+
import { FieldWrapper } from './FieldWrapper';
3+
import type { BasicFieldProps, TextAreaProps } from './types';
4+
5+
export type Props = TextAreaProps & BasicFieldProps & {
6+
ref?: React.Ref<HTMLTextAreaElement>;
7+
};
8+
9+
const Wrapper = FieldWrapper<Props>;
10+
11+
export const TextArea: React.FC<Props> = ({ ref, ...props }) => (
12+
<Wrapper {...props}>{(inputProps) => <Textarea {...inputProps} ref={ref} />}</Wrapper>
13+
);

0 commit comments

Comments
 (0)