Skip to content

Commit e5fb771

Browse files
committed
Add select for unit
1 parent a9fbde5 commit e5fb771

File tree

3 files changed

+48
-12
lines changed

3 files changed

+48
-12
lines changed

frontend/block/editor.scss

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@
1717
align-items: center;
1818
justify-content: center;
1919

20+
& > input {
21+
width: 80%;
22+
}
23+
2024
& > span {
2125
margin-left: 5px;
2226
}

frontend/block/inspector/StylePanel.tsx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ export default function StylePanel({ attributes, setAttributes }): WPElement {
1414
label={__("height", "chatrix")}
1515
value={attributes.height.value}
1616
unit={attributes.height.unit}
17-
onChange={(value) => {
18-
setAttributes({ height: { value: value, unit: "px" } });
17+
onChange={(value, unit) => {
18+
setAttributes({ height: { value: value, unit: unit } });
1919
}}
2020
/>
2121
</PanelRow>
@@ -24,8 +24,8 @@ export default function StylePanel({ attributes, setAttributes }): WPElement {
2424
label={__("border width", "chatrix")}
2525
value={attributes.borderWidth.value}
2626
unit={attributes.borderWidth.unit}
27-
onChange={(value) => {
28-
setAttributes({ borderWidth: { value: value, unit: "px" } });
27+
onChange={(value, unit) => {
28+
setAttributes({ borderWidth: { value: value, unit: unit } });
2929
}}
3030
/>
3131
</PanelRow>
@@ -52,8 +52,8 @@ export default function StylePanel({ attributes, setAttributes }): WPElement {
5252
label={__("border radius", "chatrix")}
5353
value={attributes.borderRadius.value}
5454
unit={attributes.borderRadius.unit}
55-
onChange={(value) => {
56-
setAttributes({ borderRadius: { value: value, unit: "px" } });
55+
onChange={(value, unit) => {
56+
setAttributes({ borderRadius: { value: value, unit: unit } });
5757
}}
5858
/>
5959
</PanelRow>

frontend/block/inspector/TextControlWithUnit.tsx

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,53 @@
11
import { BaseControl } from "@wordpress/components";
22
import { useInstanceId } from "@wordpress/compose";
3-
import { forwardRef } from "@wordpress/element";
4-
import type { ChangeEvent, ForwardedRef } from 'react';
3+
import { forwardRef, useState } from "@wordpress/element";
4+
import type { ChangeEvent, FormEvent, ForwardedRef } from 'react';
55

66
interface Props {
77
value: number
88
unit: string
9-
onChange: any
9+
onChange: (value: number, unit: string) => any
1010
label: string
1111
help?: string
1212
}
1313

14+
interface State {
15+
value: number;
16+
unit: string;
17+
}
18+
1419
export const TextControlWithUnit = forwardRef(UnforwardedTextControlWithUnit);
1520
export default TextControlWithUnit;
1621

1722
function UnforwardedTextControlWithUnit(props: Props, ref: ForwardedRef<HTMLInputElement>) {
1823
const instanceId = useInstanceId(TextControlWithUnit);
1924
const id = `inspector-text-control-${instanceId}`;
20-
const onChangeValue = (event: ChangeEvent<HTMLInputElement>) => props.onChange(event.target.value);
25+
const units = ["px"];
26+
27+
const [state, setState] = useState<State>({
28+
value: props.value,
29+
unit: props.unit,
30+
});
31+
32+
const onChangeValue = (event: ChangeEvent<HTMLInputElement>) => {
33+
const target = event.target as HTMLInputElement;
34+
const value = +target.value;
35+
setState({
36+
...state,
37+
value: value,
38+
});
39+
props.onChange(value, state.unit);
40+
};
41+
42+
const onChangeUnit = (event: FormEvent<HTMLSelectElement>) => {
43+
const target = event.target as HTMLSelectElement;
44+
const unit = target.value;
45+
setState({
46+
...state,
47+
unit: unit,
48+
});
49+
props.onChange(state.value, unit);
50+
};
2151

2252
return (
2353
<div className={"chatrix-text-control-with-unit"}>
@@ -31,12 +61,14 @@ function UnforwardedTextControlWithUnit(props: Props, ref: ForwardedRef<HTMLInpu
3161
className="components-text-control__input"
3262
type={"text"}
3363
id={id}
34-
value={props.value}
64+
value={state.value}
3565
onChange={onChangeValue}
3666
aria-describedby={!!props.help ? id + '__help' : undefined}
3767
ref={ref}
3868
/>
39-
<span>{props.unit}</span>
69+
<select value={state.unit} onChange={onChangeUnit}>
70+
{units.map(unit => <option value={unit} key={unit}>{unit}</option>)}
71+
</select>
4072
</span>
4173
</BaseControl>
4274
</div>

0 commit comments

Comments
 (0)