DXC Logo

Date Input

Date inputs enable users to type or select a date in a predefined format.

Props

NameTypeDescriptionDefault
ariaLabelstringSpecifies a string to be used as the name for the date input element when no label is provided.'Date input'
autocompletestringHTML autocomplete attribute. Lets the user specify if any permission the user agent has to provide automated assistance in filling out the input value. Its value must be one of all the possible values of the HTML autocomplete attribute. See MDN for further information.'off'
clearablebooleanIf true, the date input will have an action to clear the entered value.false
defaultValuestringInitial value of the input element, only when it is uncontrolled.-
disabledbooleanIf true, the component will be disabled.false
errorstringIf it is a defined value and also a truthy string, the component will change its appearance, showing the error below the date input component. If the defined value is an empty string, it will reserve a space below the component for a future error, but it would not change its look. In case of being undefined or null, both the appearance and the space for the error message would not be modified.-
formatstringThe format in which the date value will be displayed. User must follow this format when editing the value or it will be considered as an invalid date. In this case, the onBlur and onChange functions will be called with an internal error as a parameter reporting the situation.'dd-MM-yyyy'
helperTextstringHelper text to be placed above the date.-
labelstringText to be placed above the date input.-
margin'xxsmall' | 'xsmall' | 'small' | 'medium' | 'large' | 'xlarge' | 'xxlarge' | MarginSize of the margin to be applied to the component. You can pass an object with 'top', 'bottom', 'left' and 'right' properties in order to specify different margin sizes.-
namestringName attribute of the input element.-
onBlur(val: { value: string; error?: string; date?: Date }) => voidThis function will be called when the input element loses the focus. An object including the string value, the error and the date value will be passed to this function. If the string value is a valid date, error will be undefined. Also, if the string value is not a valid date, date will be undefined.-
onChange(val: { value: string; error?: string; date?: Date }) => voidThis function will be called when the user types within the input element of the component. An object including the string value, the error and the date value will be passed to this function. If the string value is a valid date, error will be undefined. Also, if the string value is not a valid date, date will be undefined.-
optionalbooleanIf true, the date will be optional, showing the text '(Optional)' next to the label. Otherwise, the field will be considered required and an error will be passed as a parameter to the onBlur and onChange functions when it has not been filled.false
placeholderbooleanIf true, the date format will appear as placeholder in the field.false
readOnlybooleanIf true, the component will not be mutable, meaning the user can not edit the control. The date picker cannot be opened either. In addition, the clear action will not be displayed even if the flag is set to true.false
refReact.Ref<HTMLDivElement>Reference to the component.-
size'small' | 'medium' | 'large' | 'fillParent'Size of the component.'medium'
tabIndexnumberValue of the tabindex attribute.0
valuestringValue of the input element. If undefined, the component will be uncontrolled and the value will be managed internally by the component.-

Examples

Basic usage

() => {
  return (
    <DxcInset space="var(--spacing-padding-xl)">
      <DxcDateInput label="Start date" />
    </DxcInset>
  );
}

Controlled

() => {
  const [value, setValue] = useState("");
  const onChange = ({ value }) => {
    setValue(value);
  };
  const onBlur = ({ value }) => {
    setValue(value);
  };

  return (
    <DxcInset space="var(--spacing-padding-xl)">
      <DxcDateInput
        label="Start date"
        helperText="Please enter the start date."
        placeholder
        value={value}
        onChange={onChange}
        onBlur={onBlur}
        clearable
      />
    </DxcInset>
  );
}

Uncontrolled

() => {
  const inputRef = useRef();
  const handleSubmit = () => {
    const input = inputRef.current.getElementsByTagName("input")[0];
    console.log(input.value);
  };

  return (
    <DxcInset space="var(--spacing-padding-xl)">
      <DxcFlex direction="column" gap="var(--spacing-gap-xl)" justifyContent="flex-start">
        <DxcDateInput
          label="Start date"
          helperText="Please enter the start date."
          placeholder
          defaultValue="10-08-1998"
          clearable
          ref={inputRef}
        />
        <DxcButton label="Submit" onClick={handleSubmit} size={{width: "medium"}} />
      </DxcFlex>
    </DxcInset>
  );
}

Error handling

The component behaviour varies depending on the value of the error. We recommend reading the description of the prop carefully to fully understand the following example.

() => {
  const [value, setValue] = useState("");
  const [error, setError] = useState("");
  const onChange = ({ value }) => {
    setValue(value);
  };
  
  const onBlur = ({ value, error }) => {
    setValue(value);
    setError(error);
  };

  return (
    <DxcInset space="var(--spacing-padding-xl)">
      <DxcDateInput
        label="Start date"
        helperText="Please enter the start date."
        format="MM/dd/yyyy"
        value={value}
        onChange={onChange}
        onBlur={onBlur}
        error={error == undefined ? "" : error}
        clearable
        placeholder
      />
    </DxcInset>
  );
}