@@ -3,23 +3,22 @@ import {
33 Flex ,
44 HStack ,
55 Icon ,
6- Text ,
6+ IconButton ,
77 Input ,
88 InputGroup ,
99 InputLeftElement ,
10- IconButton ,
11- useColorModeValue ,
10+ Text ,
1211 Tooltip ,
12+ useColorModeValue ,
1313} from "@chakra-ui/react"
14- import { set } from "lodash"
15- import type * as React from "react"
16- import { useState , useEffect , forwardRef } from "react"
14+ import { RxCornerBottomLeft } from "@react-icons/all-files/rx/RxCornerBottomLeft"
15+ import { RxCornerBottomRight } from "@react-icons/all-files/rx/RxCornerBottomRight"
1716import { RxCornerTopLeft } from "@react-icons/all-files/rx/RxCornerTopLeft"
1817import { RxCornerTopRight } from "@react-icons/all-files/rx/RxCornerTopRight"
19- import { RxCornerBottomRight } from "@react-icons/all-files/rx/RxCornerBottomRight"
20- import { RxCornerBottomLeft } from "@react-icons/all-files/rx/RxCornerBottomLeft"
2118import { TbBorderCorners } from "@react-icons/all-files/tb/TbBorderCorners"
22- import { FieldErrors } from "react-hook-form"
19+ import { set } from "lodash"
20+ import type * as React from "react"
21+ import { useEffect , useState } from "react"
2322
2423type RadiusMode = "uniform" | "individual"
2524
@@ -28,28 +27,43 @@ export type RadiusState = {
2827 radius : RadiusObject | string
2928}
3029
31- type RadiusInputFieldProps = {
32- id ?: string
33- onChange : ( value : RadiusState ) => void
34- errors ?: FieldErrors < Record < string , unknown > >
35- name : string ,
36- value ?: Partial < RadiusState >
37- }
38-
3930export type RadiusObject = {
4031 topLeft : string | "max"
4132 topRight : string | "max"
4233 bottomRight : string | "max"
4334 bottomLeft : string | "max"
4435}
4536
37+ type ErrorObject = {
38+ message : string
39+ }
40+
41+ type CornerErrors = {
42+ [ key in keyof RadiusObject ] ?: ErrorObject
43+ } & ErrorObject
44+
45+ export type RadiusErrors = Record <
46+ string ,
47+ {
48+ radius ?: CornerErrors
49+ }
50+ >
51+
52+ type RadiusInputFieldProps = {
53+ id ?: string
54+ onChange : ( value : RadiusState ) => void
55+ errors ?: RadiusErrors
56+ name : string
57+ value ?: Partial < RadiusState >
58+ }
59+
4660type RadiusDirection = "topLeft" | "topRight" | "bottomRight" | "bottomLeft"
4761
4862function getUpdatedRadiusValue (
4963 current : RadiusObject | string ,
5064 corner : RadiusDirection | "all" ,
5165 value : string ,
52- mode : "uniform" | "individual"
66+ mode : "uniform" | "individual" ,
5367) : RadiusObject | string {
5468 let inputValue : RadiusObject | number | string
5569 try {
@@ -60,24 +74,37 @@ function getUpdatedRadiusValue(
6074 if ( mode === "uniform" ) {
6175 if ( inputValue === "" ) {
6276 return ""
63- } else if ( typeof inputValue === "string" || typeof inputValue === "number" ) {
77+ } else if (
78+ typeof inputValue === "string" ||
79+ typeof inputValue === "number"
80+ ) {
6481 return inputValue . toString ( )
6582 } else {
6683 const { topLeft, topRight, bottomRight, bottomLeft } = inputValue
67- if ( topLeft === topRight && topLeft === bottomRight && topLeft === bottomLeft ) {
84+ if (
85+ topLeft === topRight &&
86+ topLeft === bottomRight &&
87+ topLeft === bottomLeft
88+ ) {
6889 return topLeft
6990 } else {
70- return "" ;
91+ return ""
7192 }
7293 }
7394 } else {
7495 let commonValue : string = ""
7596 if ( typeof inputValue === "string" || typeof inputValue === "number" ) {
7697 commonValue = inputValue . toString ( )
7798 }
78- const updatedRadius = current && typeof current === "object"
79- ? { ...current }
80- : { topLeft : commonValue , topRight : commonValue , bottomRight : commonValue , bottomLeft : commonValue }
99+ const updatedRadius =
100+ current && typeof current === "object"
101+ ? { ...current }
102+ : {
103+ topLeft : commonValue ,
104+ topRight : commonValue ,
105+ bottomRight : commonValue ,
106+ bottomLeft : commonValue ,
107+ }
81108 if ( corner !== "all" ) {
82109 set ( updatedRadius , corner , inputValue . toString ( ) )
83110 }
@@ -90,29 +117,36 @@ export const RadiusInputField: React.FC<RadiusInputFieldProps> = ({
90117 onChange,
91118 errors,
92119 name : propertyName ,
93- value
120+ value,
94121} ) => {
95- const [ radiusMode , setRadiusMode ] = useState < RadiusMode > ( value ?. mode ?? "uniform" )
96- const [ radiusValue , setRadiusValue ] = useState < RadiusObject | string > ( value ?. radius ?? "" )
122+ const [ radiusMode , setRadiusMode ] = useState < RadiusMode > (
123+ value ?. mode ?? "uniform" ,
124+ )
125+ const [ radiusValue , setRadiusValue ] = useState < RadiusObject | string > (
126+ value ?. radius ?? "" ,
127+ )
97128 const errorRed = useColorModeValue ( "red.500" , "red.300" )
98129 const activeColor = useColorModeValue ( "blue.500" , "blue.600" )
99130 const inactiveColor = useColorModeValue ( "gray.600" , "gray.400" )
100131
132+ // biome-ignore lint/correctness/useExhaustiveDependencies: <causes re-render loop if added>
101133 useEffect ( ( ) => {
102- const formatRadiusValue = ( value : RadiusObject | string ) : string | RadiusObject => {
134+ const formatRadiusValue = (
135+ value : RadiusObject | string ,
136+ ) : string | RadiusObject => {
103137 if ( value === "" ) return ""
104138 if ( typeof value === "string" ) {
105139 return value
106140 } else {
107- return value ;
141+ return value
108142 }
109143 }
110144 const formattedValue = formatRadiusValue ( radiusValue )
111145 onChange ( { mode : radiusMode , radius : formattedValue } )
112146 } , [ radiusValue , radiusMode ] )
113147
114-
115148 return (
149+ // biome-ignore lint/a11y/useSemanticElements: <role used to concur to chakra standard>
116150 < HStack
117151 as = "fieldset"
118152 id = { id }
@@ -127,27 +161,35 @@ export const RadiusInputField: React.FC<RadiusInputFieldProps> = ({
127161 < Input
128162 onChange = { ( e ) => {
129163 const val = e . target . value
130- setRadiusValue ( getUpdatedRadiusValue (
131- radiusValue ,
132- "all" ,
133- val ,
134- radiusMode
135- ) )
164+ setRadiusValue (
165+ getUpdatedRadiusValue ( radiusValue , "all" , val , radiusMode ) ,
166+ )
136167 } }
137168 value = { typeof radiusValue === "string" ? radiusValue : "" }
138169 placeholder = "Uniform Radius"
139170 isInvalid = { ! ! errors ?. [ propertyName ] ?. radius }
140171 fontSize = "sm"
141172 />
142- < Text fontSize = 'xs' color = { errorRed } > { errors ?. [ propertyName ] ?. radius ?. message } </ Text >
173+ < Text fontSize = "xs" color = { errorRed } >
174+ { errors ?. [ propertyName ] ?. radius ?. message }
175+ </ Text >
143176 </ Box >
144177 ) : (
178+ // biome-ignore lint/complexity/noUselessFragments: <fragment is required otherwise syntax breaks>
145179 < >
146180 { [
147181 { name : "topLeft" , label : "Top Left" , icon : RxCornerTopLeft } ,
148182 { name : "topRight" , label : "Top Right" , icon : RxCornerTopRight } ,
149- { name : "bottomLeft" , label : "Bottom Left" , icon : RxCornerBottomLeft } ,
150- { name : "bottomRight" , label : "Bottom Right" , icon : RxCornerBottomRight } ,
183+ {
184+ name : "bottomLeft" ,
185+ label : "Bottom Left" ,
186+ icon : RxCornerBottomLeft ,
187+ } ,
188+ {
189+ name : "bottomRight" ,
190+ label : "Bottom Right" ,
191+ icon : RxCornerBottomRight ,
192+ } ,
151193 ] . map ( ( { name, label, icon } ) => (
152194 < Box flex = "1 1 calc(50% - 4px)" key = { name } >
153195 < InputGroup >
@@ -157,53 +199,79 @@ export const RadiusInputField: React.FC<RadiusInputFieldProps> = ({
157199 < Input
158200 onChange = { ( e ) => {
159201 const val = e . target . value
160- setRadiusValue ( getUpdatedRadiusValue (
161- radiusValue ,
162- name as RadiusDirection ,
163- val ,
164- radiusMode
165- ) )
202+ setRadiusValue (
203+ getUpdatedRadiusValue (
204+ radiusValue ,
205+ name as RadiusDirection ,
206+ val ,
207+ radiusMode ,
208+ ) ,
209+ )
166210 } }
167- value = { typeof radiusValue === "object" ? radiusValue ?. [ name as RadiusDirection ] ?? "" : "" }
211+ value = {
212+ typeof radiusValue === "object"
213+ ? ( radiusValue ?. [ name as RadiusDirection ] ?? "" )
214+ : ""
215+ }
168216 placeholder = { label }
169- isInvalid = { ! ! errors ?. [ propertyName ] ?. radius ?. [ name as RadiusDirection ] }
217+ isInvalid = {
218+ ! ! errors ?. [ propertyName ] ?. radius ?. [
219+ name as RadiusDirection
220+ ]
221+ }
170222 fontSize = "sm"
171223 />
172224 </ InputGroup >
173- < Text fontSize = 'xs' color = { errorRed } > { errors ?. [ propertyName ] ?. radius ?. [ name as RadiusDirection ] ?. message } </ Text >
225+ < Text fontSize = "xs" color = { errorRed } >
226+ {
227+ errors ?. [ propertyName ] ?. radius ?. [ name as RadiusDirection ]
228+ ?. message
229+ }
230+ </ Text >
174231 </ Box >
175232 ) ) }
176233 </ >
177234 ) }
178235 </ Flex >
179236 < Tooltip
180237 hasArrow
181- label = { radiusMode === "uniform" ? "Enable individual radius" : "Disable individual radius" }
238+ label = {
239+ radiusMode === "uniform"
240+ ? "Enable individual radius"
241+ : "Disable individual radius"
242+ }
182243 openDelay = { 200 }
183244 modifiers = { [
184245 {
185- name : ' zIndex' ,
246+ name : " zIndex" ,
186247 enabled : true ,
187- phase : ' write' ,
248+ phase : " write" ,
188249 fn ( { state } ) {
189- state . elements . popper . style . zIndex = ' 2100' ;
250+ state . elements . popper . style . zIndex = " 2100"
190251 } ,
191252 } ,
192253 ] }
193254 >
194255 < IconButton
195- aria-label = { radiusMode === "uniform" ? "Switch to individual radius" : "Switch to uniform radius" }
256+ aria-label = {
257+ radiusMode === "uniform"
258+ ? "Switch to individual radius"
259+ : "Switch to uniform radius"
260+ }
196261 aria-pressed = { radiusMode === "individual" }
197262 icon = { < TbBorderCorners size = { 20 } /> }
198263 padding = "0.05em"
199264 onClick = { ( ) => {
200- const newRadiusMode = radiusMode === "uniform" ? "individual" : "uniform"
201- setRadiusValue ( getUpdatedRadiusValue (
202- radiusValue ,
203- "all" ,
204- JSON . stringify ( radiusValue ) ,
205- newRadiusMode
206- ) )
265+ const newRadiusMode =
266+ radiusMode === "uniform" ? "individual" : "uniform"
267+ setRadiusValue (
268+ getUpdatedRadiusValue (
269+ radiusValue ,
270+ "all" ,
271+ JSON . stringify ( radiusValue ) ,
272+ newRadiusMode ,
273+ ) ,
274+ )
207275 setRadiusMode ( newRadiusMode )
208276 } }
209277 variant = "outline"
0 commit comments