feat(admin): add RoleBadge component and integrate it in user management views
- add new RoleBadge shared component for consistent role display - export RoleBadge from shared components index - replace inline Badge usage with RoleBadge in UsersPage role column - use RoleBadge via renderTag prop in UserEditModal role TagInput - simplify TagInput Pill to a generic unstyled pill, removing color logic
This commit is contained in:
@@ -0,0 +1,45 @@
|
||||
'use client';
|
||||
|
||||
const hexToRgb = (hex) => {
|
||||
const result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
|
||||
return result
|
||||
? { r: parseInt(result[1], 16), g: parseInt(result[2], 16), b: parseInt(result[3], 16) }
|
||||
: null;
|
||||
};
|
||||
|
||||
const RoleBadge = ({ name, color, onRemove }) => {
|
||||
const rgb = color ? hexToRgb(color) : null;
|
||||
const style = rgb
|
||||
? { backgroundColor: `rgba(${rgb.r},${rgb.g},${rgb.b},0.15)`, borderColor: `rgba(${rgb.r},${rgb.g},${rgb.b},0.4)`, color }
|
||||
: {};
|
||||
const fallback = !rgb
|
||||
? 'bg-neutral-100 dark:bg-neutral-700 border-neutral-200 dark:border-neutral-600 text-neutral-700 dark:text-neutral-300'
|
||||
: '';
|
||||
|
||||
return (
|
||||
<span
|
||||
className={`inline-flex items-center gap-1 px-2 py-0.5 rounded-full text-xs font-medium border ${fallback}`}
|
||||
style={style}
|
||||
>
|
||||
{rgb && (
|
||||
<span
|
||||
className="inline-block w-2 h-2 rounded-full flex-shrink-0"
|
||||
style={{ backgroundColor: color }}
|
||||
/>
|
||||
)}
|
||||
{name}
|
||||
{onRemove && (
|
||||
<button
|
||||
type="button"
|
||||
onClick={(e) => { e.stopPropagation(); onRemove(); }}
|
||||
className="ml-0.5 hover:opacity-70 transition-opacity leading-none"
|
||||
aria-label={`Retirer ${name}`}
|
||||
>
|
||||
×
|
||||
</button>
|
||||
)}
|
||||
</span>
|
||||
);
|
||||
};
|
||||
|
||||
export default RoleBadge;
|
||||
@@ -3,45 +3,19 @@
|
||||
import { useState, useRef, useEffect } from 'react';
|
||||
import { createPortal } from 'react-dom';
|
||||
|
||||
const hexToRgb = (hex) => {
|
||||
const result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
|
||||
return result
|
||||
? { r: parseInt(result[1], 16), g: parseInt(result[2], 16), b: parseInt(result[3], 16) }
|
||||
: null;
|
||||
};
|
||||
|
||||
const Pill = ({ option, onRemove }) => {
|
||||
const rgb = option.color ? hexToRgb(option.color) : null;
|
||||
const pillStyle = rgb
|
||||
? { backgroundColor: `rgba(${rgb.r},${rgb.g},${rgb.b},0.15)`, borderColor: `rgba(${rgb.r},${rgb.g},${rgb.b},0.4)`, color: option.color }
|
||||
: {};
|
||||
const fallbackClass = !rgb
|
||||
? 'bg-neutral-100 dark:bg-neutral-700 border-neutral-200 dark:border-neutral-600 text-neutral-700 dark:text-neutral-300'
|
||||
: '';
|
||||
|
||||
return (
|
||||
<span
|
||||
className={`inline-flex items-center gap-1 px-2 py-0.5 rounded-full text-xs font-medium border ${fallbackClass}`}
|
||||
style={pillStyle}
|
||||
const Pill = ({ option, onRemove }) => (
|
||||
<span className="inline-flex items-center gap-1 px-2 py-0.5 rounded-full text-xs font-medium border bg-neutral-100 dark:bg-neutral-700 border-neutral-200 dark:border-neutral-600 text-neutral-700 dark:text-neutral-300">
|
||||
{option.label}
|
||||
<button
|
||||
type="button"
|
||||
onClick={(e) => { e.stopPropagation(); onRemove(); }}
|
||||
className="ml-0.5 hover:opacity-70 transition-opacity leading-none"
|
||||
aria-label={`Retirer ${option.label}`}
|
||||
>
|
||||
{option.color && (
|
||||
<span
|
||||
className="inline-block w-2 h-2 rounded-full flex-shrink-0"
|
||||
style={{ backgroundColor: option.color }}
|
||||
/>
|
||||
)}
|
||||
{option.label}
|
||||
<button
|
||||
type="button"
|
||||
onClick={(e) => { e.stopPropagation(); onRemove(); }}
|
||||
className="ml-0.5 hover:opacity-70 transition-opacity leading-none"
|
||||
aria-label={`Retirer ${option.label}`}
|
||||
>
|
||||
×
|
||||
</button>
|
||||
</span>
|
||||
);
|
||||
};
|
||||
×
|
||||
</button>
|
||||
</span>
|
||||
);
|
||||
|
||||
const TagInput = ({
|
||||
options = [],
|
||||
@@ -51,6 +25,7 @@ const TagInput = ({
|
||||
description,
|
||||
placeholder = 'Ajouter...',
|
||||
error,
|
||||
renderTag,
|
||||
}) => {
|
||||
const [inputValue, setInputValue] = useState('');
|
||||
const [isOpen, setIsOpen] = useState(false);
|
||||
@@ -129,7 +104,9 @@ const TagInput = ({
|
||||
onClick={() => { inputRef.current?.focus(); setIsOpen(true); }}
|
||||
>
|
||||
{selectedOptions.map(opt => (
|
||||
<Pill key={opt.value} option={opt} onRemove={() => removeOption(opt.value)} />
|
||||
renderTag
|
||||
? renderTag(opt, () => removeOption(opt.value))
|
||||
: <Pill key={opt.value} option={opt} onRemove={() => removeOption(opt.value)} />
|
||||
))}
|
||||
|
||||
<input
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
// Template components exports
|
||||
export { default as Badge, StatusBadge, TypeBadge } from './Badge';
|
||||
export { default as RoleBadge } from './RoleBadge';
|
||||
export { default as Button } from './Button';
|
||||
export { default as Card } from './Card';
|
||||
export { default as Input } from './Input';
|
||||
|
||||
Reference in New Issue
Block a user