From 94754163127c6c4bf168b2060b6eeb2f1474d5fe Mon Sep 17 00:00:00 2001 From: Quentin Date: Thu, 13 Jan 2022 15:16:26 +0100 Subject: [PATCH] Add new format - buttonsSelect --- README.md | 1 + src/form.js | 3 ++ src/format.ts | 3 +- src/inputs/SelectInput.js | 34 ++++++++++++++++++++++ src/style.js | 61 +++++++++++++++------------------------ 5 files changed, 63 insertions(+), 39 deletions(-) diff --git a/README.md b/README.md index 9d2f1d6..048c780 100644 --- a/README.md +++ b/README.md @@ -66,6 +66,7 @@ export const Example = () => { - **type** (required): the type of value. It provided by the imported object `type`. It could be just string like `string`, `number`, `bool`, `object`, `date` or `file` - **format**: Over the type you can display a special format for the field. It provided by the imported object `format` or just a string - `select`: display a [react-select](https://react-select.com/home) field with provided options + - `buttonsSelect`: display a buttons group, drawn with the same options than the format `select` - `code`: if the type is `string`, display a code input (draw with [react-ace](https://github.com/securingsincity/react-ace)) - `markdown`: if the type is `string`, display a markdown input - `text`: if the type is `string`, display a textarea diff --git a/src/form.js b/src/form.js index a4c0845..42a7c21 100644 --- a/src/form.js +++ b/src/form.js @@ -360,6 +360,7 @@ const Step = ({ entry, step, error, register, schema, control, trigger, getValue }} /> ) + case format.buttonsSelect: case format.select: return ( { if (value === null) { @@ -20,6 +23,7 @@ const valueToSelectOption = (value, possibleValues = [], isMulti = false, maybeT }; export const SelectInput = (props) => { + const classes = useCustomStyle() const possibleValues = (props.possibleValues || []) .map(v => props.transformer ? props.transformer(v) : v) .map(v => ({ @@ -80,6 +84,36 @@ export const SelectInput = (props) => { } } + const handleSelectButtonClick = (v) => { + if (props.isMulti) { + if (value.includes(v)) { + return onChange(value.filter(val => val.value !== v.value)) + } else { + return onChange([...value, v]) + } + } + return onChange(v) + } + + if (props.format === format.buttonsSelect) { + return ( +
+ {values.map((v, idx) => { + const active = props.isMulti ? value.includes(v) : v.value === value.value + return ( + + ) + })} +
+ ) + } + if (props.createOption) { return ( ({ + color, + borderColor: color, + "&:hover": { + color: '#fff', + backgroundColor: color, + borderColor: darker, + }, + "&.active": { + color: '#fff', + backgroundColor: color, + borderColor: darker, + }, + +}) + export const style = { input: { display: "block", @@ -17,7 +33,8 @@ export const style = { padding: 10, fontSize: "1rem", cursor: "pointer", - border: 0, + borderWidth: '1px', + backgroundColor: '#fff' }, btn_sm: { fontSize: "0.875rem", @@ -33,42 +50,10 @@ export const style = { borderBottomLeftRadius: 0, }, }, - btn_red: { - color: "#fff", - backgroundColor: "#dc3545", - borderColor: "#dc3545", - "&:hover": { - backgroundColor: "#c82333", - borderColor: "#bd2130", - }, - }, - btn_green: { - color: "#fff", - backgroundColor: "#28a745", - borderColor: "#28a745", - "&:hover": { - backgroundColor: "#218838", - borderColor: "#1e7e34", - }, - }, - btn_blue: { - color: "#fff", - backgroundColor: "#007bff", - borderColor: "#007bff", - "&:hover": { - backgroundColor: "#0069d9", - borderColor: "#0062cc", - }, - }, - btn_grey: { - color: "#fff", - backgroundColor: "#6c757d", - borderColor: "#6c757d", - "&:hover": { - backgroundColor: "#5c636a", - borderColor: "#5c636a", - }, - }, + btn_red: buttonOutline("#dc3545", "#bd2130"), + btn_green: buttonOutline("#28a745", "#1e7e34"), + btn_blue: buttonOutline("#007bff", "#0069d9"), + btn_grey: buttonOutline("#6c757d", "#5c636a"), txt_red: { color: "#dc3545", }, @@ -149,7 +134,7 @@ export const style = { color: "tomato" }, input__invalid: { - borderColor: '#dc3545' + borderColor: '#dc3545 !important', }, invalid_feedback: { width: "100%",