|
| 1 | +import React from "react"; |
| 2 | + |
| 3 | +import { utils } from "@rjsf/core"; |
| 4 | +import { JSONSchema7 } from "json-schema"; |
| 5 | + |
| 6 | +import Grid from "@material-ui/core/Grid"; |
| 7 | +import FormControl from "@material-ui/core/FormControl"; |
| 8 | +import Input from "@material-ui/core/Input"; |
| 9 | +import InputLabel from "@material-ui/core/InputLabel"; |
| 10 | + |
| 11 | +import IconButton from "../IconButton/IconButton"; |
| 12 | + |
| 13 | +const { ADDITIONAL_PROPERTY_FLAG } = utils; |
| 14 | + |
| 15 | +type WrapIfAdditionalProps = { |
| 16 | + children: React.ReactElement; |
| 17 | + classNames: string; |
| 18 | + disabled: boolean; |
| 19 | + id: string; |
| 20 | + label: string; |
| 21 | + onDropPropertyClick: (index: string) => (event?: any) => void; |
| 22 | + onKeyChange: (index: string) => (event?: any) => void; |
| 23 | + readonly: boolean; |
| 24 | + required: boolean; |
| 25 | + schema: JSONSchema7; |
| 26 | +}; |
| 27 | + |
| 28 | +const WrapIfAdditional = ({ |
| 29 | + children, |
| 30 | + disabled, |
| 31 | + id, |
| 32 | + label, |
| 33 | + onDropPropertyClick, |
| 34 | + onKeyChange, |
| 35 | + readonly, |
| 36 | + required, |
| 37 | + schema, |
| 38 | +}: WrapIfAdditionalProps) => { |
| 39 | + const keyLabel = `${label} Key`; // i18n ? |
| 40 | + const additional = schema.hasOwnProperty(ADDITIONAL_PROPERTY_FLAG); |
| 41 | + const btnStyle = { |
| 42 | + flex: 1, |
| 43 | + paddingLeft: 6, |
| 44 | + paddingRight: 6, |
| 45 | + fontWeight: "bold", |
| 46 | + }; |
| 47 | + |
| 48 | + if (!additional) { |
| 49 | + return <>{children}</>; |
| 50 | + } |
| 51 | + |
| 52 | + const handleBlur = ({ target }: React.FocusEvent<HTMLInputElement>) => |
| 53 | + onKeyChange(target.value); |
| 54 | + |
| 55 | + return ( |
| 56 | + <Grid container={true} key={`${id}-key`} alignItems="center" spacing={2}> |
| 57 | + <Grid item={true} xs> |
| 58 | + <FormControl fullWidth={true} required={required}> |
| 59 | + <InputLabel>{keyLabel}</InputLabel> |
| 60 | + <Input |
| 61 | + defaultValue={label} |
| 62 | + disabled={disabled || readonly} |
| 63 | + id={`${id}-key`} |
| 64 | + name={`${id}-key`} |
| 65 | + onBlur={!readonly ? handleBlur : undefined} |
| 66 | + type="text" |
| 67 | + /> |
| 68 | + </FormControl> |
| 69 | + </Grid> |
| 70 | + <Grid item={true} xs> |
| 71 | + {children} |
| 72 | + </Grid> |
| 73 | + <Grid item={true}> |
| 74 | + <IconButton |
| 75 | + icon="remove" |
| 76 | + tabIndex={-1} |
| 77 | + style={btnStyle as any} |
| 78 | + disabled={disabled || readonly} |
| 79 | + onClick={onDropPropertyClick(label)} |
| 80 | + /> |
| 81 | + </Grid> |
| 82 | + </Grid> |
| 83 | + ); |
| 84 | +}; |
| 85 | + |
| 86 | +export default WrapIfAdditional; |
0 commit comments