Skip to content

Commit d5cfda0

Browse files
committed
Refactor UI components by standardizing Input and Textarea props and styling, implementing Textarea as a forward-ref component, and integrating Input for numerical display in the Slider component
1 parent 830438f commit d5cfda0

8 files changed

Lines changed: 167 additions & 169 deletions

File tree

‎packages/ui/src/components/editor/common/Input/Input.tsx‎

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,25 @@ import styles from './Input.module.scss'
33
type Props = {
44
id?: string
55
type?: string
6-
value: string
6+
value: string | number
77
placeholder?: string
88
max_width?: number
9+
width?: number
910
onChange: (value: string) => void
1011
onKeyDown?: (e: React.KeyboardEvent<HTMLInputElement>) => void
12+
min?: number
13+
max?: number
14+
step?: number
1115
}
1216

1317
export const Input: React.FC<Props> = (props) => {
14-
const style = props.max_width
15-
? { maxWidth: `${props.max_width}px` }
16-
: undefined
18+
const style: React.CSSProperties = {}
19+
if (props.max_width) {
20+
style.maxWidth = `${props.max_width}px`
21+
}
22+
if (props.width) {
23+
style.width = `${props.width}px`
24+
}
1725

1826
return (
1927
<input
@@ -24,7 +32,10 @@ export const Input: React.FC<Props> = (props) => {
2432
onKeyDown={props.onKeyDown}
2533
className={styles.input}
2634
placeholder={props.placeholder}
27-
style={style}
35+
style={Object.keys(style).length > 0 ? style : undefined}
36+
min={props.min}
37+
max={props.max}
38+
step={props.step}
2839
/>
2940
)
3041
}

‎packages/ui/src/components/editor/common/Textarea/Textarea.module.scss‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
border: 1px solid var(--cwc-border-color);
33
background: var(--vscode-input-background);
44
color: var(--vscode-input-foreground);
5-
padding: var(--padding-4px) var(--padding-8px);
5+
padding: var(--padding-4px) var(--padding-7px);
66
border-radius: var(--border-radius-4px);
77
width: 100%;
88
resize: none;
Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,35 @@
1+
import React from 'react'
12
import TextareaAutosize from 'react-textarea-autosize'
23
import styles from './Textarea.module.scss'
34

45
type Props = {
56
id?: string
6-
value: string
7+
value?: string
78
placeholder?: string
89
min_rows?: number
910
max_rows?: number
1011
on_change: (value: string) => void
1112
on_key_down?: (e: React.KeyboardEvent<HTMLTextAreaElement>) => void
13+
onFocus?: (e: React.FocusEvent<HTMLTextAreaElement>) => void
1214
}
1315

14-
export const Textarea: React.FC<Props> = (props) => {
15-
return (
16-
<TextareaAutosize
17-
id={props.id}
18-
value={props.value}
19-
onChange={(e) => props.on_change(e.target.value)}
20-
onKeyDown={props.on_key_down}
21-
className={styles.textarea}
22-
placeholder={props.placeholder}
23-
minRows={props.min_rows}
24-
maxRows={props.max_rows}
25-
/>
26-
)
27-
}
16+
export const Textarea = React.forwardRef<HTMLTextAreaElement, Props>(
17+
(props, ref) => {
18+
return (
19+
<TextareaAutosize
20+
id={props.id}
21+
value={props.value}
22+
onChange={(e) => props.on_change(e.target.value)}
23+
onKeyDown={props.on_key_down}
24+
className={styles.textarea}
25+
placeholder={props.placeholder}
26+
minRows={props.min_rows}
27+
maxRows={props.max_rows}
28+
onFocus={props.onFocus}
29+
ref={ref}
30+
/>
31+
)
32+
}
33+
)
34+
35+
Textarea.displayName = 'Textarea'

‎packages/ui/src/components/editor/panel/Field/Field.module.scss‎

Lines changed: 0 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -14,55 +14,4 @@
1414
font-size: 12px;
1515
color: var(--vscode-descriptionForeground);
1616
}
17-
18-
input:not([type='checkbox']),
19-
select {
20-
background-color: var(--vscode-input-background);
21-
color: var(--vscode-input-foreground);
22-
border-radius: var(--border-radius-3px);
23-
padding: var(--padding-4px) var(--padding-6px);
24-
font-size: var(--font-size-13px);
25-
border: none;
26-
27-
&:focus {
28-
outline: 1px solid var(--vscode-focusBorder);
29-
outline-offset: -1px;
30-
}
31-
32-
&:disabled {
33-
opacity: 0.5;
34-
}
35-
}
36-
37-
select {
38-
width: 100%;
39-
cursor: pointer;
40-
border: none;
41-
padding: var(--padding-4px) var(--padding-2px);
42-
}
43-
44-
input[type='range'] {
45-
padding: 0;
46-
&:focus {
47-
outline: none;
48-
}
49-
}
50-
51-
textarea {
52-
background-color: var(--vscode-input-background);
53-
color: var(--vscode-input-foreground);
54-
border-radius: var(--border-radius-3px);
55-
resize: none;
56-
overflow: hidden;
57-
font-family: inherit;
58-
font-size: var(--font-size-13px);
59-
padding: var(--padding-5px) var(--padding-6px);
60-
border: none;
61-
width: 100%;
62-
63-
&:focus {
64-
outline: 1px solid var(--vscode-focusBorder);
65-
outline-offset: -1px;
66-
}
67-
}
6817
}

