|
3 | 3 | * SPDX-License-Identifier: BSD-3-Clause
|
4 | 4 | */
|
5 | 5 |
|
| 6 | +import ClayIcon from '@clayui/icon'; |
6 | 7 | import classNames from 'classnames';
|
7 | 8 | import React from 'react';
|
8 | 9 |
|
@@ -155,6 +156,97 @@ const GroupInsetItem = React.forwardRef<
|
155 | 156 |
|
156 | 157 | GroupInsetItem.displayName = 'ClayInputGroupInsetItem';
|
157 | 158 |
|
| 159 | +interface IInlineTextProps |
| 160 | + extends React.HTMLAttributes< |
| 161 | + HTMLDivElement | HTMLSpanElement | HTMLLabelElement |
| 162 | + > { |
| 163 | + /** |
| 164 | + * The id of the component |
| 165 | + */ |
| 166 | + id?: string; |
| 167 | + |
| 168 | + /** |
| 169 | + * Text that hints at the expected data type to be entered. |
| 170 | + */ |
| 171 | + placeholder?: string; |
| 172 | + |
| 173 | + /** |
| 174 | + * A flag that sets the component as noneditable. |
| 175 | + */ |
| 176 | + readOnly?: boolean; |
| 177 | + |
| 178 | + /** |
| 179 | + * Path to the spritemap that Icon should use when referencing symbols. |
| 180 | + */ |
| 181 | + spritemap?: string; |
| 182 | +} |
| 183 | + |
| 184 | +const InlineText = React.forwardRef< |
| 185 | + HTMLDivElement | HTMLSpanElement | HTMLLabelElement, |
| 186 | + IInlineTextProps |
| 187 | +>( |
| 188 | + ({ |
| 189 | + children, |
| 190 | + className, |
| 191 | + id, |
| 192 | + placeholder, |
| 193 | + readOnly, |
| 194 | + spritemap, |
| 195 | + ...otherProps |
| 196 | + }: IInlineTextProps) => { |
| 197 | + const inputRef = React.useRef<HTMLDivElement>(null); |
| 198 | + |
| 199 | + const [inputValue, setInputValue] = React.useState(''); |
| 200 | + |
| 201 | + return ( |
| 202 | + <div className="form-control-fit-content inline-text-input"> |
| 203 | + <div className={classNames('form-control', className)}> |
| 204 | + <Input |
| 205 | + aria-hidden |
| 206 | + className="form-control-hidden" |
| 207 | + id={id} |
| 208 | + onFocus={() => { |
| 209 | + inputRef.current?.focus(); |
| 210 | + }} |
| 211 | + readOnly |
| 212 | + tabIndex={-1} |
| 213 | + type="text" |
| 214 | + value={inputValue} |
| 215 | + /> |
| 216 | + <span |
| 217 | + className="form-control-inset" |
| 218 | + // @ts-ignore |
| 219 | + contentEditable={readOnly ? false : 'plaintext-only'} |
| 220 | + onBlur={(event) => { |
| 221 | + if (inputValue.trim() === '') { |
| 222 | + const input = event.target as HTMLElement; |
| 223 | + |
| 224 | + input.innerHTML = ''; |
| 225 | + } |
| 226 | + }} |
| 227 | + onInput={(event) => { |
| 228 | + const input = event.target as HTMLElement; |
| 229 | + |
| 230 | + setInputValue(input.innerText); |
| 231 | + }} |
| 232 | + placeholder={placeholder} |
| 233 | + ref={inputRef} |
| 234 | + role="textbox" |
| 235 | + tabIndex={0} |
| 236 | + {...otherProps} |
| 237 | + /> |
| 238 | + <span className="form-control-indicator form-control-item"> |
| 239 | + <ClayIcon spritemap={spritemap} symbol="pencil" /> |
| 240 | + </span> |
| 241 | + {children} |
| 242 | + </div> |
| 243 | + </div> |
| 244 | + ); |
| 245 | + } |
| 246 | +); |
| 247 | + |
| 248 | +InlineText.displayName = 'ClayInlineText'; |
| 249 | + |
158 | 250 | interface IProps extends React.InputHTMLAttributes<HTMLInputElement> {
|
159 | 251 | /**
|
160 | 252 | * Input component to render. Can either be a string like 'input' or 'textarea' or a component.
|
@@ -183,6 +275,7 @@ interface IForwardRef<T, P = {}>
|
183 | 275 | GroupInsetItem: typeof GroupInsetItem;
|
184 | 276 | GroupItem: typeof GroupItem;
|
185 | 277 | GroupText: typeof GroupText;
|
| 278 | + InlineText: typeof InlineText; |
186 | 279 | }
|
187 | 280 |
|
188 | 281 | function forwardRef<T, P = {}>(component: React.RefForwardingComponent<T, P>) {
|
@@ -221,5 +314,6 @@ Input.Group = Group;
|
221 | 314 | Input.GroupInsetItem = GroupInsetItem;
|
222 | 315 | Input.GroupItem = GroupItem;
|
223 | 316 | Input.GroupText = GroupText;
|
| 317 | +Input.InlineText = InlineText; |
224 | 318 |
|
225 | 319 | export default Input;
|
0 commit comments