diff --git a/packages/island/src/atoms/link/Link.tsx b/packages/island/src/atoms/link/Link.tsx index 54e9a25..5aa91fc 100644 --- a/packages/island/src/atoms/link/Link.tsx +++ b/packages/island/src/atoms/link/Link.tsx @@ -11,6 +11,7 @@ import { linkVariants } from './variants'; export function Link({ standalone, icon, + iconStart, className, children, ...props @@ -24,6 +25,7 @@ export function Link({ className={cn(linkVariants({ standalone }), className)} {...props} > + {iconStart && } {children} diff --git a/packages/island/src/atoms/link/types.ts b/packages/island/src/atoms/link/types.ts index 4121b9c..f40105a 100644 --- a/packages/island/src/atoms/link/types.ts +++ b/packages/island/src/atoms/link/types.ts @@ -9,4 +9,6 @@ export type LinkProps = ComponentPropsWithRef & VariantProps & { /** Trailing icon, such as `external` or `chevron-left` ("go to" points left in RTL). */ icon?: IconName; + /** Leading icon, for an action named by its verb, such as `edit` before «ویرایش شماره». */ + iconStart?: IconName; }; diff --git a/packages/island/src/molecules/file-field/FileField.tsx b/packages/island/src/molecules/file-field/FileField.tsx new file mode 100644 index 0000000..33c83ef --- /dev/null +++ b/packages/island/src/molecules/file-field/FileField.tsx @@ -0,0 +1,192 @@ +'use client'; + +import { useEffect, useId, useRef } from 'react'; +import type { ChangeEvent } from 'react'; + +import { Button } from '../../atoms/button'; +import { FlexBox } from '../../atoms/flex-box'; +import { Icon } from '../../atoms/icon'; +import { Typography } from '../../atoms/typography'; +import { Element } from '../../primitives/element'; +import { cn } from '../../tools/cn'; +import type { FileFieldProps } from './types'; +import { fileDropVariants, filePickedVariants } from './variants'; + +/** + * One file to send with a form, such as a national ID card image. Empty, the whole dashed box + * opens the picker; picked, it shows a thumbnail (for images), the name and the size with a + * remove button; uploading, a bar with the percent replaces remove. The file stays in memory. + */ +export function FileField({ + label, + hint, + file, + size, + onChange, + onBlur, + accept, + error, + progress, + disabled = false, + labels, + id: idProp, + name, + className, +}: FileFieldProps) { + const generatedId = useId(); + const id = idProp ?? generatedId; + const messageId = `${id}-message`; + const isInvalid = error != null && error !== false; + const isUploading = progress !== undefined; + + const pick = (event: ChangeEvent) => { + onChange(event.target.files?.[0] ?? null); + // The same file can be picked again after it was removed or rejected. + event.target.value = ''; + }; + + return ( + + + {label} + + {file ? ( + + {file.type.startsWith('image/') ? ( + + ) : ( + + )} + + + {file.name} + + {isUploading ? ( + <> + + {labels.uploading && ( + + {labels.uploading(progress)} + + )} + + ) : ( + size != null && ( + + {size} + + ) + )} + + {!isUploading && ( +