mirror of
https://github.com/CommunityOx/ox_lib.git
synced 2026-08-18 23:46:03 +01:00
feat(web/input): support default values (#66)
* feat(web/dialog): Support placeholders * feat(web/dialog): Support default values * feat(web/dialog): Support default state of checkbox * feat(web/dialog): Support placeholder for number input * feat(web/dialog): Support default value for select
This commit is contained in:
@@ -30,10 +30,10 @@ interface Props {
|
|||||||
// data: {
|
// data: {
|
||||||
// heading: "Police locker",
|
// heading: "Police locker",
|
||||||
// rows: [
|
// rows: [
|
||||||
// { type: "input", label: "Locker number" },
|
// { type: "input", label: "Locker number", placeholder: "420" },
|
||||||
// { type: "checkbox", label: "Some checkbox" },
|
// { type: "checkbox", label: "Some checkbox" },
|
||||||
// { type: "input", label: "Locker PIN", password: true, icon: "lock" },
|
// { type: "input", label: "Locker PIN", password: true, icon: "lock" },
|
||||||
// { type: "checkbox", label: "Some other checkbox" },
|
// { type: "checkbox", label: "Some other checkbox", checked: true },
|
||||||
// {
|
// {
|
||||||
// type: "select",
|
// type: "select",
|
||||||
// label: "Locker type",
|
// label: "Locker type",
|
||||||
@@ -43,7 +43,7 @@ interface Props {
|
|||||||
// { value: "option3", label: "Option 3" },
|
// { value: "option3", label: "Option 3" },
|
||||||
// ],
|
// ],
|
||||||
// },
|
// },
|
||||||
// { type: "number", label: "Number counter" },
|
// { type: "number", label: "Number counter", default: 12 },
|
||||||
// ],
|
// ],
|
||||||
// },
|
// },
|
||||||
// },
|
// },
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
import { Box, Checkbox } from "@chakra-ui/react";
|
import { Box, Checkbox } from "@chakra-ui/react";
|
||||||
|
import { useEffect } from "react";
|
||||||
import { Row } from "../../../interfaces/dialog";
|
import { Row } from "../../../interfaces/dialog";
|
||||||
|
|
||||||
interface Props {
|
interface Props {
|
||||||
@@ -8,11 +9,16 @@ interface Props {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const CheckboxField: React.FC<Props> = (props) => {
|
const CheckboxField: React.FC<Props> = (props) => {
|
||||||
|
useEffect(() => {
|
||||||
|
if(props.row.checked) props.handleChange(props.row.checked, props.index);
|
||||||
|
}, []);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<Box mb={3} key={`checkbox-${props.index}`}>
|
<Box mb={3} key={`checkbox-${props.index}`}>
|
||||||
<Checkbox
|
<Checkbox
|
||||||
onChange={(e) => props.handleChange(e.target.checked, props.index)}
|
onChange={(e) => props.handleChange(e.target.checked, props.index)}
|
||||||
|
defaultChecked={props.row.checked}
|
||||||
>
|
>
|
||||||
{props.row.label}
|
{props.row.label}
|
||||||
</Checkbox>
|
</Checkbox>
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ import {
|
|||||||
Input,
|
Input,
|
||||||
} from "@chakra-ui/react";
|
} from "@chakra-ui/react";
|
||||||
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
|
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
|
||||||
|
import { useEffect } from "react";
|
||||||
import { Row } from "../../../interfaces/dialog";
|
import { Row } from "../../../interfaces/dialog";
|
||||||
|
|
||||||
interface Props {
|
interface Props {
|
||||||
@@ -18,6 +19,10 @@ interface Props {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const InputField: React.FC<Props> = (props) => {
|
const InputField: React.FC<Props> = (props) => {
|
||||||
|
useEffect(() => {
|
||||||
|
if(props.row.default) props.handleChange(props.row.default, props.index);
|
||||||
|
}, []);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<Box mb={3} textAlign="left">
|
<Box mb={3} textAlign="left">
|
||||||
@@ -31,6 +36,8 @@ const InputField: React.FC<Props> = (props) => {
|
|||||||
)}
|
)}
|
||||||
<Input
|
<Input
|
||||||
onChange={(e) => props.handleChange(e.target.value, props.index)}
|
onChange={(e) => props.handleChange(e.target.value, props.index)}
|
||||||
|
placeholder={props.row.placeholder}
|
||||||
|
defaultValue={props.row.default}
|
||||||
type={
|
type={
|
||||||
!props.row.password || props.passwordStates[props.index]
|
!props.row.password || props.passwordStates[props.index]
|
||||||
? "text"
|
? "text"
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ import {
|
|||||||
NumberIncrementStepper,
|
NumberIncrementStepper,
|
||||||
NumberDecrementStepper,
|
NumberDecrementStepper,
|
||||||
} from "@chakra-ui/react";
|
} from "@chakra-ui/react";
|
||||||
|
import { useEffect } from "react";
|
||||||
import { Row } from "../../../interfaces/dialog";
|
import { Row } from "../../../interfaces/dialog";
|
||||||
|
|
||||||
interface Props {
|
interface Props {
|
||||||
@@ -16,11 +17,15 @@ interface Props {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const InputNumber: React.FC<Props> = (props) => {
|
const InputNumber: React.FC<Props> = (props) => {
|
||||||
|
useEffect(() => {
|
||||||
|
if(props.row.default) props.handleChange(props.row.default, props.index);
|
||||||
|
}, []);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Box mb={3}>
|
<Box mb={3}>
|
||||||
<Text>{props.row.label}</Text>
|
<Text>{props.row.label}</Text>
|
||||||
<NumberInput onChange={(e) => props.handleChange(+e, props.index)}>
|
<NumberInput onChange={(e) => props.handleChange(+e, props.index)} defaultValue={props.row.default}>
|
||||||
<NumberInputField />
|
<NumberInputField placeholder={props.row.placeholder} />
|
||||||
<NumberInputStepper>
|
<NumberInputStepper>
|
||||||
<NumberIncrementStepper />
|
<NumberIncrementStepper />
|
||||||
<NumberDecrementStepper />
|
<NumberDecrementStepper />
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
import { Box, Select } from "@chakra-ui/react";
|
import { Box, Select } from "@chakra-ui/react";
|
||||||
|
import { useEffect } from "react";
|
||||||
import { Row } from "../../../interfaces/dialog";
|
import { Row } from "../../../interfaces/dialog";
|
||||||
|
|
||||||
interface Props {
|
interface Props {
|
||||||
@@ -8,11 +9,22 @@ interface Props {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const SelectField: React.FC<Props> = (props) => {
|
const SelectField: React.FC<Props> = (props) => {
|
||||||
|
useEffect(() => {
|
||||||
|
if(props.row.default) {
|
||||||
|
props.row.options?.map((option) => {
|
||||||
|
if(props.row.default === option.value) {
|
||||||
|
props.handleChange(option.value, props.index);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}, []);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<Box mb={3} key={`select-${props.index}`}>
|
<Box mb={3} key={`select-${props.index}`}>
|
||||||
<Select
|
<Select
|
||||||
onChange={(e) => props.handleChange(e.target.value, props.index)}
|
onChange={(e) => props.handleChange(e.target.value, props.index)}
|
||||||
|
defaultValue={props.row.default}
|
||||||
>
|
>
|
||||||
{/* Hacky workaround for selectable placeholder issue */}
|
{/* Hacky workaround for selectable placeholder issue */}
|
||||||
<option value="" selected hidden disabled>
|
<option value="" selected hidden disabled>
|
||||||
|
|||||||
@@ -5,6 +5,9 @@ type RowType = "input" | "checkbox" | "select" | "number";
|
|||||||
export interface Row {
|
export interface Row {
|
||||||
type: RowType;
|
type: RowType;
|
||||||
label: string;
|
label: string;
|
||||||
|
placeholder?: string;
|
||||||
|
default?: string | number;
|
||||||
|
checked?: boolean;
|
||||||
options?: { value: string; label?: string }[];
|
options?: { value: string; label?: string }[];
|
||||||
password?: boolean;
|
password?: boolean;
|
||||||
icon?: IconProp;
|
icon?: IconProp;
|
||||||
|
|||||||
Reference in New Issue
Block a user