Spaces:
Build error
Build error
File size: 5,629 Bytes
0bfe2e3 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 |
import { cva } from 'class-variance-authority';
import * as React from 'react';
import { cn, ComponentAnatomy, defineStyleAnatomy } from '../core/styling';
/* -------------------------------------------------------------------------------------------------
* Anatomy
* -----------------------------------------------------------------------------------------------*/
export const BasicFieldAnatomy = defineStyleAnatomy({
fieldLabel: cva([
'UI-BasicField__fieldLabel',
'text-base w-fit font-semibold self-start',
'data-[error=true]:text-red-500',
]),
fieldAsterisk: cva('UI-BasicField__fieldAsterisk ml-1 text-red-500 text-sm'),
fieldDetails: cva('UI-BasicField__fieldDetails'),
field: cva('UI-BasicField__field relative w-full space-y-1'),
fieldHelpText: cva('UI-BasicField__fieldHelpText text-sm text-[--muted]'),
fieldErrorText: cva('UI-BasicField__fieldErrorText text-sm text-red-500'),
});
/* -------------------------------------------------------------------------------------------------
* BasicFieldOptions
* - Field components inherit these props
* -----------------------------------------------------------------------------------------------*/
export type BasicFieldOptions = ComponentAnatomy<typeof BasicFieldAnatomy> & {
/**
* The id of the field. If not provided, a unique id will be generated.
*/
id?: string | undefined;
/**
* The form field name.
*/
name?: string;
/**
* The label of the field.
*/
label?: React.ReactNode;
/**
* Additional props to pass to the label element.
*/
labelProps?: React.LabelHTMLAttributes<HTMLLabelElement>;
/**
* Help or description text to display below the field.
*/
help?: React.ReactNode;
/**
* Error text to display below the field.
*/
error?: string;
/**
* If `true`, the field will be required.
*/
required?: boolean;
/**
* If `true`, the field will be disabled.
*/
disabled?: boolean;
/**
* If `true`, the field will be readonly.
*/
readonly?: boolean;
};
/* -------------------------------------------------------------------------------------------------
* Extract BasicFieldProps
* -----------------------------------------------------------------------------------------------*/
export function extractBasicFieldProps<Props extends BasicFieldOptions>(
props: Props,
id: string
) {
const {
name,
label,
labelProps,
help,
error,
required,
disabled = false,
readonly = false,
fieldDetailsClass,
fieldLabelClass,
fieldAsteriskClass,
fieldClass,
fieldErrorTextClass,
fieldHelpTextClass,
id: _id,
...rest
} = props;
return [
rest,
{
id: _id || id,
name,
label,
help,
error,
disabled,
required,
readonly,
fieldAsteriskClass,
fieldErrorTextClass,
fieldHelpTextClass,
fieldDetailsClass,
fieldLabelClass,
fieldClass,
labelProps,
},
] as [
Omit<
Props,
| 'label'
| 'name'
| 'help'
| 'error'
| 'disabled'
| 'required'
| 'readonly'
| 'fieldDetailsClass'
| 'fieldLabelClass'
| 'fieldClass'
| 'fieldHelpTextClass'
| 'fieldErrorTextClass'
| 'id'
| 'labelProps'
| 'fieldAsteriskClass'
>,
Omit<BasicFieldOptions, 'id'> & {
id: string;
},
];
}
/* -------------------------------------------------------------------------------------------------
* BasicField
* -----------------------------------------------------------------------------------------------*/
export type BasicFieldProps = React.ComponentPropsWithoutRef<'div'> &
BasicFieldOptions;
export const BasicField = React.memo(
React.forwardRef<HTMLDivElement, BasicFieldProps>((props, ref) => {
const {
children,
className,
labelProps,
id,
label,
error,
help,
disabled,
readonly,
required,
fieldClass,
fieldDetailsClass,
fieldLabelClass,
fieldAsteriskClass,
fieldErrorTextClass,
fieldHelpTextClass,
...rest
} = props;
return (
<div
className={cn(BasicFieldAnatomy.field(), className, fieldClass)}
{...rest}
ref={ref}
>
{!!label && (
<label
htmlFor={disabled ? undefined : id}
className={cn(BasicFieldAnatomy.fieldLabel(), fieldLabelClass)}
data-error={!!error}
{...labelProps}
>
{label}
{required && (
<span
className={cn(
BasicFieldAnatomy.fieldAsterisk(),
fieldAsteriskClass
)}
>
*
</span>
)}
</label>
)}
{children}
{(!!help || !!error) && (
<div
className={cn(BasicFieldAnatomy.fieldDetails(), fieldDetailsClass)}
>
{!!help && (
<div
className={cn(
BasicFieldAnatomy.fieldHelpText(),
fieldHelpTextClass
)}
>
{help}
</div>
)}
{!!error && (
<div
className={cn(
BasicFieldAnatomy.fieldErrorText(),
fieldErrorTextClass
)}
>
{error}
</div>
)}
</div>
)}
</div>
);
})
);
BasicField.displayName = 'BasicField';
|