|
| 1 | +import * as React from "react"; |
| 2 | + |
| 3 | +import Tree from '../../../components/One/slots/TreeSlot'; |
| 4 | + |
| 5 | +import makeField from "../components/makeField"; |
| 6 | + |
| 7 | +import IManaged, { PickProp } from "../../../model/IManaged"; |
| 8 | +import IAnything from "../../../model/IAnything"; |
| 9 | +import IField from "../../../model/IField"; |
| 10 | + |
| 11 | +/** |
| 12 | + * Interface for the props of the ITreeField component. |
| 13 | + * |
| 14 | + * @template Data The type of data in the field. |
| 15 | + * @template Payload The type of payload in the field. |
| 16 | + */ |
| 17 | +export interface ITreeFieldProps<Data = IAnything, Payload = IAnything> { |
| 18 | + /** |
| 19 | + * Validation factory config |
| 20 | + * |
| 21 | + * @template IField - Type representing the field object. |
| 22 | + * @template Data - Type representing the data object. |
| 23 | + * @template Payload - Type representing the payload object. |
| 24 | + * |
| 25 | + * @returns The value of the "validation" property. |
| 26 | + */ |
| 27 | + validation?: PickProp<IField<Data, Payload>, 'validation'>; |
| 28 | + /** |
| 29 | + * Returns the "description" property of a given object. |
| 30 | + * |
| 31 | + * @template T - The type of the object. |
| 32 | + * @template K - The literal key type. |
| 33 | + * |
| 34 | + * @param obj - The object from which to extract the property. |
| 35 | + * @param key - The literal key representing the property to extract. |
| 36 | + * |
| 37 | + * @returns - The value of the specified property. |
| 38 | + */ |
| 39 | + description?: PickProp<IField<Data, Payload>, "description">; |
| 40 | + /** |
| 41 | + * Type definition for the "title" property picked from the given object type. |
| 42 | + * |
| 43 | + * @template IField - The object type that contains the "title" property. |
| 44 | + * @template Data - The data type of the "title" property. |
| 45 | + * @template Payload - The payload type of the "title" property. |
| 46 | + * |
| 47 | + * @param - The object from which the "title" property will be picked. |
| 48 | + * |
| 49 | + * @returns - The resulting object that only contains the "title" property. |
| 50 | + */ |
| 51 | + title?: PickProp<IField<Data, Payload>, "title">; |
| 52 | + /** |
| 53 | + * Type definition for the variable placeholder. |
| 54 | + * |
| 55 | + * @template Data - The type of data for the field. |
| 56 | + * @template Payload - The type of payload for the field. |
| 57 | + * @typedef placeholder |
| 58 | + */ |
| 59 | + placeholder?: PickProp<IField<Data, Payload>, "placeholder">; |
| 60 | + /** |
| 61 | + * Specifies if a field is readOnly. |
| 62 | + * |
| 63 | + * @typedef Readonly |
| 64 | + * |
| 65 | + * @typedef IField |
| 66 | + * @typedef Payload |
| 67 | + * @typedef PickProp |
| 68 | + * @typedef Data |
| 69 | + * |
| 70 | + * @param readonly - The field being checked for readOnly status. |
| 71 | + * |
| 72 | + * @returns - A boolean value indicating if the field is readOnly. |
| 73 | + */ |
| 74 | + readonly?: PickProp<IField<Data, Payload>, "readonly">; |
| 75 | + /** |
| 76 | + * Represents the `disabled` property of a field in a form. |
| 77 | + * |
| 78 | + * @typedef Disabled |
| 79 | + * |
| 80 | + * @property disabled - Indicates whether the field is disabled or not. |
| 81 | + * @template Data - The type of data stored in the form. |
| 82 | + * @template Payload - The type of payload used for form submission. |
| 83 | + */ |
| 84 | + disabled?: PickProp<IField<Data, Payload>, "disabled">; |
| 85 | + /** |
| 86 | + * Represents the item tree of a specific field in the data payload. |
| 87 | + * |
| 88 | + * @typedef ItemTree |
| 89 | + */ |
| 90 | + itemTree?: PickProp<IField<Data, Payload>, 'itemTree'>; |
| 91 | +} |
| 92 | + |
| 93 | +/** |
| 94 | + * Represents the private interface for the TreeField component. |
| 95 | + * |
| 96 | + * @template Data The type of data for the TreeField component. |
| 97 | + */ |
| 98 | +export interface ITreeFieldPrivate<Data = IAnything> { |
| 99 | + onChange: PickProp<IManaged<Data>, "onChange">; |
| 100 | + invalid: PickProp<IManaged<Data>, "invalid">; |
| 101 | + incorrect: PickProp<IManaged<Data>, "incorrect">; |
| 102 | + value: PickProp<IManaged<Data>, "value">; |
| 103 | + loading: PickProp<IManaged<Data>, "loading">; |
| 104 | + disabled: PickProp<IManaged<Data>, "disabled">; |
| 105 | + dirty: PickProp<IManaged<Data>, "dirty">; |
| 106 | + name: PickProp<IManaged<Data>, "name">; |
| 107 | +} |
| 108 | + |
| 109 | +/** |
| 110 | + * Renders a TreeField component. |
| 111 | + * |
| 112 | + * @param props - The props object containing the necessary data for rendering the TreeField. |
| 113 | + * @param props.invalid - Determines if the TreeField is invalid. |
| 114 | + * @param props.value - The value of the TreeField. |
| 115 | + * @param props.disabled - Determines if the TreeField is disabled. |
| 116 | + * @param props.readonly - Determines if the TreeField is readonly. |
| 117 | + * @param props.incorrect - Determines if the TreeField is incorrect. |
| 118 | + * @param props.description - The description text for the TreeField. |
| 119 | + * @param props.outlined - Determines if the TreeField should have an outlined style. |
| 120 | + * @param props.title - The title text for the TreeField. |
| 121 | + * @param props.placeholder - The placeholder text for the TreeField. |
| 122 | + * @param props.itemTree - The itemTree object for rendering the Tree. |
| 123 | + * @param props.dirty - Determines if the TreeField has been modified. |
| 124 | + * @param props.loading - Determines if the TreeField is currently loading. |
| 125 | + * @param props.onChange - The callback function to be called when the value of the TreeField changes. |
| 126 | + * @param props.name - The name attribute of the TreeField. |
| 127 | + * @param props.withContextMenu - Determines if the TreeField should have a context menu. |
| 128 | + * |
| 129 | + * @returns The rendered TreeField component. |
| 130 | + */ |
| 131 | +export const TreeField = ({ |
| 132 | + invalid, |
| 133 | + value, |
| 134 | + disabled, |
| 135 | + readonly, |
| 136 | + incorrect, |
| 137 | + description = "", |
| 138 | + title = "", |
| 139 | + placeholder = "", |
| 140 | + itemTree, |
| 141 | + dirty, |
| 142 | + loading, |
| 143 | + onChange, |
| 144 | + name, |
| 145 | +}: ITreeFieldProps & ITreeFieldPrivate) => ( |
| 146 | + <Tree |
| 147 | + invalid={invalid} |
| 148 | + incorrect={incorrect} |
| 149 | + value={value} |
| 150 | + readonly={readonly} |
| 151 | + disabled={disabled} |
| 152 | + description={description} |
| 153 | + title={title} |
| 154 | + placeholder={placeholder} |
| 155 | + itemTree={itemTree} |
| 156 | + dirty={dirty} |
| 157 | + loading={loading} |
| 158 | + onChange={onChange} |
| 159 | + name={name} |
| 160 | + /> |
| 161 | +); |
| 162 | + |
| 163 | +TreeField.displayName = 'TreeField'; |
| 164 | + |
| 165 | +export default makeField(TreeField, { |
| 166 | + withApplyQueue: true, |
| 167 | + skipDirtyPressListener: true, |
| 168 | + skipFocusReadonly: true, |
| 169 | + skipDebounce: true, |
| 170 | +}); |
0 commit comments