1
+ import { RESERVED_COLORS } from '@digdir/designsystemet' ;
1
2
import {
2
3
Button ,
3
4
Heading ,
@@ -10,6 +11,7 @@ import { ColorPicker, type IColor } from 'react-color-palette';
10
11
import { useThemeStore } from '../../../store' ;
11
12
12
13
import cl from 'clsx/lite' ;
14
+ import { useState } from 'react' ;
13
15
import classes from './ColorPane.module.css' ;
14
16
15
17
type ColorPaneProps = {
@@ -38,13 +40,36 @@ export const ColorPane = ({
38
40
colorType,
39
41
} : ColorPaneProps ) => {
40
42
const mainColors = useThemeStore ( ( state ) => state . colors . main ) ;
43
+ const [ colorError , setColorError ] = useState ( '' ) ;
44
+
41
45
const disableRemoveButton = colorType === 'main' && mainColors . length === 1 ;
42
46
43
47
const getHeading = ( ) => {
44
48
const t = colorType === 'main' ? 'hovedfarge' : 'støttefarge' ;
45
49
return type === 'addColor' ? 'Legg til ' + t : 'Rediger farge' ;
46
50
} ;
47
51
52
+ const checkNameIsValid = ( ) => {
53
+ if ( name === '' ) {
54
+ setColorError ( 'Navnet på fargen kan ikke være tomt' ) ;
55
+ return false ;
56
+ }
57
+
58
+ if ( RESERVED_COLORS . includes ( name . toLowerCase ( ) ) ) {
59
+ setColorError (
60
+ 'Navnet på fargen kan ikke være det samme som våre systemfarger' ,
61
+ ) ;
62
+ return false ;
63
+ }
64
+ setColorError ( '' ) ;
65
+ return true ;
66
+ } ;
67
+
68
+ const closeTab = ( ) => {
69
+ setColorError ( '' ) ;
70
+ onClose ( ) ;
71
+ } ;
72
+
48
73
return (
49
74
< div
50
75
className = { cl ( classes . colorPage , type . includes ( 'Color' ) && classes . show ) }
@@ -53,7 +78,7 @@ export const ColorPane = ({
53
78
< Button
54
79
data-size = 'sm'
55
80
variant = 'tertiary'
56
- onClick = { ( ) => onClose ( ) }
81
+ onClick = { closeTab }
57
82
className = { classes . back }
58
83
>
59
84
< ChevronLeftIcon title = 'a11y-title' fontSize = '1.5rem' /> Gå tilbake
@@ -102,6 +127,8 @@ export const ColorPane = ({
102
127
. toLowerCase ( ) ;
103
128
setName ( value ) ;
104
129
} }
130
+ onBlur = { checkNameIsValid }
131
+ error = { colorError }
105
132
/>
106
133
) }
107
134
< div className = { classes . label } > Farge</ div >
@@ -122,6 +149,8 @@ export const ColorPane = ({
122
149
data-size = 'sm'
123
150
color = 'neutral'
124
151
onClick = { ( ) => {
152
+ /* Check here as well to disable sending new color */
153
+ if ( ! checkNameIsValid ( ) ) return ;
125
154
onPrimaryClicked ( color . hex , name ) ;
126
155
} }
127
156
>
@@ -132,9 +161,7 @@ export const ColorPane = ({
132
161
data-size = 'sm'
133
162
color = 'neutral'
134
163
variant = 'secondary'
135
- onClick = { ( ) => {
136
- onClose ( ) ;
137
- } }
164
+ onClick = { closeTab }
138
165
>
139
166
Avbryt
140
167
</ Button >
0 commit comments