‎packages/ui/src/components/editor/panel/Slider/Slider.module.scss‎

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,22 @@
1515
}
1616

1717
&::-webkit-slider-runnable-track {
18-
background: var(--vscode-input-foreground);
19-
height: 1px;
18+
background: linear-gradient(
19+
to right,
20+
var(--vscode-list-activeSelectionBackground) var(--value-percent),
21+
var(--vscode-dropdown-border) var(--value-percent)
22+
);
23+
height: 3px;
24+
border-radius: var(--border-radius-999px);
2025
}
2126

2227
&::-webkit-slider-thumb {
2328
appearance: none;
24-
margin-top: -5.5px;
29+
margin-top: -4.5px;
2530
background-color: var(--vscode-button-background);
2631
height: 12px;
2732
width: 12px;
2833
border-radius: 50%;
2934
}
3035
}
31-
32-
&__input {
33-
width: 60px;
34-
}
3536
}

‎packages/ui/src/components/editor/panel/Slider/Slider.stories.tsx‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,12 @@ export const Primary = () => {
99
const [value, set_value] = useState(0.7)
1010
return (
1111
<div style={{ padding: '20px', width: '300px' }}>
12-
<Slider value={value} onChange={(newValue) => set_value(newValue)} />
12+
<Slider
13+
value={value}
14+
onChange={(newValue) => set_value(newValue)}
15+
min={0}
16+
max={2}
17+
/>
1318
<div
1419
style={{
1520
marginTop: '10px',

‎packages/ui/src/components/editor/panel/Slider/Slider.tsx‎

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,54 @@
1-
import { useState } from 'react'
1+
import { useEffect, useState } from 'react'
2+
import { Input } from '../../common/Input'
23
import styles from './Slider.module.scss'
34

45
type Props = {
56
value: number
67
onChange: (value: number) => void
8+
min: number
9+
max: number
710
}
811

912
export const Slider: React.FC<Props> = (props) => {
1013
const [value, set_value] = useState(props.value)
1114

15+
useEffect(() => {
16+
set_value(props.value)
17+
}, [props.value])
18+
19+
const handleInputChange = (newValue: string) => {
20+
const numValue = parseFloat(newValue)
21+
if (!isNaN(numValue)) {
22+
set_value(numValue)
23+
if (numValue >= props.min && numValue <= props.max) {
24+
props.onChange(numValue)
25+
}
26+
}
27+
}
28+
29+
const percentage = ((value - props.min) / (props.max - props.min)) * 100
30+
1231
return (
1332
<div className={styles.container}>
14-
<input
33+
<Input
1534
type="number"
16-
min="0"
17-
max="2"
18-
step="0.05"
35+
min={props.min}
36+
max={props.max}
37+
step={0.05}
1938
value={value}
20-
onChange={(e) => {
21-
set_value(parseFloat(e.target.value))
22-
const value = parseFloat(e.target.value)
23-
if (!isNaN(value) && value >= 0 && value <= 1) {
24-
props.onChange(value)
25-
}
26-
}}
27-
className={styles.container__input}
39+
onChange={handleInputChange}
40+
width={60}
2841
/>
2942
<input
3043
type="range"
31-
min="0"
32-
max="2"
44+
min={String(props.min)}
45+
max={String(props.max)}
3346
step="0.05"
3447
value={value}
3548
onChange={(e) => set_value(parseFloat(e.target.value))}
3649
onMouseUp={() => props.onChange(value)}
3750
className={styles.container__slider}
51+
style={{ '--value-percent': `${percentage}%` } as any}
3852
/>
3953
</div>
4054
)

0 commit comments

Comments
 (0)